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/sleep_logs/sleep_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 EditSleep_logsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    
    bedtime: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    sleep_onset_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    awakenings_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    final_wake_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    out_of_bed_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    total_sleep_minutes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sleep_quality: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    dream_recall: false,
    

    

    

    

    

    

    
    
    

    
    dream_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    morning_energy: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    sleep_aids_used: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    disturbances: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AwakeningsCount"
    >
        <Field
            type="number"
            name="awakenings_count"
            placeholder="AwakeningsCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalSleepTime(minutes)"
    >
        <Field
            type="number"
            name="total_sleep_minutes"
            placeholder="TotalSleepTime(minutes)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='DreamRecall' labelFor='dream_recall'>
        <Field
            name='dream_recall'
            id='dream_recall'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="DreamNotes" hasTextareaHeight>
        <Field name="dream_notes" as="textarea" placeholder="DreamNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="SleepAidsUsed" hasTextareaHeight>
        <Field name="sleep_aids_used" as="textarea" placeholder="SleepAidsUsed" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditSleep_logsPage
