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/medicine_dispatches/medicine_dispatchesSlice'
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 EditMedicine_dispatches = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    requested_by_rural_doctor: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    approved_by_admin: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    coordinated_by_city_doctor: null,
    

    
    
    

    

    

    

    

    
    requested_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    approved_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    dispatched_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    delivered_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    
    dispatch_note: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'destination_address': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'destination_latitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'destination_longitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tracking_reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { medicine_dispatchesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RequestedByRuralDoctor' labelFor='requested_by_rural_doctor'>
        <Field
            name='requested_by_rural_doctor'
            id='requested_by_rural_doctor'
            component={SelectField}
            options={initialValues.requested_by_rural_doctor}
            itemRef={'rural_doctors'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ApprovedByAdmin' labelFor='approved_by_admin'>
        <Field
            name='approved_by_admin'
            id='approved_by_admin'
            component={SelectField}
            options={initialValues.approved_by_admin}
            itemRef={'admins'}
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CoordinatedByCityDoctor' labelFor='coordinated_by_city_doctor'>
        <Field
            name='coordinated_by_city_doctor'
            id='coordinated_by_city_doctor'
            component={SelectField}
            options={initialValues.coordinated_by_city_doctor}
            itemRef={'city_doctors'}
      
        
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="requested">requested</option>
          
            <option value="approved">approved</option>
          
            <option value="packed">packed</option>
          
            <option value="dispatched">dispatched</option>
          
            <option value="in_transit">in_transit</option>
          
            <option value="delivered">delivered</option>
          
            <option value="cancelled">cancelled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="DispatchNote" hasTextareaHeight>
        <Field name="dispatch_note" as="textarea" placeholder="DispatchNote" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="DestinationAddress"
    >
        <Field
            name="destination_address"
            placeholder="DestinationAddress"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DestinationLatitude"
    >
        <Field
            type="number"
            name="destination_latitude"
            placeholder="DestinationLatitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DestinationLongitude"
    >
        <Field
            type="number"
            name="destination_longitude"
            placeholder="DestinationLongitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



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

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

export default EditMedicine_dispatches
