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/coffee_lots/coffee_lotsSlice'
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 EditCoffee_lots = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'lot_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    region: null,
    

    
    
    

    

    

    

    

    

    

    

    
    process_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    grade: '',
    

    

    

    

    
    
    

    

    

    
    crop_year: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'moisture_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    tasting_notes: '',
    

    

    

    

    

    

    

    

    

    

    
    
    
    'scaa_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    certifications: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'available_quantity_kg': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_available: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    images: [],
    

    

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

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

  const { coffee_lotsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="LotCode"
    >
        <Field
            name="lot_code"
            placeholder="LotCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Region' labelFor='region'>
        <Field
            name='region'
            id='region'
            component={SelectField}
            options={initialValues.region}
            itemRef={'coffee_regions'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'region_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ProcessType" labelFor="process_type">
        <Field name="process_type" id="process_type" component="select">
          
            <option value="washed">washed</option>
          
            <option value="natural">natural</option>
          
            <option value="honey">honey</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Grade" labelFor="grade">
        <Field name="grade" id="grade" component="select">
          
            <option value="grade_1">grade_1</option>
          
            <option value="grade_2">grade_2</option>
          
            <option value="grade_3">grade_3</option>
          
            <option value="grade_4">grade_4</option>
          
            <option value="grade_5">grade_5</option>
          
            <option value="commercial">commercial</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CropYear"
    >
        <Field
            type="number"
            name="crop_year"
            placeholder="CropYear"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MoisturePercent"
    >
        <Field
            type="number"
            name="moisture_percent"
            placeholder="MoisturePercent"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='TastingNotes' hasTextareaHeight>
        <Field
            name='tasting_notes'
            id='tasting_notes'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SCAAScore"
    >
        <Field
            type="number"
            name="scaa_score"
            placeholder="SCAAScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Certifications" hasTextareaHeight>
        <Field name="certifications" as="textarea" placeholder="Certifications" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AvailableQuantity(kg)"
    >
        <Field
            type="number"
            name="available_quantity_kg"
            placeholder="AvailableQuantity(kg)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsAvailable' labelFor='is_available'>
        <Field
            name='is_available'
            id='is_available'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='Images'
                color='info'
                icon={mdiUpload}
                path={'coffee_lots/images'}
                name='images'
                id='images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></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('/coffee_lots/coffee_lots-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCoffee_lots
