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/foods/foodsSlice'
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 EditFoodsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'food_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'brand': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'barcode': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'serving_size_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    calories: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'protein_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'carbs_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'sugars_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'fat_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'saturated_fat_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'fibre_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'sodium_mg': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    micronutrients_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    fodmap_tag: '',
    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="FoodName"
    >
        <Field
            name="food_name"
            placeholder="FoodName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Barcode"
    >
        <Field
            name="barcode"
            placeholder="Barcode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ServingSize(g)"
    >
        <Field
            type="number"
            name="serving_size_g"
            placeholder="ServingSize(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Calories"
    >
        <Field
            type="number"
            name="calories"
            placeholder="Calories"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Protein(g)"
    >
        <Field
            type="number"
            name="protein_g"
            placeholder="Protein(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Carbohydrates(g)"
    >
        <Field
            type="number"
            name="carbs_g"
            placeholder="Carbohydrates(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Sugars(g)"
    >
        <Field
            type="number"
            name="sugars_g"
            placeholder="Sugars(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Fats(g)"
    >
        <Field
            type="number"
            name="fat_g"
            placeholder="Fats(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SaturatedFats(g)"
    >
        <Field
            type="number"
            name="saturated_fat_g"
            placeholder="SaturatedFats(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Fibre(g)"
    >
        <Field
            type="number"
            name="fibre_g"
            placeholder="Fibre(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Sodium(mg)"
    >
        <Field
            type="number"
            name="sodium_mg"
            placeholder="Sodium(mg)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="MicronutrientsJSON" hasTextareaHeight>
        <Field name="micronutrients_json" as="textarea" placeholder="MicronutrientsJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="FODMAPTag" labelFor="fodmap_tag">
        <Field name="fodmap_tag" id="fodmap_tag" component="select">
          
            <option value="unknown">unknown</option>
          
            <option value="low_fodmap">low_fodmap</option>
          
            <option value="high_fodmap">high_fodmap</option>
          
        </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('/foods/foods-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditFoodsPage
