import { mdiChartTimelineVariant, mdiUpload } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement, useEffect, useState } from 'react'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";

import CardBox from '../../components/CardBox'
import LayoutAuthenticated from '../../layouts/Authenticated'
import SectionMain from '../../components/SectionMain'
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton'
import { getPageTitle } from '../../config'

import { Field, Form, Formik } from 'formik'
import FormField from '../../components/FormField'
import BaseDivider from '../../components/BaseDivider'
import BaseButtons from '../../components/BaseButtons'
import BaseButton from '../../components/BaseButton'
import FormCheckRadio from '../../components/FormCheckRadio'
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup'
import FormFilePicker from '../../components/FormFilePicker'
import FormImagePicker from '../../components/FormImagePicker'
import { SelectField } from "../../components/SelectField";
import { SelectFieldMany } from "../../components/SelectFieldMany";
import { SwitchField } from '../../components/SwitchField'
import {RichTextField} from "../../components/RichTextField";

import { update, fetch } from '../../stores/booking_requests/booking_requestsSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";

import {hasPermission} from "../../helpers/userPermissions";



const EditBooking_requests = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    tenant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    requested_by: null,
    

    
    
    
    'request_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    check_in_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    check_out_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    preferred_property: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    preferred_unit_type: null,
    

    
    
    

    

    

    
    preferred_bedrooms: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    guest_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    purpose_of_stay: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    special_requirements: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'budget_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'max_budget_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'currency': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    travelers: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    approval_steps: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    documents: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    comments: [],
    
    
    
  }
  const [initialValues, setInitialValues] = useState(initVals)

  const { booking_requests } = useAppSelector((state) => state.booking_requests)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { booking_requestsId } = router.query

  useEffect(() => {
    dispatch(fetch({ id: booking_requestsId }))
  }, [booking_requestsId])

  useEffect(() => {
    if (typeof booking_requests === 'object') {
      setInitialValues(booking_requests)
    }
  }, [booking_requests])

  useEffect(() => {
      if (typeof booking_requests === 'object') {

          const newInitialVal = {...initVals};

          Object.keys(initVals).forEach(el => newInitialVal[el] = (booking_requests)[el])

          setInitialValues(newInitialVal);
      }
  }, [booking_requests])

  const handleSubmit = async (data) => {
    await dispatch(update({ id: booking_requestsId, data }))
    await router.push('/booking_requests/booking_requests-list')
  }

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit booking_requests')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit booking_requests'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Tenant' labelFor='tenant'>
        <Field
            name='tenant'
            id='tenant'
            component={SelectField}
            options={initialValues.tenant}
            itemRef={'tenants'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
    {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
    <FormField label='Organization' labelFor='organization'>
        <Field
            name='organization'
            id='organization'
            component={SelectField}
            options={initialValues.organization}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
     } 

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Requestedby' labelFor='requested_by'>
        <Field
            name='requested_by'
            id='requested_by'
            component={SelectField}
            options={initialValues.requested_by}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Requestcode"
    >
        <Field
            name="request_code"
            placeholder="Requestcode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="submitted">submitted</option>
          
            <option value="in_review">in_review</option>
          
            <option value="changes_requested">changes_requested</option>
          
            <option value="approved">approved</option>
          
            <option value="rejected">rejected</option>
          
            <option value="expired">expired</option>
          
            <option value="converted_to_reservation">converted_to_reservation</option>
          
            <option value="canceled">canceled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="Check-inat"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.check_in_at ?
                  new Date(
                      dayjs(initialValues.check_in_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'check_in_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="Check-outat"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.check_out_at ?
                  new Date(
                      dayjs(initialValues.check_out_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'check_out_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Preferredproperty' labelFor='preferred_property'>
        <Field
            name='preferred_property'
            id='preferred_property'
            component={SelectField}
            options={initialValues.preferred_property}
            itemRef={'properties'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Preferredunittype' labelFor='preferred_unit_type'>
        <Field
            name='preferred_unit_type'
            id='preferred_unit_type'
            component={SelectField}
            options={initialValues.preferred_unit_type}
            itemRef={'unit_types'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="Preferredbedrooms"
    >
        <Field
            type="number"
            name="preferred_bedrooms"
            placeholder="Preferredbedrooms"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Guestcount"
    >
        <Field
            type="number"
            name="guest_count"
            placeholder="Guestcount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Purposeofstay" hasTextareaHeight>
        <Field name="purpose_of_stay" as="textarea" placeholder="Purposeofstay" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Specialrequirements" hasTextareaHeight>
        <Field name="special_requirements" as="textarea" placeholder="Specialrequirements" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Budgetcode"
    >
        <Field
            name="budget_code"
            placeholder="Budgetcode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Maxbudgetamount"
    >
        <Field
            type="number"
            name="max_budget_amount"
            placeholder="Maxbudgetamount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Currency"
    >
        <Field
            name="currency"
            placeholder="Currency"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Travelers' labelFor='travelers'>
          <Field
              name='travelers'
              id='travelers'
              component={SelectFieldMany}
              options={initialValues.travelers}
              itemRef={'booking_request_travelers'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'full_name'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Approvalsteps' labelFor='approval_steps'>
          <Field
              name='approval_steps'
              id='approval_steps'
              component={SelectFieldMany}
              options={initialValues.approval_steps}
              itemRef={'approval_steps'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'decision_notes'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Documents' labelFor='documents'>
          <Field
              name='documents'
              id='documents'
              component={SelectFieldMany}
              options={initialValues.documents}
              itemRef={'documents'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'file_name'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Comments' labelFor='comments'>
          <Field
              name='comments'
              id='comments'
              component={SelectFieldMany}
              options={initialValues.comments}
              itemRef={'activity_comments'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'body'}
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  


  

              <BaseDivider />
              <BaseButtons>
                <BaseButton type="submit" color="info" label="Submit" />
                <BaseButton type="reset" color="info" outline label="Reset" />
                <BaseButton type='reset' color='danger' outline label='Cancel' onClick={() => router.push('/booking_requests/booking_requests-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

EditBooking_requests.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated
        
          permission={'UPDATE_BOOKING_REQUESTS'}
        
      >
          {page}
      </LayoutAuthenticated>
  )
}

export default EditBooking_requests
