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

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

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

            <p>{brands?.distillery?.name ?? 'No data'}</p>
          </div>

          <>
            <p className={'block font-bold mb-2'}>Bottles Brand</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>Proof</th>

                      <th>Type</th>

                      <th>MSRPRange</th>

                      <th>SecondaryValueRange</th>

                      <th>OpenedBottleIndicator</th>

                      <th>Quantity</th>

                      <th>Barcode</th>

                      <th>Age</th>
                    </tr>
                  </thead>
                  <tbody>
                    {brands.bottles_brand &&
                      Array.isArray(brands.bottles_brand) &&
                      brands.bottles_brand.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/bottles/bottles-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='name'>{item.name}</td>

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

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

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

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

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

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

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

                          <td data-label='age'>{item.age}</td>
                        </tr>
                      ))}
                  </tbody>
                </table>
              </div>
              {!brands?.bottles_brand?.length && (
                <div className={'text-center py-4'}>No data</div>
              )}
            </CardBox>
          </>

          <BaseDivider />

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

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

export default BrandsView;
