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/skill_modules/skill_modulesSlice'
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 EditSkill_modulesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'module_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    module_type: '',
    

    

    

    

    
    
    

    

    
    module_description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    evidence_level: '',
    

    

    

    

    
    
    

    
    method_steps: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    requires_citations: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="ModuleName"
    >
        <Field
            name="module_name"
            placeholder="ModuleName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ModuleType" labelFor="module_type">
        <Field name="module_type" id="module_type" component="select">
          
            <option value="thinking_method">thinking_method</option>
          
            <option value="compliance">compliance</option>
          
            <option value="analysis">analysis</option>
          
            <option value="generation">generation</option>
          
            <option value="research">research</option>
          
            <option value="optimization">optimization</option>
          
            <option value="simulation">simulation</option>
          
            <option value="creative">creative</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='ModuleDescription' hasTextareaHeight>
        <Field
            name='module_description'
            id='module_description'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EvidenceLevel" labelFor="evidence_level">
        <Field name="evidence_level" id="evidence_level" component="select">
          
            <option value="high">high</option>
          
            <option value="medium">medium</option>
          
            <option value="low">low</option>
          
            <option value="unknown">unknown</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="MethodSteps" hasTextareaHeight>
        <Field name="method_steps" as="textarea" placeholder="MethodSteps" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='RequiresCitations' labelFor='requires_citations'>
        <Field
            name='requires_citations'
            id='requires_citations'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsActive' labelFor='is_active'>
        <Field
            name='is_active'
            id='is_active'
            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('/skill_modules/skill_modules-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditSkill_modulesPage
