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/vehicle_categories/vehicle_categoriesSlice'
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 EditVehicle_categories = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    transmission: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    fuel_type: '',
    

    

    

    

    
    
    

    

    

    
    seats: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    doors: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    large_bags: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    small_bags: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    has_air_conditioning: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    image: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { vehicle_categoriesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="CategoryName"
    >
        <Field
            name="name"
            placeholder="CategoryName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Description" hasTextareaHeight>
        <Field name="description" as="textarea" placeholder="Description" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Transmission" labelFor="transmission">
        <Field name="transmission" id="transmission" component="select">
          
            <option value="automatic">automatic</option>
          
            <option value="manual">manual</option>
          
            <option value="any">any</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="FuelType" labelFor="fuel_type">
        <Field name="fuel_type" id="fuel_type" component="select">
          
            <option value="petrol">petrol</option>
          
            <option value="diesel">diesel</option>
          
            <option value="hybrid">hybrid</option>
          
            <option value="electric">electric</option>
          
            <option value="plugin_hybrid">plugin_hybrid</option>
          
            <option value="any">any</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Seats"
    >
        <Field
            type="number"
            name="seats"
            placeholder="Seats"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Doors"
    >
        <Field
            type="number"
            name="doors"
            placeholder="Doors"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LargeBags"
    >
        <Field
            type="number"
            name="large_bags"
            placeholder="LargeBags"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SmallBags"
    >
        <Field
            type="number"
            name="small_bags"
            placeholder="SmallBags"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='HasAirConditioning' labelFor='has_air_conditioning'>
        <Field
            name='has_air_conditioning'
            id='has_air_conditioning'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

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

export default EditVehicle_categories
