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/items/itemsSlice'
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 ItemsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { items } = useAppSelector((state) => state.items)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ItemType</p>
                    <p>{items?.item_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

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

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Weight</p>
                  <p>{items?.weight || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsStackable'>
                    <SwitchField
                      field={{name: 'is_stackable', value: items?.is_stackable}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MaxStackSize</p>
                  <p>{items?.max_stack_size || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsPerishable'>
                    <SwitchField
                      field={{name: 'is_perishable', value: items?.is_perishable}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ShelfLifeDays</p>
                  <p>{items?.shelf_life_days || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quest_objectives TargetItem</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ObjectiveType</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>TargetQuantity</th>
                                
                                
                                
                                <th>SequenceOrder</th>
                                
                                
                                
                                <th>IsOptional</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {items.quest_objectives_target_item && Array.isArray(items.quest_objectives_target_item) &&
                              items.quest_objectives_target_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quest_objectives/quest_objectives-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="objective_type">
                                        { item.objective_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="target_quantity">
                                        { item.target_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sequence_order">
                                        { item.sequence_order }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_optional">
                                        { dataFormatter.booleanFormatter(item.is_optional) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!items?.quest_objectives_target_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Store_products Item</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>CurrentPrice</th>
                                
                                
                                
                                <th>PricingMode</th>
                                
                                
                                
                                <th>ReorderPoint</th>
                                
                                
                                
                                <th>TargetStock</th>
                                
                                
                                
                                <th>IsListed</th>
                                
                                
                                
                                <th>TaxRate</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {items.store_products_item && Array.isArray(items.store_products_item) &&
                              items.store_products_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/store_products/store_products-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="current_price">
                                        { item.current_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pricing_mode">
                                        { item.pricing_mode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reorder_point">
                                        { item.reorder_point }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_stock">
                                        { item.target_stock }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_listed">
                                        { dataFormatter.booleanFormatter(item.is_listed) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_rate">
                                        { item.tax_rate }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!items?.store_products_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Purchase_order_lines Item</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>UnitCost</th>
                                
                                
                                
                                <th>LineTotal</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {items.purchase_order_lines_item && Array.isArray(items.purchase_order_lines_item) &&
                              items.purchase_order_lines_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/purchase_order_lines/purchase_order_lines-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity_ordered">
                                        { item.quantity_ordered }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_cost">
                                        { item.unit_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="line_total">
                                        { item.line_total }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!items?.purchase_order_lines_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Price_modifiers Item</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Cause</th>
                                
                                
                                
                                <th>Multiplier</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {items.price_modifiers_item && Array.isArray(items.price_modifiers_item) &&
                              items.price_modifiers_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/price_modifiers/price_modifiers-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="cause">
                                        { item.cause }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="multiplier">
                                        { item.multiplier }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!items?.price_modifiers_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Crops SeedItem</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>GrowthDays</th>
                                
                                
                                
                                <th>RegrowthDays</th>
                                
                                
                                
                                <th>IsPerennial</th>
                                
                                
                                
                                <th>PreferredSeason</th>
                                
                                
                                
                                <th>WaterPerDay</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {items.crops_seed_item && Array.isArray(items.crops_seed_item) &&
                              items.crops_seed_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/crops/crops-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="growth_days">
                                        { item.growth_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="regrowth_days">
                                        { item.regrowth_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_perennial">
                                        { dataFormatter.booleanFormatter(item.is_perennial) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="preferred_season">
                                        { item.preferred_season }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="water_per_day">
                                        { item.water_per_day }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!items?.crops_seed_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                <>
                    <p className={'block font-bold mb-2'}>Crops HarvestItem</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>GrowthDays</th>
                                
                                
                                
                                <th>RegrowthDays</th>
                                
                                
                                
                                <th>IsPerennial</th>
                                
                                
                                
                                <th>PreferredSeason</th>
                                
                                
                                
                                <th>WaterPerDay</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {items.crops_harvest_item && Array.isArray(items.crops_harvest_item) &&
                              items.crops_harvest_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/crops/crops-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="growth_days">
                                        { item.growth_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="regrowth_days">
                                        { item.regrowth_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_perennial">
                                        { dataFormatter.booleanFormatter(item.is_perennial) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="preferred_season">
                                        { item.preferred_season }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="water_per_day">
                                        { item.water_per_day }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!items?.crops_harvest_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Fish_species Item</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>Rarity</th>
                                
                                
                                
                                <th>ActiveTime</th>
                                
                                
                                
                                <th>PreferredWeather</th>
                                
                                
                                
                                <th>BaseBiteRate</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {items.fish_species_item && Array.isArray(items.fish_species_item) &&
                              items.fish_species_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/fish_species/fish_species-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="rarity">
                                        { item.rarity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="active_time">
                                        { item.active_time }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="preferred_weather">
                                        { item.preferred_weather }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="base_bite_rate">
                                        { item.base_bite_rate }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!items?.fish_species_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Crafting_recipes OutputItem</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>RecipeType</th>
                                
                                
                                
                                
                                
                                <th>OutputQuantity</th>
                                
                                
                                
                                <th>CraftTimeSeconds</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {items.crafting_recipes_output_item && Array.isArray(items.crafting_recipes_output_item) &&
                              items.crafting_recipes_output_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/crafting_recipes/crafting_recipes-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recipe_type">
                                        { item.recipe_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="output_quantity">
                                        { item.output_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="craft_time_seconds">
                                        { item.craft_time_seconds }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!items?.crafting_recipes_output_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Recipe_ingredients Item</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {items.recipe_ingredients_item && Array.isArray(items.recipe_ingredients_item) &&
                              items.recipe_ingredients_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/recipe_ingredients/recipe_ingredients-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!items?.recipe_ingredients_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default ItemsView;