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/health_metrics/health_metricsSlice'
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 EditHealth_metricsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    
    measured_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    metric_type: '',
    

    

    

    

    
    
    
    'value_primary': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'value_secondary': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'unit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    panel_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'device_used': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_fasting: false,
    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="MetricType" labelFor="metric_type">
        <Field name="metric_type" id="metric_type" component="select">
          
            <option value="blood_pressure">blood_pressure</option>
          
            <option value="resting_heart_rate">resting_heart_rate</option>
          
            <option value="spo2">spo2</option>
          
            <option value="body_temperature">body_temperature</option>
          
            <option value="blood_glucose">blood_glucose</option>
          
            <option value="hba1c">hba1c</option>
          
            <option value="cholesterol_panel">cholesterol_panel</option>
          
            <option value="full_blood_count">full_blood_count</option>
          
            <option value="liver_function">liver_function</option>
          
            <option value="kidney_function">kidney_function</option>
          
            <option value="thyroid_panel">thyroid_panel</option>
          
            <option value="iron_studies">iron_studies</option>
          
            <option value="vitamin_levels">vitamin_levels</option>
          
            <option value="mineral_levels">mineral_levels</option>
          
            <option value="hormones">hormones</option>
          
            <option value="inflammation_markers">inflammation_markers</option>
          
            <option value="metabolic_panel">metabolic_panel</option>
          
            <option value="cardiac_markers">cardiac_markers</option>
          
            <option value="urine_analysis">urine_analysis</option>
          
            <option value="hrv">hrv</option>
          
            <option value="body_composition">body_composition</option>
          
            <option value="waist_circumference">waist_circumference</option>
          
            <option value="hip_circumference">hip_circumference</option>
          
            <option value="neck_circumference">neck_circumference</option>
          
            <option value="body_measurements">body_measurements</option>
          
            <option value="dexa_scan">dexa_scan</option>
          
            <option value="lung_function">lung_function</option>
          
            <option value="skin_health_marker">skin_health_marker</option>
          
            <option value="intraocular_pressure">intraocular_pressure</option>
          
            <option value="resting_metabolic_rate">resting_metabolic_rate</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PrimaryValue"
    >
        <Field
            type="number"
            name="value_primary"
            placeholder="PrimaryValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SecondaryValue"
    >
        <Field
            type="number"
            name="value_secondary"
            placeholder="SecondaryValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Unit"
    >
        <Field
            name="unit"
            placeholder="Unit"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="PanelJSON" hasTextareaHeight>
        <Field name="panel_json" as="textarea" placeholder="PanelJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="DeviceUsed"
    >
        <Field
            name="device_used"
            placeholder="DeviceUsed"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Fasting' labelFor='is_fasting'>
        <Field
            name='is_fasting'
            id='is_fasting'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

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

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

export default EditHealth_metricsPage
