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/quizzes/quizzesSlice'
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 EditQuizzesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    quiz_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    owner: null,
    

    
    
    

    

    

    

    

    

    

    

    
    visibility: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_verified: false,
    

    

    

    

    

    

    
    
    
    'verified_badge_text': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    subject: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    age_group: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    difficulty: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    cover_images: [],
    

    

    
    
    

    

    

    

    

    

    
    allow_images_in_questions: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    published_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Description' hasTextareaHeight>
        <Field
            name='description'
            id='description'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="QuizType" labelFor="quiz_type">
        <Field name="quiz_type" id="quiz_type" component="select">
          
            <option value="official">official</option>
          
            <option value="community">community</option>
          
            <option value="personal">personal</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Visibility" labelFor="visibility">
        <Field name="visibility" id="visibility" component="select">
          
            <option value="private">private</option>
          
            <option value="unlisted">unlisted</option>
          
            <option value="public">public</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsVerified' labelFor='is_verified'>
        <Field
            name='is_verified'
            id='is_verified'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  
    <FormField
        label="VerifiedBadgeText"
    >
        <Field
            name="verified_badge_text"
            placeholder="VerifiedBadgeText"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Subject" labelFor="subject">
        <Field name="subject" id="subject" component="select">
          
            <option value="mathematics">mathematics</option>
          
            <option value="science">science</option>
          
            <option value="history">history</option>
          
            <option value="geography">geography</option>
          
            <option value="technology">technology</option>
          
            <option value="animals">animals</option>
          
            <option value="space">space</option>
          
            <option value="gaming">gaming</option>
          
            <option value="movies">movies</option>
          
            <option value="music">music</option>
          
            <option value="languages">languages</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AgeGroup" labelFor="age_group">
        <Field name="age_group" id="age_group" component="select">
          
            <option value="early">early</option>
          
            <option value="kids">kids</option>
          
            <option value="teens">teens</option>
          
            <option value="adult">adult</option>
          
            <option value="mixed">mixed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Difficulty" labelFor="difficulty">
        <Field name="difficulty" id="difficulty" component="select">
          
            <option value="easy">easy</option>
          
            <option value="medium">medium</option>
          
            <option value="hard">hard</option>
          
            <option value="mixed">mixed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='CoverImages'
                color='info'
                icon={mdiUpload}
                path={'quizzes/cover_images'}
                name='cover_images'
                id='cover_images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AllowImagesInQuestions' labelFor='allow_images_in_questions'>
        <Field
            name='allow_images_in_questions'
            id='allow_images_in_questions'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="PublishedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.published_at ?
                  new Date(
                      dayjs(initialValues.published_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'published_at': date})}
          />
      </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('/quizzes/quizzes-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditQuizzesPage
