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/campers/campersSlice'
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 EditCampersPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'first_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'last_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    date_of_birth: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    grade_level: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    gender: '',
    

    

    

    

    
    
    

    
    allergies: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    medical_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    special_needs_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    primary_guardian: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    
    profile_photo: [],
    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="FirstName"
    >
        <Field
            name="first_name"
            placeholder="FirstName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="LastName"
    >
        <Field
            name="last_name"
            placeholder="LastName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="GradeLevel"
    >
        <Field
            type="number"
            name="grade_level"
            placeholder="GradeLevel"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Gender" labelFor="gender">
        <Field name="gender" id="gender" component="select">
          
            <option value="female">female</option>
          
            <option value="male">male</option>
          
            <option value="nonbinary">nonbinary</option>
          
            <option value="prefer_not_to_say">prefer_not_to_say</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Allergies" hasTextareaHeight>
        <Field name="allergies" as="textarea" placeholder="Allergies" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="MedicalNotes" hasTextareaHeight>
        <Field name="medical_notes" as="textarea" placeholder="MedicalNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="SpecialNeedsNotes" hasTextareaHeight>
        <Field name="special_needs_notes" as="textarea" placeholder="SpecialNeedsNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PrimaryGuardian' labelFor='primary_guardian'>
        <Field
            name='primary_guardian'
            id='primary_guardian'
            component={SelectField}
            options={initialValues.primary_guardian}
            itemRef={'guardians'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'primary_phone'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='ProfilePhoto'
                color='info'
                icon={mdiUpload}
                path={'campers/profile_photo'}
                name='profile_photo'
                id='profile_photo'
                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('/campers/campers-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCampersPage
