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/memoriais_descritivos_asbuilt/memoriais_descritivos_asbuiltSlice';
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 Memoriais_descritivos_asbuiltView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { memoriais_descritivos_asbuilt } = useAppSelector(
    (state) => state.memoriais_descritivos_asbuilt,
  );

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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>ArquivoDigital</p>
            <p>{memoriais_descritivos_asbuilt?.arquivo_digital}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>ResponsávelTécnico</p>
            <p>{memoriais_descritivos_asbuilt?.responsavel_tecnico}</p>
          </div>

          <FormField label='ValidadoporCOPASA'>
            <SwitchField
              field={{
                name: 'validado_por_copasa',
                value: memoriais_descritivos_asbuilt?.validado_por_copasa,
              }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

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

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

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

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

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

          <BaseDivider />

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

Memoriais_descritivos_asbuiltView.getLayout = function getLayout(
  page: ReactElement,
) {
  return (
    <LayoutAuthenticated permission={'READ_MEMORIAIS_DESCRITIVOS_ASBUILT'}>
      {page}
    </LayoutAuthenticated>
  );
};

export default Memoriais_descritivos_asbuiltView;
