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/ocr_extractions/ocr_extractionsSlice'
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 EditOcr_extractions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    receipt: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    merchant: null,
    

    
    
    

    

    

    

    

    
    processed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    engine: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'invoice_number_text': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'seller_name_text': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'buyer_name_text': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tin_number_text': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    invoice_date: new Date(),
    

    

    

    

    

    

    

    
    
    
    'subtotal_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vat_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tot_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'total_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    raw_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { ocr_extractionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Receipt' labelFor='receipt'>
        <Field
            name='receipt'
            id='receipt'
            component={SelectField}
            options={initialValues.receipt}
            itemRef={'receipts'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'source_channel'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Merchant' labelFor='merchant'>
        <Field
            name='merchant'
            id='merchant'
            component={SelectField}
            options={initialValues.merchant}
            itemRef={'merchants'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'business_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="OCREngine" labelFor="engine">
        <Field name="engine" id="engine" component="select">
          
            <option value="tesseract">tesseract</option>
          
            <option value="google_vision">google_vision</option>
          
            <option value="aws_textract">aws_textract</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="success">success</option>
          
            <option value="partial">partial</option>
          
            <option value="failed">failed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="InvoiceNumber(Text)"
    >
        <Field
            name="invoice_number_text"
            placeholder="InvoiceNumber(Text)"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SellerName(Text)"
    >
        <Field
            name="seller_name_text"
            placeholder="SellerName(Text)"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BuyerName(Text)"
    >
        <Field
            name="buyer_name_text"
            placeholder="BuyerName(Text)"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TINNumber(Text)"
    >
        <Field
            name="tin_number_text"
            placeholder="TINNumber(Text)"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SubtotalAmount"
    >
        <Field
            type="number"
            name="subtotal_amount"
            placeholder="SubtotalAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="VATAmount"
    >
        <Field
            type="number"
            name="vat_amount"
            placeholder="VATAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TOTAmount"
    >
        <Field
            type="number"
            name="tot_amount"
            placeholder="TOTAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalAmount"
    >
        <Field
            type="number"
            name="total_amount"
            placeholder="TotalAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditOcr_extractions
