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/games/gamesSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";


const GamesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { games } = useAppSelector((state) => state.games)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Club</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{games?.club?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>League</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{games?.league?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Venue</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{games?.venue?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>GameTitle</p>
                    <p>{games?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>GameType</p>
                    <p>{games?.game_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{games?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='StartTime'>
                    {games.start_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={games.start_at ?
                        new Date(
                          dayjs(games.start_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No StartTime</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='EndTime'>
                    {games.end_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={games.end_at ?
                        new Date(
                          dayjs(games.end_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No EndTime</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TargetHeadcount</p>
                  <p>{games?.target_headcount || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TeamCount</p>
                  <p>{games?.team_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>FieldCost</p>
                  <p>{games?.field_cost || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='MessengerBroadcastSent'>
                    <SwitchField
                      field={{name: 'messenger_broadcast_sent', value: games?.messenger_broadcast_sent}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='FinalizedAt'>
                    {games.finalized_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={games.finalized_at ?
                        new Date(
                          dayjs(games.finalized_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No FinalizedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_rsvps Game</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>RSVPStatus</th>
                                
                                
                                
                                <th>RespondedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {games.game_rsvps_game && Array.isArray(games.game_rsvps_game) &&
                              games.game_rsvps_game.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_rsvps/game_rsvps-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="rsvp_status">
                                        { item.rsvp_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="responded_at">
                                        { dataFormatter.dateTimeFormatter(item.responded_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!games?.game_rsvps_game?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_guests Game</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Nickname</th>
                                
                                
                                
                                <th>RSVPStatus</th>
                                
                                
                                
                                <th>AttendanceStatus</th>
                                
                                
                                
                                <th>PaymentStatus</th>
                                
                                
                                
                                <th>SeedTierForBalance</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {games.game_guests_game && Array.isArray(games.game_guests_game) &&
                              games.game_guests_game.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_guests/game_guests-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="nickname">
                                        { item.nickname }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rsvp_status">
                                        { item.rsvp_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="attendance_status">
                                        { item.attendance_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_status">
                                        { item.payment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="seed_tier_for_balance">
                                        { item.seed_tier_for_balance }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!games?.game_guests_game?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_teams Game</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>TeamName</th>
                                
                                
                                
                                <th>ColorLabel</th>
                                
                                
                                
                                <th>SortOrder</th>
                                
                                
                                
                                <th>FinalScore</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {games.game_teams_game && Array.isArray(games.game_teams_game) &&
                              games.game_teams_game.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_teams/game_teams-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="color_label">
                                        { item.color_label }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sort_order">
                                        { item.sort_order }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="final_score">
                                        { item.final_score }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!games?.game_teams_game?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_attendances Game</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>AttendanceStatus</th>
                                
                                
                                
                                <th>RecordedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {games.game_attendances_game && Array.isArray(games.game_attendances_game) &&
                              games.game_attendances_game.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_attendances/game_attendances-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="attendance_status">
                                        { item.attendance_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recorded_at">
                                        { dataFormatter.dateTimeFormatter(item.recorded_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!games?.game_attendances_game?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_player_stats Game</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Goals</th>
                                
                                
                                
                                <th>Assists</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {games.game_player_stats_game && Array.isArray(games.game_player_stats_game) &&
                              games.game_player_stats_game.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_player_stats/game_player_stats-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="goals">
                                        { item.goals }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="assists">
                                        { item.assists }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!games?.game_player_stats_game?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Game_payments Game</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>FeeAmount</th>
                                
                                
                                
                                <th>PaymentStatus</th>
                                
                                
                                
                                <th>PaymentMethod</th>
                                
                                
                                
                                <th>StripePaymentReference</th>
                                
                                
                                
                                <th>PaidAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {games.game_payments_game && Array.isArray(games.game_payments_game) &&
                              games.game_payments_game.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/game_payments/game_payments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="fee_amount">
                                        { item.fee_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_status">
                                        { item.payment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_method">
                                        { item.payment_method }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stripe_payment_reference">
                                        { item.stripe_payment_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="paid_at">
                                        { dataFormatter.dateTimeFormatter(item.paid_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!games?.game_payments_game?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default GamesView;