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/field_trip_catalog/field_trip_catalogSlice'
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 EditField_trip_catalogPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    category: '',
    

    

    

    

    
    
    
    'location_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'grade_range': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'typical_min_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'typical_max_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    separate_payment_required: false,
    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="TripName"
    >
        <Field
            name="name"
            placeholder="TripName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Category" labelFor="category">
        <Field name="category" id="category" component="select">
          
            <option value="educational_environmental">educational_environmental</option>
          
            <option value="museums_culture">museums_culture</option>
          
            <option value="stem_learning">stem_learning</option>
          
            <option value="fun_recreation">fun_recreation</option>
          
            <option value="movies">movies</option>
          
            <option value="zoo">zoo</option>
          
            <option value="premium">premium</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="GradeRange"
    >
        <Field
            name="grade_range"
            placeholder="GradeRange"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TypicalMinRate"
    >
        <Field
            type="number"
            name="typical_min_rate"
            placeholder="TypicalMinRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TypicalMaxRate"
    >
        <Field
            type="number"
            name="typical_max_rate"
            placeholder="TypicalMaxRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SeparatePaymentRequired' labelFor='separate_payment_required'>
        <Field
            name='separate_payment_required'
            id='separate_payment_required'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </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('/field_trip_catalog/field_trip_catalog-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditField_trip_catalogPage
