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/driver_compliances/driver_compliancesSlice'
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 EditDriver_compliancesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    driver_profile: null,
    

    
    
    
    'license_country': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'license_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    license_issued_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    license_expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    license_restrictions: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    license_suspended: false,
    

    

    

    

    

    

    
    
    
    'cpc_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    cpc_issued_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    cpc_expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'cpc_center': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    cpc_last_training_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    cpc_next_training_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'cpc_type': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'adr_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    adr_expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    adr_tank: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    adr_packages: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    adr_last_course_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    adr_next_course_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    adr_restrictions: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'medical_cert_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'medical_issued_by': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    medical_expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'psych_test_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    psych_expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    eye_exam_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    cardio_exam_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    medical_restrictions: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tacho_card_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tacho_country': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    tacho_expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    tacho_last_download_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    tacho_next_download_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    violations_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    warnings_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    driving_ban: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    driving_ban_until: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='DriverProfile' labelFor='driver_profile'>
        <Field
            name='driver_profile'
            id='driver_profile'
            component={SelectField}
            options={initialValues.driver_profile}
            itemRef={'driver_profiles'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'driver_notes'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="LicenseCountry"
    >
        <Field
            name="license_country"
            placeholder="LicenseCountry"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="LicenseNumber"
    >
        <Field
            name="license_number"
            placeholder="LicenseNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="LicenseRestrictions" hasTextareaHeight>
        <Field name="license_restrictions" as="textarea" placeholder="LicenseRestrictions" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='LicenseSuspended' labelFor='license_suspended'>
        <Field
            name='license_suspended'
            id='license_suspended'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  
    <FormField
        label="CPCNumber"
    >
        <Field
            name="cpc_number"
            placeholder="CPCNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="CPCCenter"
    >
        <Field
            name="cpc_center"
            placeholder="CPCCenter"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="CPCType"
    >
        <Field
            name="cpc_type"
            placeholder="CPCType"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ADRNumber"
    >
        <Field
            name="adr_number"
            placeholder="ADRNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ADRTank' labelFor='adr_tank'>
        <Field
            name='adr_tank'
            id='adr_tank'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ADRPackages' labelFor='adr_packages'>
        <Field
            name='adr_packages'
            id='adr_packages'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="ADRRestrictions" hasTextareaHeight>
        <Field name="adr_restrictions" as="textarea" placeholder="ADRRestrictions" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="MedicalCertificateNumber"
    >
        <Field
            name="medical_cert_number"
            placeholder="MedicalCertificateNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="MedicalIssuedBy"
    >
        <Field
            name="medical_issued_by"
            placeholder="MedicalIssuedBy"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="PsychTestNumber"
    >
        <Field
            name="psych_test_number"
            placeholder="PsychTestNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="MedicalRestrictions" hasTextareaHeight>
        <Field name="medical_restrictions" as="textarea" placeholder="MedicalRestrictions" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TachoCardNumber"
    >
        <Field
            name="tacho_card_number"
            placeholder="TachoCardNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TachoCountry"
    >
        <Field
            name="tacho_country"
            placeholder="TachoCountry"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ViolationsCount"
    >
        <Field
            type="number"
            name="violations_count"
            placeholder="ViolationsCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="WarningsCount"
    >
        <Field
            type="number"
            name="warnings_count"
            placeholder="WarningsCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='DrivingBan' labelFor='driving_ban'>
        <Field
            name='driving_ban'
            id='driving_ban'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="DrivingBanUntil"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.driving_ban_until ?
                  new Date(
                      dayjs(initialValues.driving_ban_until).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'driving_ban_until': date})}
          />
      </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('/driver_compliances/driver_compliances-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditDriver_compliancesPage
