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/case_documents/case_documentsSlice'
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 EditCase_documents = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    authorization_case: null,
    

    
    
    

    

    

    

    

    

    

    

    
    document_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    source_channel: '',
    

    

    

    

    
    
    
    'storage_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'original_filename': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'checksum': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    uploaded_by_user: null,
    

    
    
    

    

    

    

    

    
    uploaded_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    version_number: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { case_documentsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AuthorizationCase' labelFor='authorization_case'>
        <Field
            name='authorization_case'
            id='authorization_case'
            component={SelectField}
            options={initialValues.authorization_case}
            itemRef={'authorization_cases'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'site_of_service'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DocumentType" labelFor="document_type">
        <Field name="document_type" id="document_type" component="select">
          
            <option value="order">order</option>
          
            <option value="clinical_notes">clinical_notes</option>
          
            <option value="imaging_report">imaging_report</option>
          
            <option value="labs">labs</option>
          
            <option value="prior_auth_form">prior_auth_form</option>
          
            <option value="denial_letter">denial_letter</option>
          
            <option value="appeal_letter">appeal_letter</option>
          
            <option value="insurance_card">insurance_card</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SourceChannel" labelFor="source_channel">
        <Field name="source_channel" id="source_channel" component="select">
          
            <option value="upload">upload</option>
          
            <option value="fax">fax</option>
          
            <option value="email">email</option>
          
            <option value="portal_download">portal_download</option>
          
            <option value="integration">integration</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="StorageKey"
    >
        <Field
            name="storage_key"
            placeholder="StorageKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="OriginalFilename"
    >
        <Field
            name="original_filename"
            placeholder="OriginalFilename"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Checksum"
    >
        <Field
            name="checksum"
            placeholder="Checksum"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="VersionNumber"
    >
        <Field
            type="number"
            name="version_number"
            placeholder="VersionNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='organizations' labelFor='organizations'>
        <Field
            name='organizations'
            id='organizations'
            component={SelectField}
            options={initialValues.organizations}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/case_documents/case_documents-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCase_documents
