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

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Phone Number</p>
            <p>{shipments?.phoneNumber}</p>
          </div>

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

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Address 2</p>
            <p>{shipments?.address2}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Zip Code</p>
            <p>{shipments?.zipCode}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>City</p>
            <p>{shipments?.city}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>State</p>
            <p>{shipments?.state}</p>
          </div>

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

                      <th>Weight</th>

                      <th>Length</th>

                      <th>Width</th>

                      <th>Height</th>
                    </tr>
                  </thead>
                  <tbody>
                    {shipments.products &&
                      Array.isArray(shipments.products) &&
                      shipments.products.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(
                              `/products/products-view/?id=${item.id}`,
                            )
                          }
                        >
                          <td data-label='productName'>{item.productName}</td>

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

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

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

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

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

            <p>{shipments?.quote?.carrierName ?? 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Customer Charge</p>
            <p>{shipments?.customerCharge || 'No data'}</p>
          </div>

          <div className={'mb-4'}>
            <p className={'block font-bold mb-2'}>Actual Charge</p>
            <p>{shipments?.actualCharge || 'No data'}</p>
          </div>

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

                      <th>Quote Price</th>
                    </tr>
                  </thead>
                  <tbody>
                    {shipments.quotes_shipment &&
                      Array.isArray(shipments.quotes_shipment) &&
                      shipments.quotes_shipment.map((item: any) => (
                        <tr
                          key={item.id}
                          onClick={() =>
                            router.push(`/quotes/quotes-view/?id=${item.id}`)
                          }
                        >
                          <td data-label='carrierName'>{item.carrierName}</td>

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

          <BaseDivider />

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

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

export default ShipmentsView;
