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'}>CategoryName</p>
                    <p>{item_categories?.category_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <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>ItemName</th>
                                
                                
                                
                                <th>ItemCode</th>
                                
                                
                                
                                <th>ItemType</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>LotTracked</th>
                                
                                
                                
                                <th>SerialTracked</th>
                                
                                
                                
                                <th>StandardCost</th>
                                
                                
                                
                                <th>DefaultPurchasePrice</th>
                                
                                
                                
                                <th>DefaultSalePrice</th>
                                
                                
                                
                                <th>ShelfLife(Days)</th>
                                
                                
                                
                                <th>ReorderPoint</th>
                                
                                
                                
                                <th>ReorderQuantity</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                                
                                <th>Description</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="item_name">
                                        { item.item_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="item_code">
                                        { item.item_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="item_type">
                                        { item.item_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="lot_tracked">
                                        { dataFormatter.booleanFormatter(item.lot_tracked) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="serial_tracked">
                                        { dataFormatter.booleanFormatter(item.serial_tracked) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="standard_cost">
                                        { item.standard_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="default_purchase_price">
                                        { item.default_purchase_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="default_sale_price">
                                        { item.default_sale_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="shelf_life_days">
                                        { item.shelf_life_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reorder_point">
                                        { item.reorder_point }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reorder_qty">
                                        { item.reorder_qty }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="active">
                                        { dataFormatter.booleanFormatter(item.active) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </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;