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/banks/banksSlice'
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 BanksView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { banks } = useAppSelector((state) => state.banks)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Business</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{banks?.business?.trade_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SWIFT</p>
                    <p>{banks?.swift}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Website</p>
                    <p>{banks?.website}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Bank_accounts Bank</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>AccountTitle</th>
                                
                                
                                
                                <th>AccountNumber</th>
                                
                                
                                
                                <th>IBAN</th>
                                
                                
                                
                                <th>CardNumber</th>
                                
                                
                                
                                
                                
                                <th>OpeningBalance</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {banks.bank_accounts_bank && Array.isArray(banks.bank_accounts_bank) &&
                              banks.bank_accounts_bank.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/bank_accounts/bank_accounts-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="account_title">
                                        { item.account_title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="account_number">
                                        { item.account_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="iban">
                                        { item.iban }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="card_number">
                                        { item.card_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="opening_balance">
                                        { item.opening_balance }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!banks?.bank_accounts_bank?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Letters_of_credit IssuingBank</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>LCNumber</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>IssueDate</th>
                                
                                
                                
                                <th>ExpiryDate</th>
                                
                                
                                
                                <th>Amount</th>
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {banks.letters_of_credit_bank && Array.isArray(banks.letters_of_credit_bank) &&
                              banks.letters_of_credit_bank.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/letters_of_credit/letters_of_credit-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="lc_number">
                                        { item.lc_number }
                                    </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="amount">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!banks?.letters_of_credit_bank?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default BanksView;