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/leagues/leaguesSlice'
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 LeaguesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { leagues } = useAppSelector((state) => state.leagues)
    
    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 leagues')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View leagues')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/leagues/leagues-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SeasonLabel</p>
                    <p>{leagues?.season_label}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='RostersLocked'>
                    <SwitchField
                      field={{name: 'rosters_locked', value: leagues?.rosters_locked}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>RegistrationFee</p>
                  <p>{leagues?.registration_fee || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>League_teams League</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>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {leagues.league_teams_league && Array.isArray(leagues.league_teams_league) &&
                              leagues.league_teams_league.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/league_teams/league_teams-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="color_label">
                                        { item.color_label }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!leagues?.league_teams_league?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Games League</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>GameTitle</th>
                                
                                
                                
                                <th>GameType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>StartTime</th>
                                
                                
                                
                                <th>EndTime</th>
                                
                                
                                
                                <th>TargetHeadcount</th>
                                
                                
                                
                                <th>TeamCount</th>
                                
                                
                                
                                <th>FieldCost</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                <th>MessengerBroadcastSent</th>
                                
                                
                                
                                <th>FinalizedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {leagues.games_league && Array.isArray(leagues.games_league) &&
                              leagues.games_league.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/games/games-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="game_type">
                                        { item.game_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="start_at">
                                        { dataFormatter.dateTimeFormatter(item.start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_at">
                                        { dataFormatter.dateTimeFormatter(item.end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_headcount">
                                        { item.target_headcount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="team_count">
                                        { item.team_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="field_cost">
                                        { item.field_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="messenger_broadcast_sent">
                                        { dataFormatter.booleanFormatter(item.messenger_broadcast_sent) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finalized_at">
                                        { dataFormatter.dateTimeFormatter(item.finalized_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!leagues?.games_league?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>League_payments League</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>AmountPaid</th>
                                
                                
                                
                                <th>PaymentStatus</th>
                                
                                
                                
                                <th>LastPaidAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {leagues.league_payments_league && Array.isArray(leagues.league_payments_league) &&
                              leagues.league_payments_league.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/league_payments/league_payments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="fee_amount">
                                        { item.fee_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount_paid">
                                        { item.amount_paid }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_status">
                                        { item.payment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_paid_at">
                                        { dataFormatter.dateTimeFormatter(item.last_paid_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!leagues?.league_payments_league?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>League_standings League</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Played</th>
                                
                                
                                
                                <th>Wins</th>
                                
                                
                                
                                <th>Draws</th>
                                
                                
                                
                                <th>Losses</th>
                                
                                
                                
                                <th>GoalsFor</th>
                                
                                
                                
                                <th>GoalsAgainst</th>
                                
                                
                                
                                <th>GoalDifference</th>
                                
                                
                                
                                <th>Points</th>
                                
                                
                                
                                <th>Rank</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {leagues.league_standings_league && Array.isArray(leagues.league_standings_league) &&
                              leagues.league_standings_league.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/league_standings/league_standings-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="played">
                                        { item.played }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="wins">
                                        { item.wins }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="draws">
                                        { item.draws }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="losses">
                                        { item.losses }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="goals_for">
                                        { item.goals_for }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="goals_against">
                                        { item.goals_against }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="goal_difference">
                                        { item.goal_difference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="points">
                                        { item.points }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rank">
                                        { item.rank }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!leagues?.league_standings_league?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>League_finish_awards League</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>FinishRank</th>
                                
                                
                                
                                <th>BonusPoints</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {leagues.league_finish_awards_league && Array.isArray(leagues.league_finish_awards_league) &&
                              leagues.league_finish_awards_league.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/league_finish_awards/league_finish_awards-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="finish_rank">
                                        { item.finish_rank }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bonus_points">
                                        { item.bonus_points }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!leagues?.league_finish_awards_league?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default LeaguesView;