<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Gestion des KF</div>
<br>

<?php
include("../config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_constants.php3");
include($BIB."bib_admin.php3");

function print_error($text)
{
	echo "<div><font color=#FF0000>".$text."</font></div><br>";
}


if ($submit == "Valider")
{
	$error =false;
	$Affichage = $Cible;
	
	// 1. On détermine ce qu'on fait (ajout ou maj)
	if (intval($tourId) <= 0)
	{
		$error = true;
		print_error("Vous n'avez pas indiquer le n° de Tour !!! (la session)");
	}
	
	if ($Ajouter == 1)
	{
		echo "On ajoute : ".$Ajout_KF."<br>";
		if ($Ajout_KF == 0)
		{
			print_error("Précisez le nombre de KF à ajouter/retirer");
			$error = true;
		}
		else
		{
			if ($Ajout_KF < 0)
			{
				$Rq = " KF = KF - '".(-$Ajout_KF)."' ";
				$RqPS = " KFextra = KFextra - '".(-$Ajout_KF)."' ";
			}
			else
			{
				$Rq = " KF = KF + '".($Ajout_KF)."' ";
				$RqPS = " KFextra = KFextra + '".($Ajout_KF)."' ";
			}
		}
	}
	elseif ($Ajouter == 2)
	{
//		if (is_int($MAJ_KF) == false)
		if (false)
		{
			echo "maj_kf : ".$MAJ_KF."<br>";
			print_error("Précisez le nombre de KF à mettre");
			$error = true;
		}
		else
		{
			if (!($MAJ_KF > 0))
			{
				print_error("Le nombre de KF doit être positif");
				$error = true;
			}
			else
				$Rq = " KF = '".$MAJ_KF."' ";
		}
	}
	
	if ($error == false)
	{
		switch ($Cible)
		{
			case 1 :
				if (strlen($pseudo) > 0)
					main_db_query("UPDATE Players Set ".$Rq." WHERE Nickname='".$pseudo."'");
				else
					print_error("Indiquez le pseudo du joueur");
				break;
			case 2 :
				if ($playerId > 0)
					main_db_query("UPDATE Players Set ".$Rq." WHERE Id='".$playerId."'");
				else
					print_error("Indiquez l'Id du joueur");
				break;
			case 3 :
				if ($placeStart <= 0)
				{
					print_error("Le début de la fourchette doit être plus grand que 0");
				}
				elseif ($placeEnd < $placeStart)
				{
					print_error("Le haut de la fourchette doit être au dessus du bas !");
				}
				else
				{
					//$players = main_db_query("SELECT Id FROM Players ORDER By Score DESC Limit ".($placeStart-1).", ".($placeEnd - $placeStart+1));
					if ($dateTour == "")
						$tourDate = db_fetch_array(main_db_query("SELECT MAX(Date) AS Date FROM PlayerScores WHERE TourId='".$tourId."'"));
					else
						$tourDate['Date'] = $dateTour;
						
					$areq = "SELECT PlayerId, Id FROM PlayerScores WHERE PlayerScores.TourId='".$tourId."' AND PlayerScores.Date='".$tourDate['Date']."' ORDER By Score DESC Limit ".($placeStart-1).", ".($placeEnd - $placeStart+1);
					//$areq = "SELECT PlayerId, Id FROM PlayerScores WHERE PlayerScores.TourId='".$tourId."' AND PlayerScores.Date='".$tourDate['Date']."' ORDER By Id DESC Limit ".($placeStart-1).", ".($placeEnd - $placeStart+1);
					$players = main_db_query($areq);
					echo $areq;
					while ($CurPlayer = db_fetch_array($players))
					{
						main_db_query("UPDATE Players Set ".$Rq." WHERE Id='".$CurPlayer['PlayerId']."'");
						main_db_query("UPDATE PlayerScores Set ".$RqPS." WHERE Id='".$CurPlayer['Id']."'");
					}
				}
				break;
			case 4 :
				break;
		}
	}
	else
		echo "erreur !";
}

switch ($Affichage)
{
	case 1 :
		$SelectCritere = " WHERE NickName='".$pseudo."' ";
		break;
	case 2 :
		$SelectCritere = " WHERE Id='".$playerId."' ";
		break;
	case 3 :
		$SelectCritere = "ORDER By Score DESC Limit ".($placeStart-1).", ".($placeEnd - $placeStart+1);
		break;
	case 4 :
		$SelectCritere = "";
		break;
	default :
		$SelectCritere = " WHERE Id=0";
		
}	

$Requ = "SELECT Id, NickName, KF, Score, MemberId FROM Players ".$SelectCritere;
$rows = main_db_query("SELECT Id, NickName, KF, Score, MemberId FROM Players ".$SelectCritere);
?>

<table border=1>
<tr class=trTitle>
			<td>Id</td>
			<td>Pseudo</td>
			<td>KF</td>
			<td>Score</td>
			<td>MemberId</td>
		</tr>

<?php

while ($row = db_fetch_array($rows))
{
	echo "<tr>";
	echo "<td>".$row['Id']."</td>";
	echo "<td>".$row['NickName']."</td>";
	echo "<td>".$row['KF']."</td>";
	echo "<td>".$row['Score']."</td>";
	echo "<td>".$row['MemberId']."</td>";
	echo "</tr>";
}

?>

</table>
<br><hr><br>




<form>
Afficher : <br><br>
<input type="radio" name="Affichage" <?php if ($Affichage==1) echo "checked";?>  value="1">&nbsp;Le joueur de pseudo : <input type=text name=pseudo value="<?php echo $pseudo?>">
<br>&nbsp;Ou&nbsp;<input type="radio" name="Affichage" <?php if ($Affichage==2) echo "checked";?> value="2">&nbsp;Le joueur d'id : <input type=text name=playerId value="<?php echo $playerId?>">
<br>&nbsp;Ou&nbsp;<input type="radio" name="Affichage" <?php if ($Affichage==3) echo "checked";?> value="3">&nbsp;Les joueurs classés entre la  <input type=text name=placeStart value="<?php echo $placeStart?>">e place et la <input type=text name=placeEnd value="<?php echo $placeEnd?>">e place (incluses)
<br>&nbsp;Ou&nbsp;<input type="radio" name="Affichage" <?php if ($Affichage==4) echo "checked";?> value="4">&nbsp;Tous les joueurs

<br><br><input type=submit name=submit value="Afficher">

</form>
<br><hr>
Ou mettre à jour :<br><br>
<form>
<table border=0 cellpadding=4 cellspacing=4 class=univers>
	<tr class=trTitle>
		<td align=center colspan=9>Liste des Tours</td>
	</tr>
	<tr class=trTitle>
		<td >N°</td>
		<td >Nom</td>	
		<td >ShowTime</td>		
		<td >EndTime</td>
		<td >Done</td>		
	</tr>
<?php
# on affiche les matchs dont les résultats on été rentrés pour tel category
$TourRows = main_db_query("SELECT * FROM Tours ORDER By ShowTime ASC");

while($TourRow = db_fetch_array($TourRows))
{
	$color = ($r % 2 == 0)? "#FFFFFF":"#E8E8E8";
	$r++;
	echo "<tr bgcolor=".$color.">";
	echo "<td>".$TourRow['Id']."</td>";
	echo "<td>".$TourRow['Name']."</td>";
	echo "<td>".$TourRow['ShowTime']."</td>";
	echo "<td>".$TourRow['EndTime']."</td>";
	echo "<td>".$TourRow['Done']."</td>";
	echo "</tr>";
}

?>
</table>
1. Choix de la session : 
<br>TourId : <input type=text name=tourId value="<?php echo $tourId ?>"> &nbsp;(option) date dans le Tour : <input type=text name=dateTour value="<?php echo $dateTour ?>">
<br><br>
2. Choisissez l'action : <br>
<input type="radio" name="Ajouter" checked value="1">&nbsp;Ajouter&nbsp;<input type=text name=Ajout_KF value="<?php echo $Ajout_KF?>"> (mettre un chiffre négatif pour en enlever)
<!--<br>&nbsp;Ou&nbsp;<input type="radio" name="Ajouter" value="2">&nbsp;Mettre à &nbsp;<input type=text name=MAJ_KF value="<?php echo $MAJ_KF?>">-->
<br><br>
3. Choisissez la cible :<br>
<!--<input type="radio" name="Cible" <?php if ($Cible==1) echo "checked";?> value="1">&nbsp;Le joueur de pseudo : <input type=text name=pseudo value="<?php echo $pseudo?>">
<br>&nbsp;Ou&nbsp;<input type="radio" name="Cible" <?php if ($Cible==2) echo "checked";?> value="2">&nbsp;Le joueur d'id : <input type=text name=playerId value="<?php echo $playerId?>">-->
<br>&nbsp;Ou&nbsp;<input type="radio" name="Cible" <?php if ($Cible==3) echo "checked";?> value="3">&nbsp;Les joueurs classés entre la  <input type=text name=placeStart value="<?php echo $placeStart?>">e place et la <input type=text name=placeEnd value="<?php echo $placeEnd?>">e place (incluses)
<!--<br>&nbsp;Ou&nbsp;<input type="radio" name="Cible" <?php if ($Cible==4) echo "checked";?> value="4">&nbsp;Tous les joueurs-->

<br><br><input type=submit name=submit value="Valider">

</form>
</body>
</html>
