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/emergency_medical_cards/emergency_medical_cardsSlice'
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 EditEmergency_medical_cardsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'full_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    date_of_birth: new Date(),
    

    

    

    

    

    

    

    
    
    
    'blood_type': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    critical_allergies: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    critical_conditions: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    critical_medications: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    emergency_contacts: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    treating_doctor_contact: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    organ_donor_status: '',
    

    

    

    

    
    
    

    
    advance_directives_summary: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit emergency_medical_cards')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit emergency_medical_cards'} 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="FullName"
    >
        <Field
            name="full_name"
            placeholder="FullName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="BloodType"
    >
        <Field
            name="blood_type"
            placeholder="BloodType"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="CriticalAllergies" hasTextareaHeight>
        <Field name="critical_allergies" as="textarea" placeholder="CriticalAllergies" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="CriticalConditions" hasTextareaHeight>
        <Field name="critical_conditions" as="textarea" placeholder="CriticalConditions" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="CriticalMedications" hasTextareaHeight>
        <Field name="critical_medications" as="textarea" placeholder="CriticalMedications" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="EmergencyContacts" hasTextareaHeight>
        <Field name="emergency_contacts" as="textarea" placeholder="EmergencyContacts" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="TreatingDoctorContact" hasTextareaHeight>
        <Field name="treating_doctor_contact" as="textarea" placeholder="TreatingDoctorContact" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="OrganDonorStatus" labelFor="organ_donor_status">
        <Field name="organ_donor_status" id="organ_donor_status" component="select">
          
            <option value="yes">yes</option>
          
            <option value="no">no</option>
          
            <option value="specific_organs">specific_organs</option>
          
            <option value="unknown">unknown</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditEmergency_medical_cardsPage
