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/tests/testsSlice'
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 EditTests = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    owner_user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    subject: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    grade_level: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    textbook: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    chapter: null,
    

    
    
    

    

    

    

    

    

    

    

    
    build_mode: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    
    target_question_count: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'target_total_points': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'target_average_difficulty': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    shuffle_questions: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    shuffle_answers: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    assigned_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    due_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    
    instructions: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { testsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="TestTitle"
    >
        <Field
            name="title"
            placeholder="TestTitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='GradeLevel' labelFor='grade_level'>
        <Field
            name='grade_level'
            id='grade_level'
            component={SelectField}
            options={initialValues.grade_level}
            itemRef={'grade_levels'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Textbook' labelFor='textbook'>
        <Field
            name='textbook'
            id='textbook'
            component={SelectField}
            options={initialValues.textbook}
            itemRef={'textbooks'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Chapter' labelFor='chapter'>
        <Field
            name='chapter'
            id='chapter'
            component={SelectField}
            options={initialValues.chapter}
            itemRef={'chapters'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BuildMode" labelFor="build_mode">
        <Field name="build_mode" id="build_mode" component="select">
          
            <option value="manual">manual</option>
          
            <option value="automatic">automatic</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="ready">ready</option>
          
            <option value="exported">exported</option>
          
            <option value="archived">archived</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TargetQuestionCount"
    >
        <Field
            type="number"
            name="target_question_count"
            placeholder="TargetQuestionCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TargetTotalPoints"
    >
        <Field
            type="number"
            name="target_total_points"
            placeholder="TargetTotalPoints"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TargetAverageDifficulty"
    >
        <Field
            type="number"
            name="target_average_difficulty"
            placeholder="TargetAverageDifficulty"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ShuffleQuestions' labelFor='shuffle_questions'>
        <Field
            name='shuffle_questions'
            id='shuffle_questions'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ShuffleAnswers' labelFor='shuffle_answers'>
        <Field
            name='shuffle_answers'
            id='shuffle_answers'
            component={SwitchField}
        ></Field>
    </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='Instructions' hasTextareaHeight>
        <Field
            name='instructions'
            id='instructions'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

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

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

export default EditTests
