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/mood_entries/mood_entriesSlice'
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 EditMood_entriesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    trade: null,
    

    
    
    

    

    

    

    

    

    

    

    
    mood: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    confidence_level: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    discipline_level: '',
    

    

    

    

    
    
    
    'stress_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    emotions_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    recorded_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Trade' labelFor='trade'>
        <Field
            name='trade'
            id='trade'
            component={SelectField}
            options={initialValues.trade}
            itemRef={'trades'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'notes'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Mood" labelFor="mood">
        <Field name="mood" id="mood" component="select">
          
            <option value="confident">confident</option>
          
            <option value="calm">calm</option>
          
            <option value="focused">focused</option>
          
            <option value="neutral">neutral</option>
          
            <option value="anxious">anxious</option>
          
            <option value="fearful">fearful</option>
          
            <option value="frustrated">frustrated</option>
          
            <option value="angry">angry</option>
          
            <option value="revengeful">revengeful</option>
          
            <option value="euphoric">euphoric</option>
          
            <option value="tired">tired</option>
          
            <option value="stressed">stressed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ConfidenceLevel" labelFor="confidence_level">
        <Field name="confidence_level" id="confidence_level" component="select">
          
            <option value="very_low">very_low</option>
          
            <option value="low">low</option>
          
            <option value="medium">medium</option>
          
            <option value="high">high</option>
          
            <option value="very_high">very_high</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DisciplineLevel" labelFor="discipline_level">
        <Field name="discipline_level" id="discipline_level" component="select">
          
            <option value="very_low">very_low</option>
          
            <option value="low">low</option>
          
            <option value="medium">medium</option>
          
            <option value="high">high</option>
          
            <option value="very_high">very_high</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="StressScore"
    >
        <Field
            type="number"
            name="stress_score"
            placeholder="StressScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="EmotionsNotes" hasTextareaHeight>
        <Field name="emotions_notes" as="textarea" placeholder="EmotionsNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="RecordedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.recorded_at ?
                  new Date(
                      dayjs(initialValues.recorded_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'recorded_at': date})}
          />
      </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('/mood_entries/mood_entries-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMood_entriesPage
