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/vehicles/vehiclesSlice'
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 EditVehicles = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    company: null,
    

    
    
    
    'vehicle_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'plate_no': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'engine_no': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'chassis_no': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'make': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'model': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    year: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'colour': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'category': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    purchase_date: new Date(),
    

    

    

    

    

    

    

    
    
    
    'purchase_cost': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    ownership_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    current_driver: null,
    

    
    
    
    'branch': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    odometer: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'insurance_policy_no': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    created_on: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    modified_on: new Date(),
    

    

    

    

    

    

    

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

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

  const { vehiclesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Company' labelFor='company'>
        <Field
            name='company'
            id='company'
            component={SelectField}
            options={initialValues.company}
            itemRef={'companies'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="VehicleCode"
    >
        <Field
            name="vehicle_code"
            placeholder="VehicleCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="PlateNo"
    >
        <Field
            name="plate_no"
            placeholder="PlateNo"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="EngineNo"
    >
        <Field
            name="engine_no"
            placeholder="EngineNo"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ChassisNo"
    >
        <Field
            name="chassis_no"
            placeholder="ChassisNo"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Make"
    >
        <Field
            name="make"
            placeholder="Make"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Model"
    >
        <Field
            name="model"
            placeholder="Model"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Year"
    >
        <Field
            type="number"
            name="year"
            placeholder="Year"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Colour"
    >
        <Field
            name="colour"
            placeholder="Colour"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Category"
    >
        <Field
            name="category"
            placeholder="Category"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PurchaseCost"
    >
        <Field
            type="number"
            name="purchase_cost"
            placeholder="PurchaseCost"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="OwnershipType" labelFor="ownership_type">
        <Field name="ownership_type" id="ownership_type" component="select">
          
            <option value="owned">owned</option>
          
            <option value="leased">leased</option>
          
            <option value="financed">financed</option>
          
            <option value="rented">rented</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="available">available</option>
          
            <option value="assigned">assigned</option>
          
            <option value="maintenance">maintenance</option>
          
            <option value="inactive">inactive</option>
          
            <option value="sold">sold</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CurrentDriver' labelFor='current_driver'>
        <Field
            name='current_driver'
            id='current_driver'
            component={SelectField}
            options={initialValues.current_driver}
            itemRef={'drivers'}
      
        
      
        
      
        
      
        
      
        
            showField={'driver_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Branch"
    >
        <Field
            name="branch"
            placeholder="Branch"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Odometer"
    >
        <Field
            type="number"
            name="odometer"
            placeholder="Odometer"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="InsurancePolicyNo"
    >
        <Field
            name="insurance_policy_no"
            placeholder="InsurancePolicyNo"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

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

export default EditVehicles
