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/payments/paymentsSlice'
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 PaymentsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { payments } = useAppSelector((state) => state.payments)

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

                <FormField label='StartPaymentDate'>
                    {payments.start_payment_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={payments.start_payment_date ?
                        new Date(
                          dayjs(payments.start_payment_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No StartPaymentDate</p>}
                </FormField>

                <FormField label='PaymentDate'>
                    {payments.payment_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={payments.payment_date ?
                        new Date(
                          dayjs(payments.payment_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No PaymentDate</p>}
                </FormField>

                <FormField label='LastPaymentPaidDate'>
                    {payments.last_payment_paid_date ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={payments.last_payment_paid_date ?
                        new Date(
                          dayjs(payments.last_payment_paid_date).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No LastPaymentPaidDate</p>}
                </FormField>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Sort</p>
                  <p>{payments?.sort || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DeductAmount</p>
                  <p>{payments?.deduct_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DeductPaidAmount</p>
                  <p>{payments?.deduct_paid_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Interval</p>
                  <p>{payments?.interval || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>InterestAmount</p>
                  <p>{payments?.interest_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CommissionAmount</p>
                  <p>{payments?.commission_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TotalAmount</p>
                  <p>{payments?.total_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TotalPaidAmount</p>
                  <p>{payments?.total_paid_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PenaltyAmount</p>
                  <p>{payments?.penalty_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PendingAmount</p>
                  <p>{payments?.pending_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CrossAmount</p>
                  <p>{payments?.cross_amount || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Remark</p>
                    <p>{payments?.remark}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Loan</p>

                        <p>{payments?.loan?.code ?? 'No data'}</p>

                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>

                        <p>{payments?.status?.name ?? 'No data'}</p>

                </div>

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

                                <th>TransactionDatetime</th>

                                <th>TransactionAmount</th>

                                <th>DeductAmount</th>

                                <th>InterestAmount</th>

                                <th>CommissionAmount</th>

                                <th>RevenueAmount</th>

                                <th>SettlementDatetime</th>

                                <th>Type</th>

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

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

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

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

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

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

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

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

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

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

                <BaseDivider />

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

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

export default PaymentsView;
