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/loads/loadsSlice'
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 EditLoads = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    company: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    order: null,
    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'load_no': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    equipment: '',
    

    

    

    

    
    
    

    
    cargo_description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    weight_kg: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'volume_m3': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    pallets: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    pieces: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'temperature_min_c': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'temperature_max_c': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    adr_required: false,
    

    

    

    

    

    

    
    
    
    'cmr_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { loadsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit loads')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit loads'} 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='Order' labelFor='order'>
        <Field
            name='order'
            id='order'
            component={SelectField}
            options={initialValues.order}
            itemRef={'orders'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'order_no'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="PLANNED">PLANNED</option>
          
            <option value="ASSIGNED">ASSIGNED</option>
          
            <option value="IN_TRANSIT">IN_TRANSIT</option>
          
            <option value="DELIVERED">DELIVERED</option>
          
            <option value="CANCELLED">CANCELLED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="LoadNo"
    >
        <Field
            name="load_no"
            placeholder="LoadNo"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Reference"
    >
        <Field
            name="reference"
            placeholder="Reference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EquipmentRequirement" labelFor="equipment">
        <Field name="equipment" id="equipment" component="select">
          
            <option value="NONE">NONE</option>
          
            <option value="FRIGO">FRIGO</option>
          
            <option value="ADR">ADR</option>
          
            <option value="LIFTGATE">LIFTGATE</option>
          
            <option value="DOUBLE_CREW">DOUBLE_CREW</option>
          
            <option value="OTHER">OTHER</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="CargoDescription" hasTextareaHeight>
        <Field name="cargo_description" as="textarea" placeholder="CargoDescription" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Weight(kg)"
    >
        <Field
            type="number"
            name="weight_kg"
            placeholder="Weight(kg)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Volume(m³)"
    >
        <Field
            type="number"
            name="volume_m3"
            placeholder="Volume(m³)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Pallets"
    >
        <Field
            type="number"
            name="pallets"
            placeholder="Pallets"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Pieces"
    >
        <Field
            type="number"
            name="pieces"
            placeholder="Pieces"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TemperatureMin(°C)"
    >
        <Field
            type="number"
            name="temperature_min_c"
            placeholder="TemperatureMin(°C)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TemperatureMax(°C)"
    >
        <Field
            type="number"
            name="temperature_max_c"
            placeholder="TemperatureMax(°C)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ADRRequired' labelFor='adr_required'>
        <Field
            name='adr_required'
            id='adr_required'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  
    <FormField
        label="CMRNumber"
    >
        <Field
            name="cmr_number"
            placeholder="CMRNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </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('/loads/loads-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditLoads
