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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Timing Id</p>
            <p>{departments?.timing_id}</p>
          </div>

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

                      <th>Rate Type</th>

                      <th>Standard Service</th>

                      <th>Service Text</th>

                      <th>Rate</th>

                      <th>Service Id</th>

                      <th>Service Code</th>

                      <th>Unit Type</th>

                      <th>Cost</th>

                      <th>Qty</th>

                      <th>Currency</th>

                      <th>Rate Gbp</th>

                      <th>Rate Eur</th>

                      <th>Rate Usd</th>

                      <th>Yes Fixed Text</th>

                      <th>No Fixed Text</th>
                    </tr>
                  </thead>
                  <tbody>
                    {departments.default_activities_products_department &&
                      Array.isArray(
                        departments.default_activities_products_department,
                      ) &&
                      departments.default_activities_products_department.map(
                        (item: any) => (
                          <tr
                            key={item.id}
                            onClick={() =>
                              router.push(
                                `/default_activities_products/default_activities_products-view/?id=${item.id}`,
                              )
                            }
                          >
                            <td data-label='name'>{item.name}</td>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          <BaseDivider />

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

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

export default DepartmentsView;
