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/ap_invoices/ap_invoicesSlice'
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 EditAp_invoicesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'invoiceNumber': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    vendor: null,
    

    
    
    
    'vendorInvoiceNumber': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    invoiceDate: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    receiptDate: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    dueDate: new Date(),
    

    

    

    

    

    

    

    
    
    
    'grossAmount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vatAmount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'ewtAmount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'netPayable': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    glAccount: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    costCenter: null,
    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    journalEntry: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="APInvoiceNumber"
    >
        <Field
            name="invoiceNumber"
            placeholder="APInvoiceNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Vendor' labelFor='vendor'>
        <Field
            name='vendor'
            id='vendor'
            component={SelectField}
            options={initialValues.vendor}
            itemRef={'vendors'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'vendorName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="VendorInvoiceNumber"
    >
        <Field
            name="vendorInvoiceNumber"
            placeholder="VendorInvoiceNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="GrossAmount"
    >
        <Field
            type="number"
            name="grossAmount"
            placeholder="GrossAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="EWTAmount"
    >
        <Field
            type="number"
            name="ewtAmount"
            placeholder="EWTAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="NetPayable"
    >
        <Field
            type="number"
            name="netPayable"
            placeholder="NetPayable"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Description" hasTextareaHeight>
        <Field name="description" as="textarea" placeholder="Description" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='GLAccount' labelFor='glAccount'>
        <Field
            name='glAccount'
            id='glAccount'
            component={SelectField}
            options={initialValues.glAccount}
            itemRef={'accounts'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CostCenter' labelFor='costCenter'>
        <Field
            name='costCenter'
            id='costCenter'
            component={SelectField}
            options={initialValues.costCenter}
            itemRef={'cost_centers'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="UNPOSTED">UNPOSTED</option>
          
            <option value="POSTED">POSTED</option>
          
            <option value="PARTIALLY_PAID">PARTIALLY_PAID</option>
          
            <option value="FULLY_PAID">FULLY_PAID</option>
          
            <option value="OVERDUE">OVERDUE</option>
          
            <option value="CANCELLED">CANCELLED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditAp_invoicesPage
