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 EditBookingsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    learner: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    tutor: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    gig: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    gig_level: null,
    

    
    
    

    

    

    
    level_number: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'level_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'platform_fee': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tutor_payout': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    escrow_status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    scheduled_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    accepted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    milestone_submitted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    auto_release_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    completed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    deliverable_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    deliverable_files: [],
    

    

    

    
    
    

    

    

    
    revision_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    revision_reason: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'jitsi_meet_meeting_id': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'jitsi_meet_password': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    admin_note: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

  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: id, 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='Learner' labelFor='learner'>
        <Field
            name='learner'
            id='learner'
            component={SelectField}
            options={initialValues.learner}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Gig' labelFor='gig'>
        <Field
            name='gig'
            id='gig'
            component={SelectField}
            options={initialValues.gig}
            itemRef={'gigs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='GigLevel' labelFor='gig_level'>
        <Field
            name='gig_level'
            id='gig_level'
            component={SelectField}
            options={initialValues.gig_level}
            itemRef={'gig_levels'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="LevelNumberSnapshot"
    >
        <Field
            type="number"
            name="level_number"
            placeholder="LevelNumberSnapshot"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="LevelNameSnapshot"
    >
        <Field
            name="level_name"
            placeholder="LevelNameSnapshot"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Price"
    >
        <Field
            type="number"
            name="price"
            placeholder="Price"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PlatformFee"
    >
        <Field
            type="number"
            name="platform_fee"
            placeholder="PlatformFee"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TutorPayout"
    >
        <Field
            type="number"
            name="tutor_payout"
            placeholder="TutorPayout"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BookingStatus" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="pending_acceptance">pending_acceptance</option>
          
            <option value="active">active</option>
          
            <option value="milestone_submitted">milestone_submitted</option>
          
            <option value="revision_requested">revision_requested</option>
          
            <option value="completed">completed</option>
          
            <option value="disputed">disputed</option>
          
            <option value="cancelled">cancelled</option>
          
            <option value="refunded">refunded</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EscrowStatus" labelFor="escrow_status">
        <Field name="escrow_status" id="escrow_status" component="select">
          
            <option value="held">held</option>
          
            <option value="released">released</option>
          
            <option value="refunded">refunded</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="DeliverableNotes" hasTextareaHeight>
        <Field name="deliverable_notes" as="textarea" placeholder="DeliverableNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='DeliverableFiles'
            color='info'
            icon={mdiUpload}
            path={'bookings/deliverable_files'}
            name='deliverable_files'
            id='deliverable_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  
    <FormField
        label="RevisionCount"
    >
        <Field
            type="number"
            name="revision_count"
            placeholder="RevisionCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="RevisionReason" hasTextareaHeight>
        <Field name="revision_reason" as="textarea" placeholder="RevisionReason" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="JitsiMeetingID"
    >
        <Field
            name="jitsi_meet_meeting_id"
            placeholder="JitsiMeetingID"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="JitsiMeetingPassword"
    >
        <Field
            name="jitsi_meet_password"
            placeholder="JitsiMeetingPassword"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="AdminNote" hasTextareaHeight>
        <Field name="admin_note" as="textarea" placeholder="AdminNote" />
    </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>
    </>
  )
}

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

export default EditBookingsPage
