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/loan_types/loan_typesSlice'
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 Loan_typesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { loan_types } = useAppSelector((state) => state.loan_types)

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NameKH</p>
                    <p>{loan_types?.name_kh}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NameEN</p>
                    <p>{loan_types?.name_en}</p>
                </div>

                <>
                    <p className={'block font-bold mb-2'}>Loans LoanType</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>
                            {loan_types.loans_loan_type && Array.isArray(loan_types.loans_loan_type) &&
                              loan_types.loans_loan_type.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>
                        {!loan_types?.loans_loan_type?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

                <BaseDivider />

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

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

export default Loan_typesView;
