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/healthcare_providers/healthcare_providersSlice'
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 EditHealthcare_providers = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    provider_type: '',
    

    

    

    

    
    
    
    'provider_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'clinic_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    address: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { healthcare_providersId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit healthcare_providers')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit healthcare_providers'} 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="ProviderType" labelFor="provider_type">
        <Field name="provider_type" id="provider_type" component="select">
          
            <option value="gp">gp</option>
          
            <option value="specialist">specialist</option>
          
            <option value="cardiologist">cardiologist</option>
          
            <option value="endocrinologist">endocrinologist</option>
          
            <option value="dietitian">dietitian</option>
          
            <option value="physiotherapist">physiotherapist</option>
          
            <option value="personal_trainer">personal_trainer</option>
          
            <option value="sports_physician">sports_physician</option>
          
            <option value="gynaecologist">gynaecologist</option>
          
            <option value="urologist">urologist</option>
          
            <option value="psychologist">psychologist</option>
          
            <option value="psychiatrist">psychiatrist</option>
          
            <option value="sleep_specialist">sleep_specialist</option>
          
            <option value="dentist">dentist</option>
          
            <option value="optometrist">optometrist</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="ProviderName"
    >
        <Field
            name="provider_name"
            placeholder="ProviderName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ClinicorPractice"
    >
        <Field
            name="clinic_name"
            placeholder="ClinicorPractice"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Phone"
    >
        <Field
            name="phone"
            placeholder="Phone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Email"
    >
        <Field
            name="email"
            placeholder="Email"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditHealthcare_providers
