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

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

                <FormField label='IsAdmin'>
                    <SwitchField
                      field={{name: 'is_admin', value: group_menus?.is_admin}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

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

                                <th>Label</th>

                                <th>URL</th>

                                <th>ActiveURL</th>

                                <th>Permission</th>

                                <th>Icon</th>

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

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

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

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

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

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

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

                <BaseDivider />

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

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

export default Group_menusView;
