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/injuries/injuriesSlice'
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 EditInjuries = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    
    injury_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    mechanism: '',
    

    

    

    

    
    
    
    'diagnosis': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    affected_structure: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    severity: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    recovery_stage: '',
    

    

    

    

    
    
    

    
    initial_treatment: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { injuriesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="MechanismofInjury" labelFor="mechanism">
        <Field name="mechanism" id="mechanism" component="select">
          
            <option value="fall">fall</option>
          
            <option value="overuse">overuse</option>
          
            <option value="collision">collision</option>
          
            <option value="sudden_movement">sudden_movement</option>
          
            <option value="lifting">lifting</option>
          
            <option value="sport">sport</option>
          
            <option value="unknown">unknown</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Diagnosis"
    >
        <Field
            name="diagnosis"
            placeholder="Diagnosis"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AffectedStructure" labelFor="affected_structure">
        <Field name="affected_structure" id="affected_structure" component="select">
          
            <option value="muscle">muscle</option>
          
            <option value="tendon">tendon</option>
          
            <option value="ligament">ligament</option>
          
            <option value="bone">bone</option>
          
            <option value="nerve">nerve</option>
          
            <option value="joint">joint</option>
          
            <option value="skin">skin</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Severity" labelFor="severity">
        <Field name="severity" id="severity" component="select">
          
            <option value="mild">mild</option>
          
            <option value="moderate">moderate</option>
          
            <option value="severe">severe</option>
          
            <option value="grade_1">grade_1</option>
          
            <option value="grade_2">grade_2</option>
          
            <option value="grade_3">grade_3</option>
          
            <option value="unknown">unknown</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RecoveryStage" labelFor="recovery_stage">
        <Field name="recovery_stage" id="recovery_stage" component="select">
          
            <option value="acute">acute</option>
          
            <option value="subacute">subacute</option>
          
            <option value="rehab">rehab</option>
          
            <option value="return_to_sport">return_to_sport</option>
          
            <option value="fully_recovered">fully_recovered</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditInjuries
