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

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

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

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

                                <th>NameEN</th>

                                <th>NameKH</th>

                                <th>DateofBirth</th>

                                <th>PhoneNumber</th>

                                <th>StartWorkDate</th>

                                <th>BornPlace</th>

                                <th>DocumentType</th>

                                <th>DocumentNumber</th>

                                <th>EmergencyNumber</th>

                                <th>CurrentPlace</th>

                                <th>Sex</th>

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

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

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

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

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

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

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

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

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

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

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

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

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

                <BaseDivider />

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

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

export default Staff_statusView;
