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/medicine_reminders/medicine_remindersSlice'
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 EditMedicine_reminders = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    patient: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    medical_report: null,
    

    
    
    
    'medicine_label': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'dosage': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    frequency: '',
    

    

    

    

    
    
    

    

    

    

    

    
    start_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    end_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    next_due_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    
    instructions: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { medicine_remindersId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Patient' labelFor='patient'>
        <Field
            name='patient'
            id='patient'
            component={SelectField}
            options={initialValues.patient}
            itemRef={'patients'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='MedicalReport' labelFor='medical_report'>
        <Field
            name='medical_report'
            id='medical_report'
            component={SelectField}
            options={initialValues.medical_report}
            itemRef={'medical_reports'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'diagnosis'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="MedicineLabel"
    >
        <Field
            name="medicine_label"
            placeholder="MedicineLabel"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Dosage"
    >
        <Field
            name="dosage"
            placeholder="Dosage"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Frequency" labelFor="frequency">
        <Field name="frequency" id="frequency" component="select">
          
            <option value="once_daily">once_daily</option>
          
            <option value="twice_daily">twice_daily</option>
          
            <option value="thrice_daily">thrice_daily</option>
          
            <option value="every_6_hours">every_6_hours</option>
          
            <option value="every_8_hours">every_8_hours</option>
          
            <option value="weekly">weekly</option>
          
            <option value="as_needed">as_needed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="active">active</option>
          
            <option value="paused">paused</option>
          
            <option value="completed">completed</option>
          
            <option value="cancelled">cancelled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditMedicine_reminders
