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";


const ProductsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { products } = useAppSelector((state) => state.products)
    

    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'}>Title</p>
                    <p>{products?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Description</p>
                  {products.description
                    ? <p dangerouslySetInnerHTML={{__html: products.description}}/>
                    : <p>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>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <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'}>Brand</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{products?.brand?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>InventoryQuantity</p>
                  <p>{products?.inventory_quantity || 'No data'}</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'}>RatingAverage</p>
                  <p>{products?.rating_average || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Product_variants Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>VariantName</th>
                                
                                
                                
                                <th>SKU</th>
                                
                                
                                
                                <th>Size</th>
                                
                                
                                
                                <th>Difficulty</th>
                                
                                
                                
                                <th>Price</th>
                                
                                
                                
                                <th>InventoryQuantity</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.product_variants_product && Array.isArray(products.product_variants_product) &&
                              products.product_variants_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/product_variants/product_variants-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="variant_name">
                                        { item.variant_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sku">
                                        { item.sku }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="size">
                                        { item.size }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="difficulty">
                                        { item.difficulty }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price">
                                        { item.price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="inventory_quantity">
                                        { item.inventory_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.product_variants_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Product_color_sets Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>SetName</th>
                                
                                
                                
                                
                                
                                <th>TotalColors</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.product_color_sets_product && Array.isArray(products.product_color_sets_product) &&
                              products.product_color_sets_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/product_color_sets/product_color_sets-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="set_name">
                                        { item.set_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="total_colors">
                                        { item.total_colors }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.product_color_sets_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Product_brush_kits Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>KitName</th>
                                
                                
                                
                                
                                
                                <th>BrushCount</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.product_brush_kits_product && Array.isArray(products.product_brush_kits_product) &&
                              products.product_brush_kits_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/product_brush_kits/product_brush_kits-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="kit_name">
                                        { item.kit_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="brush_count">
                                        { item.brush_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.product_brush_kits_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Reviews Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Rating</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>Content</th>
                                
                                
                                
                                
                                
                                <th>IsVerifiedPurchase</th>
                                
                                
                                
                                <th>IsPublished</th>
                                
                                
                                
                                <th>ReviewedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.reviews_product && Array.isArray(products.reviews_product) &&
                              products.reviews_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/reviews/reviews-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="rating">
                                        { item.rating }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content">
                                        { item.content }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_verified_purchase">
                                        { dataFormatter.booleanFormatter(item.is_verified_purchase) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_published">
                                        { dataFormatter.booleanFormatter(item.is_published) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reviewed_at">
                                        { dataFormatter.dateTimeFormatter(item.reviewed_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.reviews_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Cart_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>LineTotal</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.cart_items_product && Array.isArray(products.cart_items_product) &&
                              products.cart_items_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/cart_items/cart_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_price">
                                        { item.unit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="line_total">
                                        { item.line_total }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.cart_items_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>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>Quantity</th>
                                
                                
                                
                                <th>UnitPrice</th>
                                
                                
                                
                                <th>LineTotal</th>
                                
                                
                                
                                <th>ItemTitleSnapshot</th>
                                
                                
                                
                                <th>VariantSnapshot</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.order_items_product && Array.isArray(products.order_items_product) &&
                              products.order_items_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/order_items/order_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_price">
                                        { item.unit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="line_total">
                                        { item.line_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="item_title_snapshot">
                                        { item.item_title_snapshot }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="variant_snapshot">
                                        { item.variant_snapshot }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.order_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;