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/travel_requests/travel_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 EditTravel_requests = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'origin': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'destination': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    depart_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    return_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    trip_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    cabin: '',
    

    

    

    

    
    
    

    

    

    
    travelers_count: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'max_budget': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    preferences: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { travel_requestsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="Origin"
    >
        <Field
            name="origin"
            placeholder="Origin"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Destination"
    >
        <Field
            name="destination"
            placeholder="Destination"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="TripType" labelFor="trip_type">
        <Field name="trip_type" id="trip_type" component="select">
          
            <option value="one_way">one_way</option>
          
            <option value="round_trip">round_trip</option>
          
            <option value="multi_city">multi_city</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Cabin" labelFor="cabin">
        <Field name="cabin" id="cabin" component="select">
          
            <option value="economy">economy</option>
          
            <option value="premium_economy">premium_economy</option>
          
            <option value="business">business</option>
          
            <option value="first">first</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TravelersCount"
    >
        <Field
            type="number"
            name="travelers_count"
            placeholder="TravelersCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaxBudget"
    >
        <Field
            type="number"
            name="max_budget"
            placeholder="MaxBudget"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Preferences" hasTextareaHeight>
        <Field name="preferences" as="textarea" placeholder="Preferences" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="searching">searching</option>
          
            <option value="needs_confirmation">needs_confirmation</option>
          
            <option value="booked">booked</option>
          
            <option value="canceled">canceled</option>
          
            <option value="failed">failed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditTravel_requests
