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/class_schedules/class_schedulesSlice'
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 EditClass_schedules = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    class_section: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    subject: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    teacher: null,
    

    
    
    

    

    

    

    

    

    

    

    
    weekday: '',
    

    

    

    

    
    
    
    'period_label': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    starts_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    ends_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'room': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { class_schedulesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ClassSection' labelFor='class_section'>
        <Field
            name='class_section'
            id='class_section'
            component={SelectField}
            options={initialValues.class_section}
            itemRef={'class_sections'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Subject' labelFor='subject'>
        <Field
            name='subject'
            id='subject'
            component={SelectField}
            options={initialValues.subject}
            itemRef={'subjects'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name_en'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Teacher' labelFor='teacher'>
        <Field
            name='teacher'
            id='teacher'
            component={SelectField}
            options={initialValues.teacher}
            itemRef={'staff_members'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Weekday" labelFor="weekday">
        <Field name="weekday" id="weekday" component="select">
          
            <option value="monday">monday</option>
          
            <option value="tuesday">tuesday</option>
          
            <option value="wednesday">wednesday</option>
          
            <option value="thursday">thursday</option>
          
            <option value="friday">friday</option>
          
            <option value="saturday">saturday</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="PeriodLabel"
    >
        <Field
            name="period_label"
            placeholder="PeriodLabel"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="Room"
    >
        <Field
            name="room"
            placeholder="Room"
        />
    </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('/class_schedules/class_schedules-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditClass_schedules
