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_status/loan_statusSlice'
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_statusView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { loan_status } = useAppSelector((state) => state.loan_status)

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

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CSS</p>
                    <p>{loan_status?.css}</p>
                </div>

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

                <BaseDivider />

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

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

export default Loan_statusView;
