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/urls/urlsSlice'
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 UrlsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { urls } = useAppSelector((state) => state.urls)

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

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Method</p>
                    <p>{urls?.method ?? 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>URI</p>
                    <p>{urls?.uri}</p>
                </div>

                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RouteName</p>
                    <p>{urls?.route_name}</p>
                </div>

                <FormField label='Active'>
                    <SwitchField
                      field={{name: 'acitve', value: urls?.acitve}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

                <FormField label='IsMenu'>
                    <SwitchField
                      field={{name: 'is_menu', value: urls?.is_menu}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>

                <>
                    <p className={'block font-bold mb-2'}>Menus URL</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>
                            {urls.menus_url && Array.isArray(urls.menus_url) &&
                              urls.menus_url.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>
                        {!urls?.menus_url?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>

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

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

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

                <BaseDivider />

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

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

export default UrlsView;
