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/learning_modules/learning_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 EditLearning_modulesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    difficulty: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    topic: '',
    

    

    

    

    
    
    

    
    summary: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    content: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    estimated_minutes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    published: false,
    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Difficulty" labelFor="difficulty">
        <Field name="difficulty" id="difficulty" component="select">
          
            <option value="beginner">beginner</option>
          
            <option value="intermediate">intermediate</option>
          
            <option value="advanced">advanced</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Topic" labelFor="topic">
        <Field name="topic" id="topic" component="select">
          
            <option value="basics">basics</option>
          
            <option value="risk_management">risk_management</option>
          
            <option value="technical_analysis">technical_analysis</option>
          
            <option value="fundamentals">fundamentals</option>
          
            <option value="options_awareness">options_awareness</option>
          
            <option value="macro">macro</option>
          
            <option value="portfolio_building">portfolio_building</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Summary" hasTextareaHeight>
        <Field name="summary" as="textarea" placeholder="Summary" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Content' hasTextareaHeight>
        <Field
            name='content'
            id='content'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="EstimatedMinutes"
    >
        <Field
            type="number"
            name="estimated_minutes"
            placeholder="EstimatedMinutes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

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

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

export default EditLearning_modulesPage
