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/bookings/bookingsSlice'
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";



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

    

    

    

    

    

    

    

    

    

    

    
    hotel: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    primary_guest: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    room_type: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    rate_plan: null,
    

    
    
    
    'booking_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    check_in_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    check_out_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    adults: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    children: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    source: '',
    

    

    

    

    
    
    
    'total_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tax_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'city_tax_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'currency': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'deposit_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    deposit_status: '',
    

    

    

    

    
    
    

    
    special_requests: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    cancelled_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    cancellation_reason: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    reserved_rooms: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    booking_guests: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    payments: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

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

  const { bookings } = useAppSelector((state) => state.bookings)
  

  const { bookingsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Hotel' labelFor='hotel'>
        <Field
            name='hotel'
            id='hotel'
            component={SelectField}
            options={initialValues.hotel}
            itemRef={'hotels'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PrimaryGuest' labelFor='primary_guest'>
        <Field
            name='primary_guest'
            id='primary_guest'
            component={SelectField}
            options={initialValues.primary_guest}
            itemRef={'guests'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'email'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RoomType' labelFor='room_type'>
        <Field
            name='room_type'
            id='room_type'
            component={SelectField}
            options={initialValues.room_type}
            itemRef={'room_types'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RatePlan' labelFor='rate_plan'>
        <Field
            name='rate_plan'
            id='rate_plan'
            component={SelectField}
            options={initialValues.rate_plan}
            itemRef={'rate_plans'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="BookingCode"
    >
        <Field
            name="booking_code"
            placeholder="BookingCode"
        />
    </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="Adults"
    >
        <Field
            type="number"
            name="adults"
            placeholder="Adults"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Children"
    >
        <Field
            type="number"
            name="children"
            placeholder="Children"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

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

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Source" labelFor="source">
        <Field name="source" id="source" component="select">
          
            <option value="web">web</option>
          
            <option value="admin">admin</option>
          
            <option value="phone">phone</option>
          
            <option value="email">email</option>
          
            <option value="ota">ota</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalAmount"
    >
        <Field
            type="number"
            name="total_amount"
            placeholder="TotalAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TaxAmount"
    >
        <Field
            type="number"
            name="tax_amount"
            placeholder="TaxAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CityTaxAmount"
    >
        <Field
            type="number"
            name="city_tax_amount"
            placeholder="CityTaxAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DepositAmount"
    >
        <Field
            type="number"
            name="deposit_amount"
            placeholder="DepositAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DepositStatus" labelFor="deposit_status">
        <Field name="deposit_status" id="deposit_status" component="select">
          
            <option value="none">none</option>
          
            <option value="requested">requested</option>
          
            <option value="paid">paid</option>
          
            <option value="refunded">refunded</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="SpecialRequests" hasTextareaHeight>
        <Field name="special_requests" as="textarea" placeholder="SpecialRequests" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="CancellationReason" hasTextareaHeight>
        <Field name="cancellation_reason" as="textarea" placeholder="CancellationReason" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='ReservedRooms' labelFor='reserved_rooms'>
          <Field
              name='reserved_rooms'
              id='reserved_rooms'
              component={SelectFieldMany}
              options={initialValues.reserved_rooms}
              itemRef={'rooms'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'room_number'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='BookingGuests' labelFor='booking_guests'>
          <Field
              name='booking_guests'
              id='booking_guests'
              component={SelectFieldMany}
              options={initialValues.booking_guests}
              itemRef={'booking_guests'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'guest_role'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Payments' labelFor='payments'>
          <Field
              name='payments'
              id='payments'
              component={SelectFieldMany}
              options={initialValues.payments}
              itemRef={'payments'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'provider_reference'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Messages' labelFor='messages'>
          <Field
              name='messages'
              id='messages'
              component={SelectFieldMany}
              options={initialValues.messages}
              itemRef={'messages'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'subject'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></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('/bookings/bookings-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditBookings
