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/cost_centers/cost_centersSlice'
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 Cost_centersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { cost_centers } = useAppSelector((state) => state.cost_centers)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Departments CostCenter</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Code</th>
                                
                                
                                
                                <th>Name</th>
                                
                                
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {cost_centers.departments_costCenter && Array.isArray(cost_centers.departments_costCenter) &&
                              cost_centers.departments_costCenter.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/departments/departments-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="code">
                                        { item.code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="isActive">
                                        { dataFormatter.booleanFormatter(item.isActive) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!cost_centers?.departments_costCenter?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Journal_lines CostCenter</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>
                            {cost_centers.journal_lines_costCenter && Array.isArray(cost_centers.journal_lines_costCenter) &&
                              cost_centers.journal_lines_costCenter.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>
                        {!cost_centers?.journal_lines_costCenter?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ap_invoices CostCenter</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>
                            {cost_centers.ap_invoices_costCenter && Array.isArray(cost_centers.ap_invoices_costCenter) &&
                              cost_centers.ap_invoices_costCenter.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>
                        {!cost_centers?.ap_invoices_costCenter?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Cost_centersView;