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/button_links/button_linksSlice';
import { saveFile } from '../../helpers/fileSaver';
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from '../../components/ImageField';
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';

import { hasPermission } from '../../helpers/userPermissions';

const Button_linksView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { button_links } = useAppSelector((state) => state.button_links);

  const { currentUser } = useAppSelector((state) => state.auth);

  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 button_links')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={removeLastCharacter('View button_links')}
          main
        >
          <BaseButton
            color='info'
            label='Edit'
            href={`/button_links/button_links-edit/?id=${id}`}
          />
        </SectionTitleLineWithButton>
        <CardBox>
          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Block</p>

            <p>{button_links?.block?.type ?? 'No data'}</p>
          </div>

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

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

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

          <FormField label='Enabled'>
            <SwitchField
              field={{ name: 'enabled', value: button_links?.enabled }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>OrderIndex</p>
            <p>{button_links?.order_index || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>ClickCount</p>
            <p>{button_links?.click_count || 'No data'}</p>
          </div>

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

            <p>{button_links?.organizations?.name ?? 'No data'}</p>
          </div>

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

                      <th>Referrer</th>

                      <th>UserAgent</th>

                      <th>IPHash</th>

                      <th>OccurredAt</th>
                    </tr>
                  </thead>
                  <tbody>
                    {button_links.analytics_events_link &&
                      Array.isArray(button_links.analytics_events_link) &&
                      button_links.analytics_events_link.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/analytics_events/analytics_events-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='event_type'>{item.event_type}</td>

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

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

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

                          <td data-label='occurred_at'>
                            {dataFormatter.dateTimeFormatter(item.occurred_at)}
                          </td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!button_links?.analytics_events_link?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <BaseDivider />

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

Button_linksView.getLayout = function getLayout(page: ReactElement) {
  return (
    <LayoutAuthenticated permission={'READ_BUTTON_LINKS'}>
      {page}
    </LayoutAuthenticated>
  );
};

export default Button_linksView;
