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/supplements/supplementsSlice'
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 EditSupplementsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'supplement_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    form: '',
    

    

    

    

    
    
    
    'dose_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'dose_unit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'timing': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    with_food: '',
    

    

    

    

    
    
    
    'brand': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'cost_per_dose': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    reason: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    reminders_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    cycling_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    
    cycle_on_days: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    cycle_off_days: '',
    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Form" labelFor="form">
        <Field name="form" id="form" component="select">
          
            <option value="capsule">capsule</option>
          
            <option value="tablet">tablet</option>
          
            <option value="powder">powder</option>
          
            <option value="liquid">liquid</option>
          
            <option value="gummy">gummy</option>
          
            <option value="patch">patch</option>
          
            <option value="topical">topical</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DoseAmount"
    >
        <Field
            type="number"
            name="dose_amount"
            placeholder="DoseAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="DoseUnit"
    >
        <Field
            name="dose_unit"
            placeholder="DoseUnit"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Timing"
    >
        <Field
            name="timing"
            placeholder="Timing"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="WithFood" labelFor="with_food">
        <Field name="with_food" id="with_food" component="select">
          
            <option value="with_food">with_food</option>
          
            <option value="empty_stomach">empty_stomach</option>
          
            <option value="either">either</option>
          
            <option value="unknown">unknown</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Brand"
    >
        <Field
            name="brand"
            placeholder="Brand"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CostperDose"
    >
        <Field
            type="number"
            name="cost_per_dose"
            placeholder="CostperDose"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ReasonforTaking" hasTextareaHeight>
        <Field name="reason" as="textarea" placeholder="ReasonforTaking" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='RemindersEnabled' labelFor='reminders_enabled'>
        <Field
            name='reminders_enabled'
            id='reminders_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='CyclingEnabled' labelFor='cycling_enabled'>
        <Field
            name='cycling_enabled'
            id='cycling_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="CycleOnDays"
    >
        <Field
            type="number"
            name="cycle_on_days"
            placeholder="CycleOnDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CycleOffDays"
    >
        <Field
            type="number"
            name="cycle_off_days"
            placeholder="CycleOffDays"
        />
    </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('/supplements/supplements-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditSupplementsPage
