<?php

require_once '../includes/php/connexionbdd.php';

$bdd = connexionBD();

$req = $bdd->query("SELECT pseudo, steam_id FROM rl_general;");

$affichageDonnees = "<ul class=\"collapsible\" data-collapsible=\"expandable\">";

while($donnee = $req->fetch())
{
    $pseudo = $donnee['pseudo'];
    $steamid = $donnee['steam_id'];

    $req2 = $bdd->query("SELECT rank, division, rating, matches FROM rl_solo WHERE pseudo = '$pseudo';");

    $affichRanks = "<div class=\"collapsible-body\"><div class=\"row\">";

    while($donnee2 = $req2->fetch())
    {
        $soloRating = $donnee2['rating'];
        $soloMatches = $donnee2['matches'];
        $rank = $donnee2['rank'];
        
        $req3 = $bdd->query("SELECT url FROM rl_img WHERE name = '$rank';");
        $donnee3 = $req3->fetch();
        $url = $donnee3['url'];
        $division = $donnee2['division'];
        
        $affichRanks .= "<div class=\"col s3 solo\"><h5 class=\"titre-rank\">Solo</h5><div class=\"image-rank\"><img src=\"$url\" alt=\"$rank\"></div><div class=\"text-rank\">$division<br/>$soloRating rating<br/>$soloMatches matches</div></div>";
    }
    
    $req2 = $bdd->query("SELECT rank, division, rating, matches FROM rl_doubles WHERE pseudo = '$pseudo';");
    
    while($donnee2 = $req2->fetch())
    {
        $doubleRating = $donnee2['rating'];
        $doubleMatches = $donnee2['matches'];
        $rank = $donnee2['rank'];
        
        $req3 = $bdd->query("SELECT url FROM rl_img WHERE name = '$rank';");
        $donnee3 = $req3->fetch();
        $url = $donnee3['url'];
        $division = $donnee2['division'];
        
        
        $affichRanks .= "<div class=\"col s3 doubles\"><h5 class=\"titre-rank\">Doubles</h5><div class=\"image-rank\"><img src=\"$url\" alt=\"$rank\"></div><div class=\"text-rank\">$division<br/>$doubleRating rating<br/>$doubleMatches matches</div></div>";
    }
    
    $req2 = $bdd->query("SELECT rank, division, rating, matches FROM rl_solo_standard WHERE pseudo = '$pseudo';");
    
    while($donnee2 = $req2->fetch())
    {
        $soloStandardRating = $donnee2['rating'];
        $soloStandardMatches = $donnee2['matches'];
        $rank = $donnee2['rank'];
        
        $req3 = $bdd->query("SELECT url FROM rl_img WHERE name = '$rank';");
        $donnee3 = $req3->fetch();
        $url = $donnee3['url'];
        $division = $donnee2['division'];
        
        
        $affichRanks .= "<div class=\"col s3 solo-standard\"><h5 class=\"titre-rank\">Solo Standard</h5><div class=\"image-rank\"><img src=\"$url\" alt=\"$rank\"></div><div class=\"text-rank\">$division<br/>$soloStandardRating rating<br/>$soloStandardMatches matches</div></div>";
    }
    
    $req2 = $bdd->query("SELECT rank, division, rating, matches FROM rl_standard WHERE pseudo = '$pseudo';");
    
    while($donnee2 = $req2->fetch())
    {
        $standardRating = $donnee2['rating'];
        $standardMatches = $donnee2['matches'];
        $rank = $donnee2['rank'];
        
        $req3 = $bdd->query("SELECT url FROM rl_img WHERE name = '$rank';");
        $donnee3 = $req3->fetch();
        $url = $donnee3['url'];
        $division = $donnee2['division'];
        
        
        $affichRanks .= "<div class=\"col s3 standard\"><h5 class=\"titre-rank\">Standard</h5><div class=\"image-rank\"><img src=\"$url\" alt=\"$rank\"></div><div class=\"text-rank\">$division<br/>$standardRating rating<br/>$standardMatches matches</div></div>";
    }
    
    $moyenne = ($soloRating + $doubleRating + $soloStandardRating + $standardRating) / 4;
    $matches = $soloMatches + $doubleMatches + $soloStandardMatches + $standardMatches;
    
    $pseudo = strtoupper($pseudo);
    $key = "F1E51986EA8980C6145EA5DED117917A";
    $imgfile = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $key . '&steamids=' . $steamid . '&format=json');
    $reponse = json_decode($imgfile, true);
    $imgurl = $reponse['response']['players'][0]['avatar'];
    
    $affichHeader = "<div class=\"collapsible-header\"><ul class=\"collection\"><li class=\"collection-item avatar\"><img src=\"$imgurl\" alt=\"$pseudo\" class=\"circle\"><span class=\"title\">$pseudo</span><p>$moyenne rating moyen<br/>$matches matches totaux</p></li></ul></div>";
    
    $affichTotal = "<li>" . $affichHeader . $affichRanks . "</li>";
    
    $affichageDonnees .= $affichTotal;
}

$affichageDonnees .= "</ul>";

echo $affichageDonnees;

?>