import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";
import {useAppDispatch, useAppSelector} from "../../stores/hooks";
import {useRouter} from "next/router";
import { fetch } from '../../stores/vendors/vendorsSlice'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";
import LayoutAuthenticated from "../../layouts/Authenticated";
import {getPageTitle} from "../../config";
import SectionTitleLineWithButton from "../../components/SectionTitleLineWithButton";
import SectionMain from "../../components/SectionMain";
import CardBox from "../../components/CardBox";
import BaseButton from "../../components/BaseButton";
import BaseDivider from "../../components/BaseDivider";
import {mdiChartTimelineVariant} from "@mdi/js";
import {SwitchField} from "../../components/SwitchField";
import FormField from "../../components/FormField";


const VendorsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { vendors } = useAppSelector((state) => state.vendors)
    

    const { id } = router.query;
    
    function removeLastCharacter(str) {
      console.log(str,`str`)
      return str.slice(0, -1);
    }

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


    return (
      <>
          <Head>
              <title>{getPageTitle('View vendors')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View vendors')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/vendors/vendors-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>VendorCode</p>
                    <p>{vendors?.vendorCode}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>VendorName</p>
                    <p>{vendors?.vendorName}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>VendorType</p>
                    <p>{vendors?.vendorType ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TaxClassification</p>
                    <p>{vendors?.taxClassification ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TINNumber</p>
                    <p>{vendors?.tinNumber}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={vendors?.birRegAddress} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ContactPerson</p>
                    <p>{vendors?.contactPerson}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ContactNumber</p>
                    <p>{vendors?.contactNumber}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Email</p>
                    <p>{vendors?.email}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={vendors?.address} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PaymentTermsDays</p>
                  <p>{vendors?.paymentTermsDays || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DefaultAccount</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{vendors?.defaultAccount?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>WithholdingATCCode</p>
                    <p>{vendors?.wtaxAtcCode}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>WithholdingTaxRate</p>
                  <p>{vendors?.wtaxRate || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BankName</p>
                    <p>{vendors?.bankName}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BankAccountNumber</p>
                    <p>{vendors?.bankAccountNumber}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BankAccountName</p>
                    <p>{vendors?.bankAccountName}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsActive'>
                    <SwitchField
                      field={{name: 'isActive', value: vendors?.isActive}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ap_invoices Vendor</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>APInvoiceNumber</th>
                                
                                
                                
                                
                                
                                <th>VendorInvoiceNumber</th>
                                
                                
                                
                                <th>InvoiceDate</th>
                                
                                
                                
                                <th>ReceiptDate</th>
                                
                                
                                
                                <th>DueDate</th>
                                
                                
                                
                                <th>GrossAmount</th>
                                
                                
                                
                                <th>VATAmount</th>
                                
                                
                                
                                <th>EWTAmount</th>
                                
                                
                                
                                <th>NetPayable</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {vendors.ap_invoices_vendor && Array.isArray(vendors.ap_invoices_vendor) &&
                              vendors.ap_invoices_vendor.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/ap_invoices/ap_invoices-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="invoiceNumber">
                                        { item.invoiceNumber }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="vendorInvoiceNumber">
                                        { item.vendorInvoiceNumber }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invoiceDate">
                                        { dataFormatter.dateTimeFormatter(item.invoiceDate) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="receiptDate">
                                        { dataFormatter.dateTimeFormatter(item.receiptDate) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dueDate">
                                        { dataFormatter.dateTimeFormatter(item.dueDate) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="grossAmount">
                                        { item.grossAmount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vatAmount">
                                        { item.vatAmount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ewtAmount">
                                        { item.ewtAmount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="netPayable">
                                        { item.netPayable }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!vendors?.ap_invoices_vendor?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

                <BaseButton
                    color='info'
                    label='Back'
                    onClick={() => router.push('/vendors/vendors-list')}
                />
              </CardBox>
          </SectionMain>
      </>
    );
};

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

export default VendorsView;