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/grade_items/grade_itemsSlice'
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 EditGrade_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    course_section: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    teacher: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    category: '',
    

    

    

    

    
    
    
    'weight': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    max_points: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    assigned_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    due_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    published: false,
    

    

    

    

    

    

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

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

  const { grade_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CourseSection' labelFor='course_section'>
        <Field
            name='course_section'
            id='course_section'
            component={SelectField}
            options={initialValues.course_section}
            itemRef={'course_sections'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="GradeItemName"
    >
        <Field
            name="name"
            placeholder="GradeItemName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Category" labelFor="category">
        <Field name="category" id="category" component="select">
          
            <option value="homework">homework</option>
          
            <option value="quiz">quiz</option>
          
            <option value="test">test</option>
          
            <option value="project">project</option>
          
            <option value="participation">participation</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Weight"
    >
        <Field
            type="number"
            name="weight"
            placeholder="Weight"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaxPoints"
    >
        <Field
            type="number"
            name="max_points"
            placeholder="MaxPoints"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Published' labelFor='published'>
        <Field
            name='published'
            id='published'
            component={SwitchField}
        ></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('/grade_items/grade_items-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditGrade_items
