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/insurance_policies/insurance_policiesSlice'
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 EditInsurance_policies = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'provider_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'policy_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'group_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'coverage_type': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'contact_phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    claims_contact: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    preauthorisation_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    eob_files: [],
    

    

    

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

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

  const { insurance_policiesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="PolicyNumber"
    >
        <Field
            name="policy_number"
            placeholder="PolicyNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="GroupNumber"
    >
        <Field
            name="group_number"
            placeholder="GroupNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CoverageType"
    >
        <Field
            name="coverage_type"
            placeholder="CoverageType"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ContactPhone"
    >
        <Field
            name="contact_phone"
            placeholder="ContactPhone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ClaimsContact" hasTextareaHeight>
        <Field name="claims_contact" as="textarea" placeholder="ClaimsContact" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="PreAuthorisationNotes" hasTextareaHeight>
        <Field name="preauthorisation_notes" as="textarea" placeholder="PreAuthorisationNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='EOBFiles'
            color='info'
            icon={mdiUpload}
            path={'insurance_policies/eob_files'}
            name='eob_files'
            id='eob_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </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('/insurance_policies/insurance_policies-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditInsurance_policies
