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/incidents/incidentsSlice'
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 EditIncidents = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    company: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    employee: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    driver_profile: null,
    

    
    
    

    

    

    

    

    

    

    

    
    type: '',
    

    

    

    

    
    
    

    

    

    

    

    
    occurred_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'currency': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'location': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    paid: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    paid_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    points: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'severity': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'reported_by': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { incidentsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit incidents')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit incidents'} 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='Employee' labelFor='employee'>
        <Field
            name='employee'
            id='employee'
            component={SelectField}
            options={initialValues.employee}
            itemRef={'employees'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'last_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <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="IncidentType" labelFor="type">
        <Field name="type" id="type" component="select">
          
            <option value="FINE">FINE</option>
          
            <option value="ACCIDENT">ACCIDENT</option>
          
            <option value="TACHO_VIOLATION">TACHO_VIOLATION</option>
          
            <option value="DISCIPLINARY">DISCIPLINARY</option>
          
            <option value="VEHICLE_DAMAGE">VEHICLE_DAMAGE</option>
          
            <option value="OTHER">OTHER</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Amount"
    >
        <Field
            type="number"
            name="amount"
            placeholder="Amount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Currency"
    >
        <Field
            name="currency"
            placeholder="Currency"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

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

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Location"
    >
        <Field
            name="location"
            placeholder="Location"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Paid' labelFor='paid'>
        <Field
            name='paid'
            id='paid'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Points"
    >
        <Field
            type="number"
            name="points"
            placeholder="Points"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Severity"
    >
        <Field
            name="severity"
            placeholder="Severity"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ReportedBy"
    >
        <Field
            name="reported_by"
            placeholder="ReportedBy"
        />
    </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('/incidents/incidents-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditIncidents
