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/subject_offerings/subject_offeringsSlice'
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 EditSubject_offerings = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    subject: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    grade: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    stream: null,
    

    
    
    

    

    
    learning_objectives_om: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    learning_objectives_am: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    learning_objectives_en: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    
    topics_outline: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    responsible_teacher: null,
    

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

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

  const { subject_offeringsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  
    <FormField label='LearningObjectivesAfaanOromoo' hasTextareaHeight>
        <Field
            name='learning_objectives_om'
            id='learning_objectives_om'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='LearningObjectivesAmharic' hasTextareaHeight>
        <Field
            name='learning_objectives_am'
            id='learning_objectives_am'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='LearningObjectivesEnglish' hasTextareaHeight>
        <Field
            name='learning_objectives_en'
            id='learning_objectives_en'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="TopicsOutline" hasTextareaHeight>
        <Field name="topics_outline" as="textarea" placeholder="TopicsOutline" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ResponsibleTeacher' labelFor='responsible_teacher'>
        <Field
            name='responsible_teacher'
            id='responsible_teacher'
            component={SelectField}
            options={initialValues.responsible_teacher}
            itemRef={'staff_members'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/subject_offerings/subject_offerings-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditSubject_offerings
