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/pain_logs/pain_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 EditPain_logs = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    
    logged_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    body_location: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    pain_type: '',
    

    

    

    

    
    
    

    

    

    
    severity: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    duration_type: '',
    

    

    

    

    
    
    

    
    better_with: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    worse_with: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    impact_level: '',
    

    

    

    

    
    
    

    
    medications_taken: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    journal_notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { pain_logsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BodyLocation" labelFor="body_location">
        <Field name="body_location" id="body_location" component="select">
          
            <option value="head_neck">head_neck</option>
          
            <option value="chest">chest</option>
          
            <option value="abdomen">abdomen</option>
          
            <option value="back">back</option>
          
            <option value="left_shoulder">left_shoulder</option>
          
            <option value="right_shoulder">right_shoulder</option>
          
            <option value="left_arm">left_arm</option>
          
            <option value="right_arm">right_arm</option>
          
            <option value="left_hip">left_hip</option>
          
            <option value="right_hip">right_hip</option>
          
            <option value="left_knee">left_knee</option>
          
            <option value="right_knee">right_knee</option>
          
            <option value="left_ankle">left_ankle</option>
          
            <option value="right_ankle">right_ankle</option>
          
            <option value="left_foot">left_foot</option>
          
            <option value="right_foot">right_foot</option>
          
            <option value="general">general</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PainType" labelFor="pain_type">
        <Field name="pain_type" id="pain_type" component="select">
          
            <option value="sharp">sharp</option>
          
            <option value="dull">dull</option>
          
            <option value="burning">burning</option>
          
            <option value="stabbing">stabbing</option>
          
            <option value="aching">aching</option>
          
            <option value="throbbing">throbbing</option>
          
            <option value="pressure">pressure</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Severity(0-10)"
    >
        <Field
            type="number"
            name="severity"
            placeholder="Severity(0-10)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DurationType" labelFor="duration_type">
        <Field name="duration_type" id="duration_type" component="select">
          
            <option value="constant">constant</option>
          
            <option value="intermittent">intermittent</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="BetterWith" hasTextareaHeight>
        <Field name="better_with" as="textarea" placeholder="BetterWith" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="WorseWith" hasTextareaHeight>
        <Field name="worse_with" as="textarea" placeholder="WorseWith" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ImpactonActivities" labelFor="impact_level">
        <Field name="impact_level" id="impact_level" component="select">
          
            <option value="none">none</option>
          
            <option value="mild_limitation">mild_limitation</option>
          
            <option value="significant_limitation">significant_limitation</option>
          
            <option value="unable_to_function">unable_to_function</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="MedicationsTaken" hasTextareaHeight>
        <Field name="medications_taken" as="textarea" placeholder="MedicationsTaken" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditPain_logs
