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/medical_appointments/medical_appointmentsSlice'
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 EditMedical_appointmentsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    provider: null,
    

    
    
    

    

    

    

    

    
    appointment_start: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    appointment_end: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    appointment_type: '',
    

    

    

    

    
    
    

    
    clinic_location: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes_to_discuss: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    documents_to_bring: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    post_appointment_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    follow_up_instructions: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    follow_up_reminder_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit medical_appointments')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit medical_appointments'} 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='Provider' labelFor='provider'>
        <Field
            name='provider'
            id='provider'
            component={SelectField}
            options={initialValues.provider}
            itemRef={'healthcare_providers'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'provider_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AppointmentType" labelFor="appointment_type">
        <Field name="appointment_type" id="appointment_type" component="select">
          
            <option value="gp">gp</option>
          
            <option value="specialist">specialist</option>
          
            <option value="check_up">check_up</option>
          
            <option value="test">test</option>
          
            <option value="procedure">procedure</option>
          
            <option value="therapy">therapy</option>
          
            <option value="dentist">dentist</option>
          
            <option value="optometrist">optometrist</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="ClinicLocation" hasTextareaHeight>
        <Field name="clinic_location" as="textarea" placeholder="ClinicLocation" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="NotestoDiscuss" hasTextareaHeight>
        <Field name="notes_to_discuss" as="textarea" placeholder="NotestoDiscuss" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="DocumentstoBring" hasTextareaHeight>
        <Field name="documents_to_bring" as="textarea" placeholder="DocumentstoBring" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="PostAppointmentNotes" hasTextareaHeight>
        <Field name="post_appointment_notes" as="textarea" placeholder="PostAppointmentNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="FollowUpInstructions" hasTextareaHeight>
        <Field name="follow_up_instructions" as="textarea" placeholder="FollowUpInstructions" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

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

export default EditMedical_appointmentsPage
