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/observation_events/observation_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 EditObservation_events = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    trial: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    trial_site: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    plot: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    plant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    observer: null,
    

    
    
    

    

    

    

    

    
    observed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    event_type: '',
    

    

    

    

    
    
    
    'summary': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    observations: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    media_assets: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { observation_eventsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Trial' labelFor='trial'>
        <Field
            name='trial'
            id='trial'
            component={SelectField}
            options={initialValues.trial}
            itemRef={'trials'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='TrialSite' labelFor='trial_site'>
        <Field
            name='trial_site'
            id='trial_site'
            component={SelectField}
            options={initialValues.trial_site}
            itemRef={'trial_sites'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Plot' labelFor='plot'>
        <Field
            name='plot'
            id='plot'
            component={SelectField}
            options={initialValues.plot}
            itemRef={'plots'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'plot_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Plant' labelFor='plant'>
        <Field
            name='plant'
            id='plant'
            component={SelectField}
            options={initialValues.plant}
            itemRef={'plants'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'plant_tag'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Observer' labelFor='observer'>
        <Field
            name='observer'
            id='observer'
            component={SelectField}
            options={initialValues.observer}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EventType" labelFor="event_type">
        <Field name="event_type" id="event_type" component="select">
          
            <option value="scouting">scouting</option>
          
            <option value="measurement">measurement</option>
          
            <option value="application">application</option>
          
            <option value="harvest">harvest</option>
          
            <option value="issue">issue</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Summary"
    >
        <Field
            name="summary"
            placeholder="Summary"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

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

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Observations' labelFor='observations'>
          <Field
              name='observations'
              id='observations'
              component={SelectFieldMany}
              options={initialValues.observations}
              itemRef={'observations'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'value_text'}
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='MediaAssets' labelFor='media_assets'>
          <Field
              name='media_assets'
              id='media_assets'
              component={SelectFieldMany}
              options={initialValues.media_assets}
              itemRef={'media_assets'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'caption'}
            
            
            
            
            
            
            
            
          ></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('/observation_events/observation_events-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditObservation_events
