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/test_versions/test_versionsSlice'
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 EditTest_versions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    test: null,
    

    
    
    
    'version_label': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    generation_strategy: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_locked: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    generated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    export_pdf: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    export_docx: [],
    

    

    

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

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

  const { test_versionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Test' labelFor='test'>
        <Field
            name='test'
            id='test'
            component={SelectField}
            options={initialValues.test}
            itemRef={'tests'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="VersionLabel"
    >
        <Field
            name="version_label"
            placeholder="VersionLabel"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="GenerationStrategy" labelFor="generation_strategy">
        <Field name="generation_strategy" id="generation_strategy" component="select">
          
            <option value="shuffle_questions">shuffle_questions</option>
          
            <option value="shuffle_answers">shuffle_answers</option>
          
            <option value="both">both</option>
          
            <option value="custom">custom</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsLocked' labelFor='is_locked'>
        <Field
            name='is_locked'
            id='is_locked'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='ExportPDF'
            color='info'
            icon={mdiUpload}
            path={'test_versions/export_pdf'}
            name='export_pdf'
            id='export_pdf'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='ExportDOCX'
            color='info'
            icon={mdiUpload}
            path={'test_versions/export_docx'}
            name='export_docx'
            id='export_docx'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></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('/test_versions/test_versions-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditTest_versions
