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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CategoryKind</p>
                    <p>{item_categories?.category_kind ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Items Category</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Name</th>
                                
                                
                                
                                
                                
                                <th>ItemType</th>
                                
                                
                                
                                
                                
                                <th>BasePrice</th>
                                
                                
                                
                                <th>Weight</th>
                                
                                
                                
                                <th>IsStackable</th>
                                
                                
                                
                                <th>MaxStackSize</th>
                                
                                
                                
                                <th>IsPerishable</th>
                                
                                
                                
                                <th>ShelfLifeDays</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {item_categories.items_category && Array.isArray(item_categories.items_category) &&
                              item_categories.items_category.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/items/items-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="item_type">
                                        { item.item_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="base_price">
                                        { item.base_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="weight">
                                        { item.weight }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_stackable">
                                        { dataFormatter.booleanFormatter(item.is_stackable) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_stack_size">
                                        { item.max_stack_size }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_perishable">
                                        { dataFormatter.booleanFormatter(item.is_perishable) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shelf_life_days">
                                        { item.shelf_life_days }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!item_categories?.items_category?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Item_categoriesView;