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/modelos_bim_asbuilt/modelos_bim_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 Modelos_bim_asbuiltView = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const { modelos_bim_asbuilt } = useAppSelector(
    (state) => state.modelos_bim_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 modelos_bim_asbuilt')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={removeLastCharacter('View modelos_bim_asbuilt')}
          main
        >
          <BaseButton
            color='info'
            label='Edit'
            href={`/modelos_bim_asbuilt/modelos_bim_asbuilt-edit/?id=${id}`}
          />
        </SectionTitleLineWithButton>
        <CardBox>
          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>NomedoModelo</p>
            <p>{modelos_bim_asbuilt?.nome_modelo}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>ArquivodoModelo</p>
            <p>{modelos_bim_asbuilt?.arquivo_modelo}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Formato</p>
            <p>{modelos_bim_asbuilt?.formato}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>PlataformadeOrigem</p>
            <p>{modelos_bim_asbuilt?.plataforma_origem}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>ResponsávelpelaModelagem</p>
            <p>{modelos_bim_asbuilt?.responsavel_modelagem}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>NíveldeDetalhamento</p>
            <p>{modelos_bim_asbuilt?.nivel_detalhamento}</p>
          </div>

          <FormField label='CoordenaçãocomOutrosModelos'>
            <SwitchField
              field={{
                name: 'coordenacao_com_outros_modelos',
                value: modelos_bim_asbuilt?.coordenacao_com_outros_modelos,
              }}
              form={{ setFieldValue: () => null }}
              disabled
            />
          </FormField>

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

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

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

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

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

          <BaseDivider />

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

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

export default Modelos_bim_asbuiltView;
