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/accounts/accountsSlice'
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 AccountsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { accounts } = useAppSelector((state) => state.accounts)
    

    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 accounts')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View accounts')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/accounts/accounts-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AccountCode</p>
                    <p>{accounts?.code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AccountName</p>
                    <p>{accounts?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AccountType</p>
                    <p>{accounts?.accountType ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ParentAccount</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{accounts?.parent?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsHeaderAccount'>
                    <SwitchField
                      field={{name: 'isHeader', value: accounts?.isHeader}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NormalBalance</p>
                    <p>{accounts?.normalBalance ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Journal_lines Account</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                <th>Debit</th>
                                
                                
                                
                                <th>Credit</th>
                                
                                
                                
                                <th>LineOrder</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {accounts.journal_lines_account && Array.isArray(accounts.journal_lines_account) &&
                              accounts.journal_lines_account.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/journal_lines/journal_lines-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="debit">
                                        { item.debit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="credit">
                                        { item.credit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lineOrder">
                                        { item.lineOrder }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!accounts?.journal_lines_account?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Vendors DefaultAccount</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>VendorCode</th>
                                
                                
                                
                                <th>VendorName</th>
                                
                                
                                
                                <th>VendorType</th>
                                
                                
                                
                                <th>TaxClassification</th>
                                
                                
                                
                                <th>TINNumber</th>
                                
                                
                                
                                <th>BIRRegisteredAddress</th>
                                
                                
                                
                                <th>ContactPerson</th>
                                
                                
                                
                                <th>ContactNumber</th>
                                
                                
                                
                                <th>Email</th>
                                
                                
                                
                                <th>Address</th>
                                
                                
                                
                                <th>PaymentTermsDays</th>
                                
                                
                                
                                
                                
                                <th>WithholdingATCCode</th>
                                
                                
                                
                                <th>WithholdingTaxRate</th>
                                
                                
                                
                                <th>BankName</th>
                                
                                
                                
                                <th>BankAccountNumber</th>
                                
                                
                                
                                <th>BankAccountName</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {accounts.vendors_defaultAccount && Array.isArray(accounts.vendors_defaultAccount) &&
                              accounts.vendors_defaultAccount.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/vendors/vendors-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="vendorCode">
                                        { item.vendorCode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vendorName">
                                        { item.vendorName }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vendorType">
                                        { item.vendorType }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="taxClassification">
                                        { item.taxClassification }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tinNumber">
                                        { item.tinNumber }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="birRegAddress">
                                        { item.birRegAddress }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="contactPerson">
                                        { item.contactPerson }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="contactNumber">
                                        { item.contactNumber }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="email">
                                        { item.email }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="address">
                                        { item.address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="paymentTermsDays">
                                        { item.paymentTermsDays }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="wtaxAtcCode">
                                        { item.wtaxAtcCode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="wtaxRate">
                                        { item.wtaxRate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bankName">
                                        { item.bankName }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bankAccountNumber">
                                        { item.bankAccountNumber }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bankAccountName">
                                        { item.bankAccountName }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="isActive">
                                        { dataFormatter.booleanFormatter(item.isActive) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!accounts?.vendors_defaultAccount?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ap_invoices GLAccount</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>
                            {accounts.ap_invoices_glAccount && Array.isArray(accounts.ap_invoices_glAccount) &&
                              accounts.ap_invoices_glAccount.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>
                        {!accounts?.ap_invoices_glAccount?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default AccountsView;