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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>URLdaImagem</p>
            <p>{frames_imagem?.imagem_url}</p>
          </div>

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Inspeção</p>

            <p>{frames_imagem?.inspecao?.tipo_inspecao ?? 'No data'}</p>
          </div>

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

            <p>{frames_imagem?.clientes?.name ?? 'No data'}</p>
          </div>

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

                      <th>PosiçãoAproximada</th>

                      <th>EPIsVisíveis</th>

                      <th>FoconoTrabalho</th>

                      <th>OperandoMáquina</th>
                    </tr>
                  </thead>
                  <tbody>
                    {frames_imagem.pessoas_frame_frame &&
                      Array.isArray(frames_imagem.pessoas_frame_frame) &&
                      frames_imagem.pessoas_frame_frame.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/pessoas_frame/pessoas_frame-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='atividade'>{item.atividade}</td>

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

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

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

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

          <BaseDivider />

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

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

export default Frames_imagemView;
