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/trailers/trailersSlice'
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 EditTrailersPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    company: null,
    

    
    
    

    

    

    

    

    

    

    

    
    type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    ownership: '',
    

    

    

    

    
    
    
    'plate_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vin': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'brand': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'model': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    year: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    first_registration_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    gross_weight_kg: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    curb_weight_kg: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    payload_kg: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    axle_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    has_liftgate: false,
    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    photo: [],
    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit trailers')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit trailers'} 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="TrailerType" labelFor="type">
        <Field name="type" id="type" component="select">
          
            <option value="SEMI_TRAILER">SEMI_TRAILER</option>
          
            <option value="FULL_TRAILER">FULL_TRAILER</option>
          
            <option value="DOLLY">DOLLY</option>
          
            <option value="OTHER">OTHER</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="ACTIVE">ACTIVE</option>
          
            <option value="INACTIVE">INACTIVE</option>
          
            <option value="SOLD">SOLD</option>
          
            <option value="SCRAPPED">SCRAPPED</option>
          
            <option value="SUSPENDED">SUSPENDED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Ownership" labelFor="ownership">
        <Field name="ownership" id="ownership" component="select">
          
            <option value="OWNED">OWNED</option>
          
            <option value="LEASED">LEASED</option>
          
            <option value="RENTED">RENTED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="PlateNumber"
    >
        <Field
            name="plate_number"
            placeholder="PlateNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="VIN"
    >
        <Field
            name="vin"
            placeholder="VIN"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Brand"
    >
        <Field
            name="brand"
            placeholder="Brand"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="GrossWeight(kg)"
    >
        <Field
            type="number"
            name="gross_weight_kg"
            placeholder="GrossWeight(kg)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CurbWeight(kg)"
    >
        <Field
            type="number"
            name="curb_weight_kg"
            placeholder="CurbWeight(kg)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Payload(kg)"
    >
        <Field
            type="number"
            name="payload_kg"
            placeholder="Payload(kg)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AxleCount"
    >
        <Field
            type="number"
            name="axle_count"
            placeholder="AxleCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='HasLiftgate' labelFor='has_liftgate'>
        <Field
            name='has_liftgate'
            id='has_liftgate'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

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

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='Photo'
                color='info'
                icon={mdiUpload}
                path={'trailers/photo'}
                name='photo'
                id='photo'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></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('/trailers/trailers-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditTrailersPage
