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/branches/branchesSlice'
import dataFormatter from '../../helpers/dataFormatter';
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 BranchesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { branches } = useAppSelector((state) => state.branches)

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

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

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

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

                <>
                    <p className={'block font-bold mb-2'}>Clients Branch</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>NameEN</th>

                                <th>NameKH</th>

                                <th>DateofBirth</th>

                                <th>PhoneNumber</th>

                                <th>IsNew</th>

                                <th>DocumentNumber</th>

                                <th>Sex</th>

                            </tr>
                            </thead>
                            <tbody>
                            {branches.clients_branch && Array.isArray(branches.clients_branch) &&
                              branches.clients_branch.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/clients/clients-view/?id=${item.id}`)}>

                                    <td data-label="code">
                                        { item.code }
                                    </td>

                                    <td data-label="name_en">
                                        { item.name_en }
                                    </td>

                                    <td data-label="name_kh">
                                        { item.name_kh }
                                    </td>

                                    <td data-label="date_of_birth">
                                        { dataFormatter.dateTimeFormatter(item.date_of_birth) }
                                    </td>

                                    <td data-label="phone_number">
                                        { item.phone_number }
                                    </td>

                                    <td data-label="is_new">
                                        { dataFormatter.booleanFormatter(item.is_new) }
                                    </td>

                                    <td data-label="document_number">
                                        { item.document_number }
                                    </td>

                                    <td data-label="sex">
                                        { item.sex }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!branches?.clients_branch?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Deposits Branch</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                                <th>DepositDatetime</th>

                                <th>DepositAmount</th>

                            </tr>
                            </thead>
                            <tbody>
                            {branches.deposits_branch && Array.isArray(branches.deposits_branch) &&
                              branches.deposits_branch.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/deposits/deposits-view/?id=${item.id}`)}>

                                    <td data-label="deposit_datetime">
                                        { dataFormatter.dateTimeFormatter(item.deposit_datetime) }
                                    </td>

                                    <td data-label="deposit_amount">
                                        { item.deposit_amount }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!branches?.deposits_branch?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Expense_items Branch</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                                <th>ExpenseDatetime</th>

                                <th>Description</th>

                                <th>ExpenseAmount</th>

                            </tr>
                            </thead>
                            <tbody>
                            {branches.expense_items_branch && Array.isArray(branches.expense_items_branch) &&
                              branches.expense_items_branch.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/expense_items/expense_items-view/?id=${item.id}`)}>

                                    <td data-label="expense_datetime">
                                        { dataFormatter.dateTimeFormatter(item.expense_datetime) }
                                    </td>

                                    <td data-label="description">
                                        { item.description }
                                    </td>

                                    <td data-label="expense_amount">
                                        { item.expense_amount }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!branches?.expense_items_branch?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Loans Branch</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>PrincipalAmount</th>

                                <th>Term</th>

                                <th>PendingAmount</th>

                                <th>LastPendingAmount</th>

                                <th>Rate</th>

                                <th>CommissionRate</th>

                                <th>RegistrationDate</th>

                                <th>StartedPaymentDate</th>

                                <th>LastPaymentDate</th>

                                <th>FinishPaymentDate</th>

                                <th>FinishDiscount</th>

                                <th>FinishDiscountAmount</th>

                                <th>AdminRate</th>

                                <th>AdminAmount</th>

                                <th>Purpose</th>

                            </tr>
                            </thead>
                            <tbody>
                            {branches.loans_branch && Array.isArray(branches.loans_branch) &&
                              branches.loans_branch.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/loans/loans-view/?id=${item.id}`)}>

                                    <td data-label="code">
                                        { item.code }
                                    </td>

                                    <td data-label="principal_amount">
                                        { item.principal_amount }
                                    </td>

                                    <td data-label="term">
                                        { item.term }
                                    </td>

                                    <td data-label="pending_amount">
                                        { item.pending_amount }
                                    </td>

                                    <td data-label="last_pending_amount">
                                        { item.last_pending_amount }
                                    </td>

                                    <td data-label="rate">
                                        { item.rate }
                                    </td>

                                    <td data-label="commission_rate">
                                        { item.commission_rate }
                                    </td>

                                    <td data-label="registration_date">
                                        { dataFormatter.dateTimeFormatter(item.registration_date) }
                                    </td>

                                    <td data-label="started_payment_date">
                                        { dataFormatter.dateTimeFormatter(item.started_payment_date) }
                                    </td>

                                    <td data-label="last_payment_date">
                                        { dataFormatter.dateTimeFormatter(item.last_payment_date) }
                                    </td>

                                    <td data-label="finish_payment_date">
                                        { dataFormatter.dateTimeFormatter(item.finish_payment_date) }
                                    </td>

                                    <td data-label="finish_discount">
                                        { item.finish_discount }
                                    </td>

                                    <td data-label="finish_discount_amount">
                                        { item.finish_discount_amount }
                                    </td>

                                    <td data-label="admin_rate">
                                        { item.admin_rate }
                                    </td>

                                    <td data-label="admin_amount">
                                        { item.admin_amount }
                                    </td>

                                    <td data-label="purpose">
                                        { item.purpose }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!branches?.loans_branch?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Payment_revenues Branch</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>AdminFeeAmount</th>

                                <th>InterestAmount</th>

                                <th>CommissionAmount</th>

                                <th>ExpenseAmount</th>

                                <th>SettlementDatetime</th>

                            </tr>
                            </thead>
                            <tbody>
                            {branches.payment_revenues_branch && Array.isArray(branches.payment_revenues_branch) &&
                              branches.payment_revenues_branch.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/payment_revenues/payment_revenues-view/?id=${item.id}`)}>

                                    <td data-label="transaction_date">
                                        { dataFormatter.dateTimeFormatter(item.transaction_date) }
                                    </td>

                                    <td data-label="admin_fee_amount">
                                        { item.admin_fee_amount }
                                    </td>

                                    <td data-label="interest_amount">
                                        { item.interest_amount }
                                    </td>

                                    <td data-label="commission_amount">
                                        { item.commission_amount }
                                    </td>

                                    <td data-label="expense_amount">
                                        { item.expense_amount }
                                    </td>

                                    <td data-label="setlement_datetime">
                                        { dataFormatter.dateTimeFormatter(item.setlement_datetime) }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!branches?.payment_revenues_branch?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <>
                    <p className={'block font-bold mb-2'}>Staffs Branch</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>

                                <th>NameEN</th>

                                <th>NameKH</th>

                                <th>DateofBirth</th>

                                <th>PhoneNumber</th>

                                <th>StartWorkDate</th>

                                <th>BornPlace</th>

                                <th>DocumentType</th>

                                <th>DocumentNumber</th>

                                <th>EmergencyNumber</th>

                                <th>CurrentPlace</th>

                                <th>Sex</th>

                            </tr>
                            </thead>
                            <tbody>
                            {branches.staffs_branch && Array.isArray(branches.staffs_branch) &&
                              branches.staffs_branch.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/staffs/staffs-view/?id=${item.id}`)}>

                                    <td data-label="name_en">
                                        { item.name_en }
                                    </td>

                                    <td data-label="name_kh">
                                        { item.name_kh }
                                    </td>

                                    <td data-label="date_of_birth">
                                        { dataFormatter.dateTimeFormatter(item.date_of_birth) }
                                    </td>

                                    <td data-label="phone_number">
                                        { item.phone_number }
                                    </td>

                                    <td data-label="start_work_date">
                                        { dataFormatter.dateTimeFormatter(item.start_work_date) }
                                    </td>

                                    <td data-label="born_place">
                                        { item.born_place }
                                    </td>

                                    <td data-label="document_type">
                                        { item.document_type }
                                    </td>

                                    <td data-label="document_number">
                                        { item.document_number }
                                    </td>

                                    <td data-label="emergency_number">
                                        { item.emergency_number }
                                    </td>

                                    <td data-label="current_place">
                                        { item.current_place }
                                    </td>

                                    <td data-label="sex">
                                        { item.sex }
                                    </td>

                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!branches?.staffs_branch?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <BaseDivider />

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

BranchesView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated>
          {page}
      </LayoutAuthenticated>
    )
}

export default BranchesView;
