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/badges/badgesSlice'
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 BadgesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { badges } = useAppSelector((state) => state.badges)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>UnlockType</p>
                    <p>{badges?.unlock_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inventory_items Badge</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>
                            {badges.inventory_items_badge && Array.isArray(badges.inventory_items_badge) &&
                              badges.inventory_items_badge.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>
                        {!badges?.inventory_items_badge?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Achievements RewardBadge</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>AchievementName</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                <th>Category</th>
                                
                                
                                
                                <th>TargetValue</th>
                                
                                
                                
                                <th>RewardYUX</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>IsRepeatable</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {badges.achievements_reward_badge && Array.isArray(badges.achievements_reward_badge) &&
                              badges.achievements_reward_badge.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/achievements/achievements-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="category">
                                        { item.category }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_value">
                                        { item.target_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reward_yux">
                                        { item.reward_yux }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="is_repeatable">
                                        { dataFormatter.booleanFormatter(item.is_repeatable) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!badges?.achievements_reward_badge?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Daily_rewards Badge</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>
                            {badges.daily_rewards_badge && Array.isArray(badges.daily_rewards_badge) &&
                              badges.daily_rewards_badge.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>
                        {!badges?.daily_rewards_badge?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default BadgesView;