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/admission_applications/admission_applicationsSlice'
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 EditAdmission_applicationsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    
    entry_type: '',
    

    

    

    

    
    
    
    'applicant_full_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    gender: '',
    

    

    

    

    
    
    

    

    

    

    

    
    date_of_birth: new Date(),
    

    

    

    

    

    

    

    
    
    
    'previous_school': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    requested_grade: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    requested_stream: null,
    

    
    
    
    'guardian_full_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'guardian_phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'guardian_email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    address_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    documents: [],
    

    

    

    
    
    

    

    

    

    

    
    submitted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  
    <FormField label="EntryType" labelFor="entry_type">
        <Field name="entry_type" id="entry_type" component="select">
          
            <option value="grade_9_entry">grade_9_entry</option>
          
            <option value="transfer">transfer</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="ApplicantFullName"
    >
        <Field
            name="applicant_full_name"
            placeholder="ApplicantFullName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Gender" labelFor="gender">
        <Field name="gender" id="gender" component="select">
          
            <option value="female">female</option>
          
            <option value="male">male</option>
          
        </Field>
    </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="PreviousSchool"
    >
        <Field
            name="previous_school"
            placeholder="PreviousSchool"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RequestedGrade' labelFor='requested_grade'>
        <Field
            name='requested_grade'
            id='requested_grade'
            component={SelectField}
            options={initialValues.requested_grade}
            itemRef={'grades'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'label'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RequestedStream' labelFor='requested_stream'>
        <Field
            name='requested_stream'
            id='requested_stream'
            component={SelectField}
            options={initialValues.requested_stream}
            itemRef={'streams'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name_en'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="GuardianFullName"
    >
        <Field
            name="guardian_full_name"
            placeholder="GuardianFullName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="GuardianPhone"
    >
        <Field
            name="guardian_phone"
            placeholder="GuardianPhone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="GuardianEmail"
    >
        <Field
            name="guardian_email"
            placeholder="GuardianEmail"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Address" hasTextareaHeight>
        <Field name="address_text" as="textarea" placeholder="Address" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='Documents'
            color='info'
            icon={mdiUpload}
            path={'admission_applications/documents'}
            name='documents'
            id='documents'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="submitted">submitted</option>
          
            <option value="under_review">under_review</option>
          
            <option value="accepted">accepted</option>
          
            <option value="rejected">rejected</option>
          
            <option value="waitlisted">waitlisted</option>
          
        </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('/admission_applications/admission_applications-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAdmission_applicationsPage
