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/nutrition_targets/nutrition_targetsSlice'
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 EditNutrition_targets = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    
    effective_from: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    effective_to: new Date(),
    

    

    

    

    

    

    

    
    
    
    'bmr': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tdee': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    goal_mode: '',
    

    

    

    

    
    
    

    

    

    
    calorie_target: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'protein_target_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'carb_target_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'fat_target_g': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'macro_preset_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { nutrition_targetsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit nutrition_targets')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit nutrition_targets'} 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="EffectiveFrom"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.effective_from ?
                  new Date(
                      dayjs(initialValues.effective_from).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'effective_from': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BMR"
    >
        <Field
            type="number"
            name="bmr"
            placeholder="BMR"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TDEE"
    >
        <Field
            type="number"
            name="tdee"
            placeholder="TDEE"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="GoalMode" labelFor="goal_mode">
        <Field name="goal_mode" id="goal_mode" component="select">
          
            <option value="deficit">deficit</option>
          
            <option value="maintenance">maintenance</option>
          
            <option value="surplus">surplus</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CalorieTarget"
    >
        <Field
            type="number"
            name="calorie_target"
            placeholder="CalorieTarget"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ProteinTarget(g)"
    >
        <Field
            type="number"
            name="protein_target_g"
            placeholder="ProteinTarget(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CarbTarget(g)"
    >
        <Field
            type="number"
            name="carb_target_g"
            placeholder="CarbTarget(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="FatTarget(g)"
    >
        <Field
            type="number"
            name="fat_target_g"
            placeholder="FatTarget(g)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="MacroPresetName"
    >
        <Field
            name="macro_preset_name"
            placeholder="MacroPresetName"
        />
    </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('/nutrition_targets/nutrition_targets-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditNutrition_targets
