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/payment_status/payment_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 Payment_statusView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { payment_status } = useAppSelector((state) => state.payment_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 payment_status')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View payment_status')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/payment_status/payment_status-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>

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

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

                <FormField label='Visible'>
                    <SwitchField
                      field={{name: 'visible', value: payment_status?.visible}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

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

                                <th>StartPaymentDate</th>

                                <th>PaymentDate</th>

                                <th>LastPaymentPaidDate</th>

                                <th>Sort</th>

                                <th>DeductAmount</th>

                                <th>DeductPaidAmount</th>

                                <th>Interval</th>

                                <th>InterestAmount</th>

                                <th>CommissionAmount</th>

                                <th>TotalAmount</th>

                                <th>TotalPaidAmount</th>

                                <th>PenaltyAmount</th>

                                <th>PendingAmount</th>

                                <th>CrossAmount</th>

                                <th>Remark</th>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                <BaseDivider />

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

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

export default Payment_statusView;
