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/currencies/currenciesSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";


const CurrenciesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { currencies } = useAppSelector((state) => state.currencies)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Symbol</p>
                    <p>{currencies?.symbol}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MinorUnit</p>
                  <p>{currencies?.minor_unit || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Contacts Currency</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ContactType</th>
                                
                                
                                
                                <th>DisplayName</th>
                                
                                
                                
                                <th>CompanyName</th>
                                
                                
                                
                                <th>FirstName</th>
                                
                                
                                
                                <th>LastName</th>
                                
                                
                                
                                <th>Email</th>
                                
                                
                                
                                <th>Phone</th>
                                
                                
                                
                                <th>BillingAddress</th>
                                
                                
                                
                                <th>ShippingAddress</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {currencies.contacts_currency && Array.isArray(currencies.contacts_currency) &&
                              currencies.contacts_currency.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/contacts/contacts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="contact_type">
                                        { item.contact_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="display_name">
                                        { item.display_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="company_name">
                                        { item.company_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="first_name">
                                        { item.first_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_name">
                                        { item.last_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="email">
                                        { item.email }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="phone">
                                        { item.phone }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="billing_address">
                                        { item.billing_address }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shipping_address">
                                        { item.shipping_address }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!currencies?.contacts_currency?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quotes Currency</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>QuoteNumber</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>IssueDate</th>
                                
                                
                                
                                <th>ExpiryDate</th>
                                
                                
                                
                                
                                
                                <th>Subtotal</th>
                                
                                
                                
                                <th>TaxTotal</th>
                                
                                
                                
                                <th>Total</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {currencies.quotes_currency && Array.isArray(currencies.quotes_currency) &&
                              currencies.quotes_currency.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quotes/quotes-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quote_number">
                                        { item.quote_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="issue_date">
                                        { dataFormatter.dateTimeFormatter(item.issue_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expiry_date">
                                        { dataFormatter.dateTimeFormatter(item.expiry_date) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="subtotal">
                                        { item.subtotal }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_total">
                                        { item.tax_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total">
                                        { item.total }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!currencies?.quotes_currency?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Invoices Currency</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>InvoiceNumber</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>IssueDate</th>
                                
                                
                                
                                <th>DueDate</th>
                                
                                
                                
                                
                                
                                <th>Subtotal</th>
                                
                                
                                
                                <th>TaxTotal</th>
                                
                                
                                
                                <th>Total</th>
                                
                                
                                
                                <th>AmountDue</th>
                                
                                
                                
                                <th>Reference</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {currencies.invoices_currency && Array.isArray(currencies.invoices_currency) &&
                              currencies.invoices_currency.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/invoices/invoices-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="invoice_number">
                                        { item.invoice_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="issue_date">
                                        { dataFormatter.dateTimeFormatter(item.issue_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_date">
                                        { dataFormatter.dateTimeFormatter(item.due_date) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="subtotal">
                                        { item.subtotal }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_total">
                                        { item.tax_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total">
                                        { item.total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount_due">
                                        { item.amount_due }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reference">
                                        { item.reference }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!currencies?.invoices_currency?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Bills Currency</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>BillNumber</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>IssueDate</th>
                                
                                
                                
                                <th>DueDate</th>
                                
                                
                                
                                
                                
                                <th>Subtotal</th>
                                
                                
                                
                                <th>TaxTotal</th>
                                
                                
                                
                                <th>Total</th>
                                
                                
                                
                                <th>AmountDue</th>
                                
                                
                                
                                <th>Reference</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {currencies.bills_currency && Array.isArray(currencies.bills_currency) &&
                              currencies.bills_currency.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/bills/bills-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="bill_number">
                                        { item.bill_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="issue_date">
                                        { dataFormatter.dateTimeFormatter(item.issue_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_date">
                                        { dataFormatter.dateTimeFormatter(item.due_date) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="subtotal">
                                        { item.subtotal }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_total">
                                        { item.tax_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total">
                                        { item.total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount_due">
                                        { item.amount_due }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reference">
                                        { item.reference }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!currencies?.bills_currency?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Expenses Currency</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>ExpenseType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ExpenseDate</th>
                                
                                
                                
                                <th>Amount</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {currencies.expenses_currency && Array.isArray(currencies.expenses_currency) &&
                              currencies.expenses_currency.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/expenses/expenses-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="expense_type">
                                        { item.expense_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expense_date">
                                        { dataFormatter.dateTimeFormatter(item.expense_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!currencies?.expenses_currency?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Bank_accounts Currency</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>BankAccountName</th>
                                
                                
                                
                                <th>BankName</th>
                                
                                
                                
                                <th>AccountNumberMasked</th>
                                
                                
                                
                                <th>AccountTypeLabel</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                                
                                <th>LastSyncAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {currencies.bank_accounts_currency && Array.isArray(currencies.bank_accounts_currency) &&
                              currencies.bank_accounts_currency.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/bank_accounts/bank_accounts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bank_name">
                                        { item.bank_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="account_number_masked">
                                        { item.account_number_masked }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="account_type_label">
                                        { item.account_type_label }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_sync_at">
                                        { dataFormatter.dateTimeFormatter(item.last_sync_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!currencies?.bank_accounts_currency?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Bank_transactions Currency</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>TransactionDate</th>
                                
                                
                                
                                <th>PostedAt</th>
                                
                                
                                
                                <th>Direction</th>
                                
                                
                                
                                <th>Amount</th>
                                
                                
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                <th>Reference</th>
                                
                                
                                
                                <th>MatchStatus</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {currencies.bank_transactions_currency && Array.isArray(currencies.bank_transactions_currency) &&
                              currencies.bank_transactions_currency.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/bank_transactions/bank_transactions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="transaction_date">
                                        { dataFormatter.dateTimeFormatter(item.transaction_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="posted_at">
                                        { dataFormatter.dateTimeFormatter(item.posted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="direction">
                                        { item.direction }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reference">
                                        { item.reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="match_status">
                                        { item.match_status }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!currencies?.bank_transactions_currency?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Payments Currency</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>PaymentType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>PaymentDate</th>
                                
                                
                                
                                <th>Amount</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Reference</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {currencies.payments_currency && Array.isArray(currencies.payments_currency) &&
                              currencies.payments_currency.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/payments/payments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="payment_type">
                                        { item.payment_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_date">
                                        { dataFormatter.dateTimeFormatter(item.payment_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="reference">
                                        { item.reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!currencies?.payments_currency?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default CurrenciesView;