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_types/expense_typesSlice'
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 Expense_typesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { expense_types } = useAppSelector((state) => state.expense_types)

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NameKH</p>
                    <p>{expense_types?.name_kh}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>NameEN</p>
                    <p>{expense_types?.name_en}</p>
                </div>

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

                                <th>ExpenseDatetime</th>

                                <th>Description</th>

                                <th>ExpenseAmount</th>

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

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

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

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

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

                <BaseDivider />

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

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

export default Expense_typesView;
