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/expense_reports/expense_reportsSlice';
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';

import { hasPermission } from '../../helpers/userPermissions';

const Expense_reportsView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { expense_reports } = useAppSelector((state) => state.expense_reports);

  const { currentUser } = useAppSelector((state) => state.auth);

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

            <p>{expense_reports?.employee?.user ?? 'No data'}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Status</p>
            <p>{expense_reports?.status ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>PolicyResult</p>
            <p>{expense_reports?.policy_result}</p>
          </div>

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

            <p>{expense_reports?.organizations?.name ?? 'No data'}</p>
          </div>

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

                      <th>Merchant</th>

                      <th>Amount</th>

                      <th>Currency</th>

                      <th>Category</th>
                    </tr>
                  </thead>
                  <tbody>
                    {expense_reports.expense_lines_report &&
                      Array.isArray(expense_reports.expense_lines_report) &&
                      expense_reports.expense_lines_report.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/expense_lines/expense_lines-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='date'>
                            {dataFormatter.dateTimeFormatter(item.date)}
                          </td>

                          <td data-label='merchant'>{item.merchant}</td>

                          <td data-label='amount'>{item.amount}</td>

                          <td data-label='currency'>{item.currency}</td>

                          <td data-label='category'>{item.category}</td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!expense_reports?.expense_lines_report?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <BaseDivider />

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

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

export default Expense_reportsView;
