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/categories/categoriesSlice'
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 CategoriesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { categories } = useAppSelector((state) => state.categories)
    

    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 categories')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View categories')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/categories/categories-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CategoryName</p>
                    <p>{categories?.category_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Description</p>
                  {categories.description
                    ? <p dangerouslySetInnerHTML={{__html: categories.description}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Featured'>
                    <SwitchField
                      field={{name: 'featured', value: categories?.featured}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SortOrder</p>
                  <p>{categories?.sort_order || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Active'>
                    <SwitchField
                      field={{name: 'active', value: categories?.active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ParentCategory</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{categories?.parent_category?.category_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Products Category</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>ProductName</th>
                                
                                
                                
                                <th>Slug</th>
                                
                                
                                
                                <th>ShortDescription</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Price</th>
                                
                                
                                
                                <th>CompareAtPrice</th>
                                
                                
                                
                                <th>TrackInventory</th>
                                
                                
                                
                                <th>StockQuantity</th>
                                
                                
                                
                                <th>SKU</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>NewBadge</th>
                                
                                
                                
                                <th>DiscountPercent</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {categories.products_category && Array.isArray(categories.products_category) &&
                              categories.products_category.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/products/products-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="product_name">
                                        { item.product_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="slug">
                                        { item.slug }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="short_description">
                                        { item.short_description }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="price">
                                        { item.price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="compare_at_price">
                                        { item.compare_at_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="track_inventory">
                                        { dataFormatter.booleanFormatter(item.track_inventory) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stock_quantity">
                                        { item.stock_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sku">
                                        { item.sku }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_new">
                                        { dataFormatter.booleanFormatter(item.is_new) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="discount_percent">
                                        { item.discount_percent }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!categories?.products_category?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Seo_entries Category</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>TargetType</th>
                                
                                
                                
                                <th>TargetSlug</th>
                                
                                
                                
                                <th>MetaTitle</th>
                                
                                
                                
                                <th>MetaDescription</th>
                                
                                
                                
                                <th>CanonicalURL</th>
                                
                                
                                
                                <th>Indexable</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {categories.seo_entries_category && Array.isArray(categories.seo_entries_category) &&
                              categories.seo_entries_category.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/seo_entries/seo_entries-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="target_type">
                                        { item.target_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_slug">
                                        { item.target_slug }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="meta_title">
                                        { item.meta_title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="meta_description">
                                        { item.meta_description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="canonical_url">
                                        { item.canonical_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="indexable">
                                        { dataFormatter.booleanFormatter(item.indexable) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!categories?.seo_entries_category?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default CategoriesView;