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/submission_grades/submission_gradesSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditSubmission_gradesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    submission: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    graded_by_teacher: null,
    

    
    
    

    

    

    

    

    
    graded_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'total_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'max_score_snapshot': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    grade_status: '',
    

    

    

    

    
    
    

    

    
    teacher_feedback: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    returned_to_student: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    returned_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    schools: null,
    

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

  const { submission_grades } = useAppSelector((state) => state.submission_grades)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Submission' labelFor='submission'>
        <Field
            name='submission'
            id='submission'
            component={SelectField}
            options={initialValues.submission}
            itemRef={'homework_submissions'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'submission_status'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='GradedByTeacher' labelFor='graded_by_teacher'>
        <Field
            name='graded_by_teacher'
            id='graded_by_teacher'
            component={SelectField}
            options={initialValues.graded_by_teacher}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalScore"
    >
        <Field
            type="number"
            name="total_score"
            placeholder="TotalScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaxScoreSnapshot"
    >
        <Field
            type="number"
            name="max_score_snapshot"
            placeholder="MaxScoreSnapshot"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="GradeStatus" labelFor="grade_status">
        <Field name="grade_status" id="grade_status" component="select">
          
            <option value="not_graded">not_graded</option>
          
            <option value="in_review">in_review</option>
          
            <option value="graded">graded</option>
          
            <option value="reopened">reopened</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='TeacherFeedback' hasTextareaHeight>
        <Field
            name='teacher_feedback'
            id='teacher_feedback'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ReturnedToStudent' labelFor='returned_to_student'>
        <Field
            name='returned_to_student'
            id='returned_to_student'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditSubmission_gradesPage
