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/supplement_evidence_items/supplement_evidence_itemsSlice'
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 EditSupplement_evidence_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'supplement_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    evidence_rating: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    summary: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    mechanism: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    benefits: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'dosing_range': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    timing_recommendations: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    interactions: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    side_effects: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    avoid_who: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { supplement_evidence_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="SupplementName"
    >
        <Field
            name="supplement_name"
            placeholder="SupplementName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="EvidenceRating(1-5)"
    >
        <Field
            type="number"
            name="evidence_rating"
            placeholder="EvidenceRating(1-5)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

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

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="MechanismofAction" hasTextareaHeight>
        <Field name="mechanism" as="textarea" placeholder="MechanismofAction" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Benefits" hasTextareaHeight>
        <Field name="benefits" as="textarea" placeholder="Benefits" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="StandardDosingRange"
    >
        <Field
            name="dosing_range"
            placeholder="StandardDosingRange"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="TimingRecommendations" hasTextareaHeight>
        <Field name="timing_recommendations" as="textarea" placeholder="TimingRecommendations" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="KnownInteractions" hasTextareaHeight>
        <Field name="interactions" as="textarea" placeholder="KnownInteractions" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="SideEffects" hasTextareaHeight>
        <Field name="side_effects" as="textarea" placeholder="SideEffects" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="WhoShouldAvoid" hasTextareaHeight>
        <Field name="avoid_who" as="textarea" placeholder="WhoShouldAvoid" />
    </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('/supplement_evidence_items/supplement_evidence_items-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditSupplement_evidence_items
