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/templates/templatesSlice';
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 TemplatesView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { templates } = useAppSelector((state) => state.templates);

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Variant</p>
            <p>{templates?.variant ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Colors</p>
            <p>{templates?.colors}</p>
          </div>

          <FormField label='IsPremium'>
            <SwitchField
              field={{ name: 'is_premium', value: templates?.is_premium }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

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

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

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

                      <th>Title</th>

                      <th>Bio</th>

                      <th>Status</th>

                      <th>PublishedAt</th>

                      <th>OGTitle</th>

                      <th>OGDescription</th>

                      <th>OGImageURL</th>
                    </tr>
                  </thead>
                  <tbody>
                    {templates.profile_pages_theme_template &&
                      Array.isArray(templates.profile_pages_theme_template) &&
                      templates.profile_pages_theme_template.map(
                        (item: any) => (
                          <tr
                            key={item.id}
                            onClick={() =>
                              router.push(
                                `/profile_pages/profile_pages-view/?id=${item.id}`,
                              )
                            }
                          >
                            <td data-label='slug'>{item.slug}</td>

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

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

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

                            <td data-label='published_at'>
                              {dataFormatter.dateTimeFormatter(
                                item.published_at,
                              )}
                            </td>

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

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

                            <td data-label='og_image_url'>
                              {item.og_image_url}
                            </td>
                          </tr>
                        ),
                      )}
                  </tbody>
                </table>
              </div>
              {!templates?.profile_pages_theme_template?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <BaseDivider />

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

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

export default TemplatesView;
