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/reservations/reservationsSlice'
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 EditReservationsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    tenant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    booking_request: null,
    

    
    
    
    'reservation_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    property: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    unit: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    unit_type: null,
    

    
    
    

    

    

    

    

    
    check_in_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    check_out_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    actual_check_in_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    actual_check_out_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    early_check_in: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    late_check_out: false,
    

    

    

    

    

    

    
    
    

    

    

    
    guest_count: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'nightly_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'monthly_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'currency': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    internal_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    external_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    guests: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    service_requests: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    invoices: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    documents: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof reservations === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (reservations)[el])
          setInitialValues(newInitialVal);
      }
  }, [reservations])

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit Reservation')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit Reservation'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox className='mx-auto max-w-6xl'>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>
              <div className='grid gap-5 md:grid-cols-2'>


  

  

  

  

  

  

  

  

  

  
  
    <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='Bookingrequest' labelFor='booking_request'>
        <Field
            name='booking_request'
            id='booking_request'
            component={SelectField}
            options={initialValues.booking_request}
            itemRef={'booking_requests'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'request_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Reservationcode"
    >
        <Field
            name="reservation_code"
            placeholder="Reservationcode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="quoted">quoted</option>
          
            <option value="confirmed">confirmed</option>
          
            <option value="checked_in">checked_in</option>
          
            <option value="checked_out">checked_out</option>
          
            <option value="canceled">canceled</option>
          
            <option value="no_show">no_show</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Unit' labelFor='unit'>
        <Field
            name='unit'
            id='unit'
            component={SelectField}
            options={initialValues.unit}
            itemRef={'units'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'unit_number'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Unittype' labelFor='unit_type'>
        <Field
            name='unit_type'
            id='unit_type'
            component={SelectField}
            options={initialValues.unit_type}
            itemRef={'unit_types'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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="Actualcheck-inat"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.actual_check_in_at ?
                  new Date(
                      dayjs(initialValues.actual_check_in_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'actual_check_in_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Earlycheck-in' labelFor='early_check_in'>
        <Field
            name='early_check_in'
            id='early_check_in'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Latecheck-out' labelFor='late_check_out'>
        <Field
            name='late_check_out'
            id='late_check_out'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Nightlyrate"
    >
        <Field
            type="number"
            name="nightly_rate"
            placeholder="Nightlyrate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Monthlyrate"
    >
        <Field
            type="number"
            name="monthly_rate"
            placeholder="Monthlyrate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Internalnotes" hasTextareaHeight>
        <Field name="internal_notes" as="textarea" placeholder="Internalnotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Externalnotes" hasTextareaHeight>
        <Field name="external_notes" as="textarea" placeholder="Externalnotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Guests' labelFor='guests'>
          <Field
              name='guests'
              id='guests'
              component={SelectFieldMany}
              options={initialValues.guests}
              itemRef={'reservation_guests'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'full_name'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Servicerequests' labelFor='service_requests'>
          <Field
              name='service_requests'
              id='service_requests'
              component={SelectFieldMany}
              options={initialValues.service_requests}
              itemRef={'service_requests'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'summary'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Invoices' labelFor='invoices'>
          <Field
              name='invoices'
              id='invoices'
              component={SelectFieldMany}
              options={initialValues.invoices}
              itemRef={'invoices'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'invoice_number'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></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>
  

    

  


  

              </div>

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

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

export default EditReservationsPage
