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/menus/menusSlice'
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 MenusView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { menus } = useAppSelector((state) => state.menus)

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

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

                        <p>{menus?.parent?.label ?? 'No data'}</p>

                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Label</p>
                    <p>{menus?.label}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>URL</p>
                    <p>{menus?.url}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ActiveURL</p>
                    <p>{menus?.active_url}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Permission</p>
                    <p>{menus?.permission}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Icon</p>
                    <p>{menus?.icon}</p>
                </div>

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

                        <p>{menus?.url?.uri ?? 'No data'}</p>

                </div>

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

                        <p>{menus?.group?.name ?? 'No data'}</p>

                </div>

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

                                <th>Status</th>

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

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

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

                <BaseDivider />

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

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

export default MenusView;
