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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Fabricante</p>
            <p>{cameras_ptz?.fabricante}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>CoordenadaLatitude</p>
            <p>{cameras_ptz?.coordenada_lat || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>CoordenadaLongitude</p>
            <p>{cameras_ptz?.coordenada_lon || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>AlturadeInstalação(m)</p>
            <p>{cameras_ptz?.altura_instalacao_m || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>ÂnguloFocal</p>
            <p>{cameras_ptz?.angulo_focal}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>IPdeAcesso</p>
            <p>{cameras_ptz?.ip_acesso}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>StatusOperacional</p>
            <p>{cameras_ptz?.status_operacional}</p>
          </div>

          <FormField label='DatadeInstalação'>
            {cameras_ptz.data_instalacao ? (
              <DatePicker
                dateFormat='yyyy-MM-dd'
                showTimeSelect
                selected={
                  cameras_ptz.data_instalacao
                    ? new Date(
                        dayjs(cameras_ptz.data_instalacao).format(
                          'YYYY-MM-DD hh:mm',
                        ),
                      )
                    : null
                }
                disabled
              />
            ) : (
              <p>No DatadeInstalação</p>
            )}
          </FormField>

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

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

            <p>{cameras_ptz?.obra?.nome_obra ?? 'No data'}</p>
          </div>

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

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

          <BaseDivider />

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

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

export default Cameras_ptzView;
