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/journal_entries/journal_entriesSlice'
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 Journal_entriesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { journal_entries } = useAppSelector((state) => state.journal_entries)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>EntryNumber</p>
                    <p>{journal_entries?.entryNumber}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='EntryDate'>
                    {journal_entries.entryDate ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={journal_entries.entryDate ?
                        new Date(
                          dayjs(journal_entries.entryDate).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No EntryDate</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>JournalType</p>
                    <p>{journal_entries?.journalType ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{journal_entries?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Reference</p>
                    <p>{journal_entries?.reference}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PayrollRun</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{journal_entries?.payrollRun?.status ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ReversalOf</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{journal_entries?.reversalOfEntry?.entryNumber ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              
                <FormField label='PostedAt'>
                    {journal_entries.postedAt ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={journal_entries.postedAt ?
                        new Date(
                          dayjs(journal_entries.postedAt).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No PostedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PostedBy</p>
                    
                    
                        <p>{journal_entries?.postedByUser?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Payroll_runs JournalEntry</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>PeriodFrom</th>
                                
                                
                                
                                <th>PeriodTo</th>
                                
                                
                                
                                <th>PayrollType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>LockedAt</th>
                                
                                
                                
                                <th>ComputedAt</th>
                                
                                
                                
                                <th>ApprovedAt</th>
                                
                                
                                
                                <th>PostedAt</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {journal_entries.payroll_runs_journalEntry && Array.isArray(journal_entries.payroll_runs_journalEntry) &&
                              journal_entries.payroll_runs_journalEntry.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/payroll_runs/payroll_runs-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="periodFrom">
                                        { dataFormatter.dateTimeFormatter(item.periodFrom) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="periodTo">
                                        { dataFormatter.dateTimeFormatter(item.periodTo) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payrollType">
                                        { item.payrollType }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lockedAt">
                                        { dataFormatter.dateTimeFormatter(item.lockedAt) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="computedAt">
                                        { dataFormatter.dateTimeFormatter(item.computedAt) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="approvedAt">
                                        { dataFormatter.dateTimeFormatter(item.approvedAt) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="postedAt">
                                        { dataFormatter.dateTimeFormatter(item.postedAt) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!journal_entries?.payroll_runs_journalEntry?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Journal_lines JournalEntry</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>
                            {journal_entries.journal_lines_journalEntry && Array.isArray(journal_entries.journal_lines_journalEntry) &&
                              journal_entries.journal_lines_journalEntry.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>
                        {!journal_entries?.journal_lines_journalEntry?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ap_invoices JournalEntry</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>
                            {journal_entries.ap_invoices_journalEntry && Array.isArray(journal_entries.ap_invoices_journalEntry) &&
                              journal_entries.ap_invoices_journalEntry.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>
                        {!journal_entries?.ap_invoices_journalEntry?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Journal_entriesView;