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/efris_submissions/efris_submissionsSlice'
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 EditEfris_submissions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    invoice: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    receipt: null,
    

    
    
    

    

    

    

    

    

    

    

    
    document_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    submission_status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    submitted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    last_attempt_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    attempt_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    request_payload_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    response_payload_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'efris_invoice_no': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'efris_receipt_no': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'verification_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    qr_code_data: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'error_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    error_message: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { efris_submissionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit efris_submissions')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit efris_submissions'} 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='Invoice' labelFor='invoice'>
        <Field
            name='invoice'
            id='invoice'
            component={SelectField}
            options={initialValues.invoice}
            itemRef={'invoices'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'invoice_number'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DocumentType" labelFor="document_type">
        <Field name="document_type" id="document_type" component="select">
          
            <option value="invoice">invoice</option>
          
            <option value="receipt">receipt</option>
          
            <option value="credit_note">credit_note</option>
          
            <option value="debit_note">debit_note</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SubmissionStatus" labelFor="submission_status">
        <Field name="submission_status" id="submission_status" component="select">
          
            <option value="queued">queued</option>
          
            <option value="submitted">submitted</option>
          
            <option value="accepted">accepted</option>
          
            <option value="rejected">rejected</option>
          
            <option value="failed">failed</option>
          
            <option value="cancelled">cancelled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AttemptCount"
    >
        <Field
            type="number"
            name="attempt_count"
            placeholder="AttemptCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="RequestPayloadJSON" hasTextareaHeight>
        <Field name="request_payload_json" as="textarea" placeholder="RequestPayloadJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ResponsePayloadJSON" hasTextareaHeight>
        <Field name="response_payload_json" as="textarea" placeholder="ResponsePayloadJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="EFRISInvoiceNumber"
    >
        <Field
            name="efris_invoice_no"
            placeholder="EFRISInvoiceNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="EFRISReceiptNumber"
    >
        <Field
            name="efris_receipt_no"
            placeholder="EFRISReceiptNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="VerificationCode"
    >
        <Field
            name="verification_code"
            placeholder="VerificationCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="QRCodeData" hasTextareaHeight>
        <Field name="qr_code_data" as="textarea" placeholder="QRCodeData" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ErrorCode"
    >
        <Field
            name="error_code"
            placeholder="ErrorCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditEfris_submissions
