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/authorization_cases/authorization_casesSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditAuthorization_casesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    location: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    patient: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    payer_plan: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    cpt_code: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    procedure_family: null,
    

    
    
    

    

    

    

    

    

    

    

    
    stage: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    urgency: '',
    

    

    

    

    
    
    
    'ordering_provider_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'rendering_provider_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'site_of_service': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    scheduled_date: new Date(),
    

    

    

    

    

    

    

    
    
    
    'dollars_at_risk': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    owner_user: null,
    

    
    
    

    

    

    

    

    
    due_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    sla_state: '',
    

    

    

    

    
    
    

    
    internal_notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

  const { authorization_cases } = useAppSelector((state) => state.authorization_cases)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
    {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
    <FormField label='Organization' labelFor='organization'>
        <Field
            name='organization'
            id='organization'
            component={SelectField}
            options={initialValues.organization}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
     } 

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Location' labelFor='location'>
        <Field
            name='location'
            id='location'
            component={SelectField}
            options={initialValues.location}
            itemRef={'locations'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PayerPlan' labelFor='payer_plan'>
        <Field
            name='payer_plan'
            id='payer_plan'
            component={SelectField}
            options={initialValues.payer_plan}
            itemRef={'payer_plans'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CPTCode' labelFor='cpt_code'>
        <Field
            name='cpt_code'
            id='cpt_code'
            component={SelectField}
            options={initialValues.cpt_code}
            itemRef={'cpt_codes'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ProcedureFamily' labelFor='procedure_family'>
        <Field
            name='procedure_family'
            id='procedure_family'
            component={SelectField}
            options={initialValues.procedure_family}
            itemRef={'procedure_families'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Stage" labelFor="stage">
        <Field name="stage" id="stage" component="select">
          
            <option value="intake">intake</option>
          
            <option value="needs_evidence">needs_evidence</option>
          
            <option value="ready_to_submit">ready_to_submit</option>
          
            <option value="submitted">submitted</option>
          
            <option value="pending_decision">pending_decision</option>
          
            <option value="approved">approved</option>
          
            <option value="denied">denied</option>
          
            <option value="appeal_in_progress">appeal_in_progress</option>
          
            <option value="closed">closed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Urgency" labelFor="urgency">
        <Field name="urgency" id="urgency" component="select">
          
            <option value="routine">routine</option>
          
            <option value="urgent">urgent</option>
          
            <option value="stat">stat</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="OrderingProviderName"
    >
        <Field
            name="ordering_provider_name"
            placeholder="OrderingProviderName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="RenderingProviderName"
    >
        <Field
            name="rendering_provider_name"
            placeholder="RenderingProviderName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SiteOfService"
    >
        <Field
            name="site_of_service"
            placeholder="SiteOfService"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DollarsAtRisk"
    >
        <Field
            type="number"
            name="dollars_at_risk"
            placeholder="DollarsAtRisk"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='OwnerUser' labelFor='owner_user'>
        <Field
            name='owner_user'
            id='owner_user'
            component={SelectField}
            options={initialValues.owner_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SLAState" labelFor="sla_state">
        <Field name="sla_state" id="sla_state" component="select">
          
            <option value="on_track">on_track</option>
          
            <option value="due_soon">due_soon</option>
          
            <option value="overdue">overdue</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditAuthorization_casesPage
