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/periods/periodsSlice';
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';

const PeriodsView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { periods } = useAppSelector((state) => state.periods);

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

          <FormField label='Period From'>
            {periods.period_from ? (
              <DatePicker
                dateFormat='yyyy-MM-dd'
                showTimeSelect
                selected={
                  periods.period_from
                    ? new Date(
                        dayjs(periods.period_from).format('YYYY-MM-DD hh:mm'),
                      )
                    : null
                }
                disabled
              />
            ) : (
              <p>No Period From</p>
            )}
          </FormField>

          <FormField label='Period To'>
            {periods.period_to ? (
              <DatePicker
                dateFormat='yyyy-MM-dd'
                showTimeSelect
                selected={
                  periods.period_to
                    ? new Date(
                        dayjs(periods.period_to).format('YYYY-MM-DD hh:mm'),
                      )
                    : null
                }
                disabled
              />
            ) : (
              <p>No Period To</p>
            )}
          </FormField>

          <>
            <p className={'block font-bold mb-2'}>Archival_items Period</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>Description</th>

                      <th>Is Published</th>
                    </tr>
                  </thead>
                  <tbody>
                    {periods.archival_items_period &&
                      Array.isArray(periods.archival_items_period) &&
                      periods.archival_items_period.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/archival_items/archival_items-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='label'>{item.label}</td>

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

                          <td data-label='isPublished'>
                            {dataFormatter.booleanFormatter(item.isPublished)}
                          </td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!periods?.archival_items_period?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <BaseDivider />

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

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

export default PeriodsView;
