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/boxes/boxesSlice'
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 BoxesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { boxes } = useAppSelector((state) => state.boxes)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BoxType</p>
                    <p>{boxes?.box_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Price(YUX)</p>
                  <p>{boxes?.price_yux || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsAvailable'>
                    <SwitchField
                      field={{name: 'is_available', value: boxes?.is_available}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Box_items Box</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>
                            {boxes.box_items_box && Array.isArray(boxes.box_items_box) &&
                              boxes.box_items_box.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>
                        {!boxes?.box_items_box?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Purchases Box</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>
                            {boxes.purchases_box && Array.isArray(boxes.purchases_box) &&
                              boxes.purchases_box.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>
                        {!boxes?.purchases_box?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Daily_rewards Box</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>
                            {boxes.daily_rewards_box && Array.isArray(boxes.daily_rewards_box) &&
                              boxes.daily_rewards_box.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>
                        {!boxes?.daily_rewards_box?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default BoxesView;