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";

import {hasPermission} from "../../helpers/userPermissions";



const EditMedicine_dispatches = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    prescription: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    from_organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    to_patient: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    to_organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    dispatched_by_doctor: null,
    

    
    
    

    

    

    

    

    
    dispatched_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    expected_delivery_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    delivered_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    dispatch_status: '',
    

    

    

    

    
    
    
    'tracking_reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'last_known_latitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'last_known_longitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    delivery_notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  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='Prescription' labelFor='prescription'>
        <Field
            name='prescription'
            id='prescription'
            component={SelectField}
            options={initialValues.prescription}
            itemRef={'prescriptions'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'notes'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='FromOrganization' labelFor='from_organization'>
        <Field
            name='from_organization'
            id='from_organization'
            component={SelectField}
            options={initialValues.from_organization}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ToPatient' labelFor='to_patient'>
        <Field
            name='to_patient'
            id='to_patient'
            component={SelectField}
            options={initialValues.to_patient}
            itemRef={'patient_profiles'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'patient_identifier'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ToOrganization' labelFor='to_organization'>
        <Field
            name='to_organization'
            id='to_organization'
            component={SelectField}
            options={initialValues.to_organization}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='DispatchedByDoctor' labelFor='dispatched_by_doctor'>
        <Field
            name='dispatched_by_doctor'
            id='dispatched_by_doctor'
            component={SelectField}
            options={initialValues.dispatched_by_doctor}
            itemRef={'doctor_profiles'}
      
        
      
        
      
        
      
        
      
        
            showField={'specialization'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </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="ExpectedDeliveryAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.expected_delivery_at ?
                  new Date(
                      dayjs(initialValues.expected_delivery_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'expected_delivery_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="DispatchStatus" labelFor="dispatch_status">
        <Field name="dispatch_status" id="dispatch_status" component="select">
          
            <option value="created">created</option>
          
            <option value="packed">packed</option>
          
            <option value="in_transit">in_transit</option>
          
            <option value="delivered">delivered</option>
          
            <option value="failed">failed</option>
          
            <option value="returned">returned</option>
          
            <option value="cancelled">cancelled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="TrackingReference"
    >
        <Field
            name="tracking_reference"
            placeholder="TrackingReference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LastKnownLatitude"
    >
        <Field
            type="number"
            name="last_known_latitude"
            placeholder="LastKnownLatitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LastKnownLongitude"
    >
        <Field
            type="number"
            name="last_known_longitude"
            placeholder="LastKnownLongitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="DeliveryNotes" hasTextareaHeight>
        <Field name="delivery_notes" as="textarea" placeholder="DeliveryNotes" />
    </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
