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/attendance_records/attendance_recordsSlice'
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 EditAttendance_records = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    attendance_session: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    student: null,
    

    
    
    

    

    

    

    

    

    

    

    
    attendance_status: '',
    

    

    

    

    
    
    

    

    

    
    minutes_late: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    comment: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { attendance_recordsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AttendanceSession' labelFor='attendance_session'>
        <Field
            name='attendance_session'
            id='attendance_session'
            component={SelectField}
            options={initialValues.attendance_session}
            itemRef={'attendance_sessions'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'status'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Student' labelFor='student'>
        <Field
            name='student'
            id='student'
            component={SelectField}
            options={initialValues.student}
            itemRef={'students'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'student_number'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AttendanceStatus" labelFor="attendance_status">
        <Field name="attendance_status" id="attendance_status" component="select">
          
            <option value="present">present</option>
          
            <option value="absent">absent</option>
          
            <option value="late">late</option>
          
            <option value="excused">excused</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MinutesLate"
    >
        <Field
            type="number"
            name="minutes_late"
            placeholder="MinutesLate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditAttendance_records
