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/menstrual_day_logs/menstrual_day_logsSlice'
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 EditMenstrual_day_logs = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    cycle: null,
    

    
    
    

    

    

    

    

    
    log_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    flow_intensity: '',
    

    

    

    

    
    
    

    

    

    
    cramping: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    bloating: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    acne: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    breast_tenderness: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    headache: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    fatigue: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    food_cravings: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    lower_back_pain: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    clotting: false,
    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { menstrual_day_logsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Cycle' labelFor='cycle'>
        <Field
            name='cycle'
            id='cycle'
            component={SelectField}
            options={initialValues.cycle}
            itemRef={'menstrual_cycles'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'notes'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="FlowIntensity" labelFor="flow_intensity">
        <Field name="flow_intensity" id="flow_intensity" component="select">
          
            <option value="spotting">spotting</option>
          
            <option value="light">light</option>
          
            <option value="medium">medium</option>
          
            <option value="heavy">heavy</option>
          
            <option value="very_heavy">very_heavy</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Cramping(1-10)"
    >
        <Field
            type="number"
            name="cramping"
            placeholder="Cramping(1-10)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Bloating(1-5)"
    >
        <Field
            type="number"
            name="bloating"
            placeholder="Bloating(1-5)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Acne' labelFor='acne'>
        <Field
            name='acne'
            id='acne'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='BreastTenderness' labelFor='breast_tenderness'>
        <Field
            name='breast_tenderness'
            id='breast_tenderness'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Headache' labelFor='headache'>
        <Field
            name='headache'
            id='headache'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Fatigue' labelFor='fatigue'>
        <Field
            name='fatigue'
            id='fatigue'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='FoodCravings' labelFor='food_cravings'>
        <Field
            name='food_cravings'
            id='food_cravings'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='LowerBackPain' labelFor='lower_back_pain'>
        <Field
            name='lower_back_pain'
            id='lower_back_pain'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Clotting' labelFor='clotting'>
        <Field
            name='clotting'
            id='clotting'
            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('/menstrual_day_logs/menstrual_day_logs-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMenstrual_day_logs
