<?php
header('Content-Type: text/html; charset=UTF-8');
error_reporting(0);
include("data/server.php");

if (!isset( $_GET["team"])){return;}
$id = $_GET["team"];


$sql = "SELECT group_id,display,standings_url, league FROM nuliga WHERE team_id = ".$id;
$result = $conn->query($sql);
$info = $result->fetch_assoc();

if (!$result || $info == null){
    echo "Ungültige Team ID";
    return;
}

$sql = "SELECT * FROM matches WHERE team_id =".$id." ORDER BY date"; 
$result = $conn->query($sql);

if ($result->num_rows < 1){return;}
$games = array();
while($row = $result->fetch_assoc()){
    $games[]=$row;
}




//$sql = "SELECT s.*, n.display FROM standings s, (SELECT group_id,display,standings_url,league from nuliga WHERE team_id = ".$id.") n WHERE s.group_id = n.group_id ORDER BY pos";
$sql = "SELECT s.* FROM standings s WHERE s.group_id = ".$info["group_id"]." ORDER BY pos";
$result = $conn->query($sql);
$standings = array();
while($row = $result->fetch_assoc()){
    $standings[]=$row;
}


$conn->close();



?>

<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="widget.css"/>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="script.js"></script>
</head>
<body>
    <h1><?=$info["league"]?></h1>
    <a class="nuliga" href="<?=$info["standings_url"]?>" target="_blank">nuLiga</a>
    <div style="clear: both"></div>
    <ul class="header">
        <li data="standings">Tabelle</li>
        <li data="results">Ergebnisse</li>
        <li data="matches" >Spielplan</li>
    </ul>
    
    <div class="table-container">
    <table class="results">
        <tbody>        
            <?php foreach (array_reverse($games) as $game){
                if ($game["score"] == "") {continue;}       
            ?>
                 <tr>
                     <td class="team_home"><?=trim($game["team_home"])?></td>
                     <td class="score <?php if ($game['win']) echo 'win';?>"><?=$game["score"]?></td>
                     <td class="team_away"><?=trim($game["team_away"])?></td>
                </tr>               
                    
            <?php }?>       
        </tbody>
    </table>
    </div>
    
    
    <table class="matches">
        <tbody> 
            <?php foreach ($games as $game){
                if ($game["score"] != "") {continue;}

                $date = date ('d.m.Y H:i',strtotime($game["date"]));
            ?>
                 <tr>
                     <td class="team_home"><?=trim($game["team_home"])?></td>
                     <td class="date"><span><?=$date?></span></td>
                     <td class="team_away"><?=trim($game["team_away"])?></td>
                </tr>
            <?php }?>
        </tbody>
    </table>

<table class="standings">
 <tbody>
            <?php foreach ($standings as $pos){?>
                 <tr class="<?php if($pos['team'] == $info["display"]) echo 'highlight';?>">
                     <td class="pos"><?=$pos["pos"]?>.</td>
                     <td class="team"><?=trim($pos["team"])?></td>
                     <td class="points"><?=$pos["points"]?></td>
                     <td class="goals"><?=$pos["goals"]?></td>
                </tr>
            <?php }?>
        </tbody>
    </table>
</body>
</html>

