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/paymentSlice';
import { saveFile } from '../../helpers/fileSaver';
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from '../../components/ImageField';
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 PaymentView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { payment } = useAppSelector((state) => state.payment);

  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')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={removeLastCharacter('View payment')}
          main
        >
          <BaseButton
            color='info'
            label='Edit'
            href={`/payment/payment-edit/?id=${id}`}
          />
        </SectionTitleLineWithButton>
        <CardBox>
          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Student</p>

            <p>{payment?.student?.first_name ?? 'No data'}</p>
          </div>

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

            <p>{payment?.invoice?.amount ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Amountpaid</p>
            <p>{payment?.amountpaid || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Paymentmode</p>
            <p>{payment?.paymentmode ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Chequeno</p>
            <p>{payment?.chequeno}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Bankname</p>
            <p>{payment?.bankname}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Transactionid</p>
            <p>{payment?.transactionid}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Ddnumber</p>
            <p>{payment?.ddnumber}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Ddissuingbank</p>
            <p>{payment?.ddissuingbank}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Receiptnumber</p>
            <p>{payment?.receiptnumber}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Discountpercentage</p>
            <p>{payment?.discountpercentage || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Discountamount</p>
            <p>{payment?.discountamount || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Discountreason</p>
            <p>{payment?.discountreason}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Remainingbalance</p>
            <p>{payment?.remainingbalance || 'No data'}</p>
          </div>

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

          <BaseDivider />

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

PaymentView.getLayout = function getLayout(page: ReactElement) {
  return (
    <LayoutAuthenticated permission={'READ_PAYMENT'}>
      {page}
    </LayoutAuthenticated>
  );
};

export default PaymentView;
