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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ColorLabel</p>
                    <p>{league_teams?.color_label}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>League_team_players LeagueTeam</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>JoinedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {league_teams.league_team_players_league_team && Array.isArray(league_teams.league_team_players_league_team) &&
                              league_teams.league_team_players_league_team.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/league_team_players/league_team_players-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="joined_at">
                                        { dataFormatter.dateTimeFormatter(item.joined_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!league_teams?.league_team_players_league_team?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>League_standings LeagueTeam</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>
                            {league_teams.league_standings_league_team && Array.isArray(league_teams.league_standings_league_team) &&
                              league_teams.league_standings_league_team.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>
                        {!league_teams?.league_standings_league_team?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                

                <BaseDivider />

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

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

export default League_teamsView;