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/library_articles/library_articlesSlice'
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 EditLibrary_articlesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    category: '',
    

    

    

    

    
    
    
    'tags_csv': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    reading_time_minutes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    difficulty_level: '',
    

    

    

    

    
    
    

    

    

    

    

    
    published_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    updated_at_custom: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    
    content: '',
    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Category" labelFor="category">
        <Field name="category" id="category" component="select">
          
            <option value="nutrition_science">nutrition_science</option>
          
            <option value="fitness_exercise_science">fitness_exercise_science</option>
          
            <option value="mental_health">mental_health</option>
          
            <option value="sleep_science">sleep_science</option>
          
            <option value="hormones">hormones</option>
          
            <option value="longevity_anti_ageing">longevity_anti_ageing</option>
          
            <option value="specific_conditions">specific_conditions</option>
          
            <option value="medications">medications</option>
          
            <option value="lab_results_explained">lab_results_explained</option>
          
            <option value="supplements">supplements</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Tags"
    >
        <Field
            name="tags_csv"
            placeholder="Tags"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ReadingTime(minutes)"
    >
        <Field
            type="number"
            name="reading_time_minutes"
            placeholder="ReadingTime(minutes)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DifficultyLevel" labelFor="difficulty_level">
        <Field name="difficulty_level" id="difficulty_level" component="select">
          
            <option value="beginner">beginner</option>
          
            <option value="intermediate">intermediate</option>
          
            <option value="advanced">advanced</option>
          
        </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>
  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  
    <FormField label='Content' hasTextareaHeight>
        <Field
            name='content'
            id='content'
            component={RichTextField}
        ></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('/library_articles/library_articles-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditLibrary_articlesPage
