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/avatars/avatarsSlice'
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 AvatarsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { avatars } = useAppSelector((state) => state.avatars)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceType</p>
                    <p>{avatars?.source_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsCollectible'>
                    <SwitchField
                      field={{name: 'is_collectible', value: avatars?.is_collectible}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ModelKey</p>
                    <p>{avatars?.model_key}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DropWeight</p>
                  <p>{avatars?.drop_weight || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Box_items Avatar</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>
                            {avatars.box_items_avatar && Array.isArray(avatars.box_items_avatar) &&
                              avatars.box_items_avatar.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>
                        {!avatars?.box_items_avatar?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inventory_items Avatar</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>
                            {avatars.inventory_items_avatar && Array.isArray(avatars.inventory_items_avatar) &&
                              avatars.inventory_items_avatar.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>
                        {!avatars?.inventory_items_avatar?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default AvatarsView;