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/cosmetics/cosmeticsSlice'
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 CosmeticsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { cosmetics } = useAppSelector((state) => state.cosmetics)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CosmeticType</p>
                    <p>{cosmetics?.cosmetic_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Rarity</p>
                    <p>{cosmetics?.rarity ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AssetKey</p>
                    <p>{cosmetics?.asset_key}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsLimitedTime'>
                    <SwitchField
                      field={{name: 'is_limited_time', value: cosmetics?.is_limited_time}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Box_items Cosmetic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>ItemKind</th>
                                
                                
                                
                                <th>Rarity</th>
                                
                                
                                
                                <th>DropRatePercent</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {cosmetics.box_items_cosmetic && Array.isArray(cosmetics.box_items_cosmetic) &&
                              cosmetics.box_items_cosmetic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/box_items/box_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="item_kind">
                                        { item.item_kind }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rarity">
                                        { item.rarity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="drop_rate_percent">
                                        { item.drop_rate_percent }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!cosmetics?.box_items_cosmetic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Purchases Cosmetic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>PurchaseType</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Amount(YUX)</th>
                                
                                
                                
                                <th>PurchasedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {cosmetics.purchases_cosmetic && Array.isArray(cosmetics.purchases_cosmetic) &&
                              cosmetics.purchases_cosmetic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/purchases/purchases-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="purchase_type">
                                        { item.purchase_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="amount_yux">
                                        { item.amount_yux }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="purchased_at">
                                        { dataFormatter.dateTimeFormatter(item.purchased_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!cosmetics?.purchases_cosmetic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inventory_items Cosmetic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ItemKind</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                                
                                <th>UnlockedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {cosmetics.inventory_items_cosmetic && Array.isArray(cosmetics.inventory_items_cosmetic) &&
                              cosmetics.inventory_items_cosmetic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/inventory_items/inventory_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="item_kind">
                                        { item.item_kind }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unlocked_at">
                                        { dataFormatter.dateTimeFormatter(item.unlocked_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!cosmetics?.inventory_items_cosmetic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Daily_rewards Cosmetic</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>DailyRewardName</th>
                                
                                
                                
                                <th>RewardType</th>
                                
                                
                                
                                <th>DayIndex</th>
                                
                                
                                
                                <th>YUXAmount</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>IsEnabled</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {cosmetics.daily_rewards_cosmetic && Array.isArray(cosmetics.daily_rewards_cosmetic) &&
                              cosmetics.daily_rewards_cosmetic.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/daily_rewards/daily_rewards-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reward_type">
                                        { item.reward_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="day_index">
                                        { item.day_index }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="yux_amount">
                                        { item.yux_amount }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_enabled">
                                        { dataFormatter.booleanFormatter(item.is_enabled) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!cosmetics?.daily_rewards_cosmetic?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default CosmeticsView;