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/chain_of_custody_events/chain_of_custody_eventsSlice'
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 EditChain_of_custody_eventsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    sample: null,
    

    
    
    

    

    

    

    

    
    event_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    event_type: '',
    

    

    

    

    
    
    
    'from_party': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'to_party': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'location': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'shipping_reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    recorded_by: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof chain_of_custody_events === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (chain_of_custody_events)[el])
          setInitialValues(newInitialVal);
      }
  }, [chain_of_custody_events])

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Sample' labelFor='sample'>
        <Field
            name='sample'
            id='sample'
            component={SelectField}
            options={initialValues.sample}
            itemRef={'samples'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'sample_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EventType" labelFor="event_type">
        <Field name="event_type" id="event_type" component="select">
          
            <option value="collected">collected</option>
          
            <option value="sealed">sealed</option>
          
            <option value="shipped">shipped</option>
          
            <option value="received">received</option>
          
            <option value="prepared">prepared</option>
          
            <option value="subsampled">subsampled</option>
          
            <option value="analyzed">analyzed</option>
          
            <option value="stored">stored</option>
          
            <option value="disposed">disposed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="FromParty"
    >
        <Field
            name="from_party"
            placeholder="FromParty"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ToParty"
    >
        <Field
            name="to_party"
            placeholder="ToParty"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ShippingReference"
    >
        <Field
            name="shipping_reference"
            placeholder="ShippingReference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Notes' hasTextareaHeight>
        <Field
            name='notes'
            id='notes'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RecordedBy' labelFor='recorded_by'>
        <Field
            name='recorded_by'
            id='recorded_by'
            component={SelectField}
            options={initialValues.recorded_by}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/chain_of_custody_events/chain_of_custody_events-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditChain_of_custody_eventsPage
