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

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

            <p>{blocks?.profile_page?.title ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Type</p>
            <p>{blocks?.type ?? 'No data'}</p>
          </div>

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

          <FormField label='Visible'>
            <SwitchField
              field={{ name: 'visible', value: blocks?.visible }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

          <FormField label='Multi Text' hasTextareaHeight>
            <textarea className={'w-full'} disabled value={blocks?.config} />
          </FormField>

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

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

          <>
            <p className={'block font-bold mb-2'}>Button_links Block</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>Icon</th>

                      <th>Enabled</th>

                      <th>OrderIndex</th>

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

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

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

                          <td data-label='enabled'>
                            {dataFormatter.booleanFormatter(item.enabled)}
                          </td>

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

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

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

                      <th>Handle</th>

                      <th>URL</th>

                      <th>OrderIndex</th>
                    </tr>
                  </thead>
                  <tbody>
                    {blocks.social_links_block &&
                      Array.isArray(blocks.social_links_block) &&
                      blocks.social_links_block.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/social_links/social_links-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='network'>{item.network}</td>

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

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

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

          <BaseDivider />

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

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

export default BlocksView;
