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/products/productsSlice'
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 ProductsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { products } = useAppSelector((state) => state.products)
    
    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 products')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View products')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/products/products-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProductName</p>
                    <p>{products?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SKU</p>
                    <p>{products?.sku}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Barcode</p>
                    <p>{products?.barcode}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Images</p>
                    {products?.images?.length
                      ? (
                        <ImageField
                          name={'images'}
                          image={products?.images}
                          className='w-20 h-20'
                        />
                      ) : <p>No Images</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Brand</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{products?.brand?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Category</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{products?.category?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProductType</p>
                    <p>{products?.product_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsTaxable'>
                    <SwitchField
                      field={{name: 'is_taxable', value: products?.is_taxable}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>VATRate</p>
                  <p>{products?.vat_rate || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CostPrice</p>
                  <p>{products?.cost_price || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SellingPrice</p>
                  <p>{products?.selling_price || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ReorderLevel</p>
                  <p>{products?.reorder_level || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='TrackInventory'>
                    <SwitchField
                      field={{name: 'track_inventory', value: products?.track_inventory}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsActive'>
                    <SwitchField
                      field={{name: 'is_active', value: products?.is_active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{products?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Price_list_items Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Price</th>
                                
                                
                                
                                <th>VATRateOverride</th>
                                
                                
                                
                                <th>EffectiveFrom</th>
                                
                                
                                
                                <th>EffectiveTo</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.price_list_items_product && Array.isArray(products.price_list_items_product) &&
                              products.price_list_items_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/price_list_items/price_list_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="price">
                                        { item.price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vat_rate_override">
                                        { item.vat_rate_override }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="effective_from">
                                        { dataFormatter.dateTimeFormatter(item.effective_from) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="effective_to">
                                        { dataFormatter.dateTimeFormatter(item.effective_to) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.price_list_items_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Sale_items Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>ProductName(Snapshot)</th>
                                
                                
                                
                                <th>SKU(Snapshot)</th>
                                
                                
                                
                                <th>Barcode(Snapshot)</th>
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                                
                                <th>UnitPrice</th>
                                
                                
                                
                                <th>LineDiscount</th>
                                
                                
                                
                                <th>VATRate</th>
                                
                                
                                
                                <th>VATAmount</th>
                                
                                
                                
                                <th>LineTotal</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.sale_items_product && Array.isArray(products.sale_items_product) &&
                              products.sale_items_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/sale_items/sale_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="product_name_snapshot">
                                        { item.product_name_snapshot }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sku_snapshot">
                                        { item.sku_snapshot }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="barcode_snapshot">
                                        { item.barcode_snapshot }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_price">
                                        { item.unit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="line_discount">
                                        { item.line_discount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vat_rate">
                                        { item.vat_rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vat_amount">
                                        { item.vat_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="line_total">
                                        { item.line_total }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.sale_items_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Return_items Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                                
                                <th>UnitPrice</th>
                                
                                
                                
                                <th>VATRate</th>
                                
                                
                                
                                <th>VATAmount</th>
                                
                                
                                
                                <th>RefundAmount</th>
                                
                                
                                
                                <th>ConditionNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.return_items_product && Array.isArray(products.return_items_product) &&
                              products.return_items_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/return_items/return_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_price">
                                        { item.unit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vat_rate">
                                        { item.vat_rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vat_amount">
                                        { item.vat_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="refund_amount">
                                        { item.refund_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="condition_notes">
                                        { item.condition_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.return_items_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Purchase_order_items Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>QuantityOrdered</th>
                                
                                
                                
                                <th>QuantityReceived</th>
                                
                                
                                
                                <th>UnitCost</th>
                                
                                
                                
                                <th>VATRate</th>
                                
                                
                                
                                <th>VATAmount</th>
                                
                                
                                
                                <th>LineTotal</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.purchase_order_items_product && Array.isArray(products.purchase_order_items_product) &&
                              products.purchase_order_items_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/purchase_order_items/purchase_order_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity_ordered">
                                        { item.quantity_ordered }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_received">
                                        { item.quantity_received }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_cost">
                                        { item.unit_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vat_rate">
                                        { item.vat_rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vat_amount">
                                        { item.vat_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="line_total">
                                        { item.line_total }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.purchase_order_items_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inventory_items Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>OnHand</th>
                                
                                
                                
                                <th>Reserved</th>
                                
                                
                                
                                <th>Available</th>
                                
                                
                                
                                <th>LastCountedAt</th>
                                
                                
                                
                                <th>LastMovementAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.inventory_items_product && Array.isArray(products.inventory_items_product) &&
                              products.inventory_items_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/inventory_items/inventory_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="on_hand">
                                        { item.on_hand }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reserved">
                                        { item.reserved }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="available">
                                        { item.available }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_counted_at">
                                        { dataFormatter.dateTimeFormatter(item.last_counted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_movement_at">
                                        { dataFormatter.dateTimeFormatter(item.last_movement_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.inventory_items_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Stock_movements Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>MovedAt</th>
                                
                                
                                
                                <th>MovementType</th>
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                                
                                <th>ReferenceCode</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.stock_movements_product && Array.isArray(products.stock_movements_product) &&
                              products.stock_movements_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/stock_movements/stock_movements-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="moved_at">
                                        { dataFormatter.dateTimeFormatter(item.moved_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="movement_type">
                                        { item.movement_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reference_code">
                                        { item.reference_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.stock_movements_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Stock_transfer_items Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>QuantityRequested</th>
                                
                                
                                
                                <th>QuantityShipped</th>
                                
                                
                                
                                <th>QuantityReceived</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.stock_transfer_items_product && Array.isArray(products.stock_transfer_items_product) &&
                              products.stock_transfer_items_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/stock_transfer_items/stock_transfer_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity_requested">
                                        { item.quantity_requested }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_shipped">
                                        { item.quantity_shipped }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_received">
                                        { item.quantity_received }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.stock_transfer_items_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

ProductsView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated
        
        permission={'READ_PRODUCTS'}
        
      >
          {page}
      </LayoutAuthenticated>
    )
}

export default ProductsView;