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/pregnancy_followups/pregnancy_followupsSlice'
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 EditPregnancy_followups = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'followup_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    patient: null,
    

    
    
    
    'weight': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'blood_pressure': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'delivery_plan': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    next_visit_date: new Date(),
    

    

    

    

    

    

    

    
    
    
    'price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { pregnancy_followupsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="رقمالمتابعة"
    >
        <Field
            name="followup_number"
            placeholder="رقمالمتابعة"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='المريضة' labelFor='patient'>
        <Field
            name='patient'
            id='patient'
            component={SelectField}
            options={initialValues.patient}
            itemRef={'patients'}
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="الوزن"
    >
        <Field
            type="number"
            name="weight"
            placeholder="الوزن"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="الضغط"
    >
        <Field
            name="blood_pressure"
            placeholder="الضغط"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="نوعالولادة"
    >
        <Field
            name="delivery_plan"
            placeholder="نوعالولادة"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="تاريخالمراجعةالقادمة"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.next_visit_date ?
                  new Date(
                      dayjs(initialValues.next_visit_date).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'next_visit_date': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="سعرالزيارة"
    >
        <Field
            type="number"
            name="price"
            placeholder="سعرالزيارة"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ملاحظات" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="ملاحظات" />
    </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('/pregnancy_followups/pregnancy_followups-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditPregnancy_followups
