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/goals/goalsSlice'
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 EditGoals = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    goal_type: '',
    

    

    

    

    
    
    

    
    follow_up_answer: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    target_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

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

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

  const { goalsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit goals')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit goals'} 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="GoalType" labelFor="goal_type">
        <Field name="goal_type" id="goal_type" component="select">
          
            <option value="lose_body_fat">lose_body_fat</option>
          
            <option value="build_muscle_mass">build_muscle_mass</option>
          
            <option value="improve_cardiovascular_fitness">improve_cardiovascular_fitness</option>
          
            <option value="increase_flexibility_mobility">increase_flexibility_mobility</option>
          
            <option value="improve_sleep_quality">improve_sleep_quality</option>
          
            <option value="reduce_stress_anxiety">reduce_stress_anxiety</option>
          
            <option value="manage_chronic_condition_better">manage_chronic_condition_better</option>
          
            <option value="improve_mental_health">improve_mental_health</option>
          
            <option value="optimise_nutrition">optimise_nutrition</option>
          
            <option value="improve_gut_health">improve_gut_health</option>
          
            <option value="improve_hormonal_balance">improve_hormonal_balance</option>
          
            <option value="increase_energy_levels">increase_energy_levels</option>
          
            <option value="longevity_anti_ageing">longevity_anti_ageing</option>
          
            <option value="athletic_performance">athletic_performance</option>
          
            <option value="post_surgery_recovery">post_surgery_recovery</option>
          
            <option value="pregnancy_preparation">pregnancy_preparation</option>
          
            <option value="postpartum_recovery">postpartum_recovery</option>
          
            <option value="improve_sexual_health">improve_sexual_health</option>
          
            <option value="improve_fertility">improve_fertility</option>
          
            <option value="quit_smoking_or_alcohol">quit_smoking_or_alcohol</option>
          
            <option value="build_better_daily_habits">build_better_daily_habits</option>
          
            <option value="improve_cognitive_performance">improve_cognitive_performance</option>
          
            <option value="support_family_member_health">support_family_member_health</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="FollowUpAnswer" hasTextareaHeight>
        <Field name="follow_up_answer" as="textarea" placeholder="FollowUpAnswer" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Active' labelFor='is_active'>
        <Field
            name='is_active'
            id='is_active'
            component={SwitchField}
        ></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('/goals/goals-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditGoals
