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/sexes/sexesSlice'
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 SexesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { sexes } = useAppSelector((state) => state.sexes)

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

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

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

                                <th>NameKH</th>

                                <th>NameEN</th>

                                <th>PhoneNumber</th>

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

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

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

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

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

                <BaseDivider />

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

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

export default SexesView;
