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

?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Les gagnants</div>
<br>
<?php
$gift = addslashes($gift);
$aSession = addslashes($aSession);
switch($submit)
{
	case "Infos":
		$row = db_fetch_array(main_db_query("SELECT *,Winners.Id AS WId FROM Winners LEFT JOIN Players ON Winners.PlayerId=Players.Id WHERE Winners.Id='".$WId."'"));
		$gift = $row[Gift];
		$aDate = $row["Date"];
		$aSession = $row[Session];
		$Ordre = $row[Ordre];
		$NickName=$row[NickName];
		break;

	case "Valider":
		# on recupere le playerId du nickname
		$row = db_fetch_array(main_db_query("SELECT Id FROM Players WHERE NickName='".$NickName."'"));

		if ($WId==0)
		{
			main_db_query("INSERT Winners (PlayerId,Gift,Date,Score,Session,Ordre) VALUES ('".$row[Id]."','".$gift."','".$aDate."','".$row[Score]."','".$aSession."','".$Ordre."')");
		}
		else
		{
			main_db_query("UPDATE Winners SET PlayerId='".$row[Id]."',Gift='".$gift."',Date='".$aDate."',Session='".$aSession."',Ordre='".$Ordre."' WHERE Id='".$WId."'");
		}
		$NickName="";
		$WId="";
		$Ordre+=10;
		break;

	case "Supprimer":
		main_db_query("DELETE FROM Winners WHERE Id='".$WId."'");
		break;
	
	case "Recuperer":
		if ($aDate=="")
			$aDate = date("Y-m-d H:i:s", my_local_time());
			
		$rows = main_db_query("SELECT PlayerId,Score FROM ScoresArchive WHERE Date='".$archiveDate."' ORDER BY Score DESC LIMIT 0, ".$nWinners);
		$r=1;
		while($row = db_fetch_array($rows))
			main_db_query("INSERT Winners (PlayerId,Gift,Date,Score,Session,Ordre)".
				" VALUES ('".$row[PlayerId]."','".$gift."','".$aDate."','".$row[Score]."','".$aSession."','".(10*$r++)."')");
		break;
	
	case "Rafraichir":
		$WId=0;
		$NickName="";
		$gift="";
		$aDate="";
		$aSession="";
		$nWinners="";
}

if ($month!="")
{
	$from="";
	$to="";
	$current_month = $month;
}
else
{
	$current_month = date("m", my_local_time());
}

if ($from=="" or $to=="")
{
	$current_year = date("Y", my_local_time());
	$from = $current_year."-".$current_month."-01 00:00:00";
	$to = $current_year."-".$current_month."-31 00:00:00";
}

$rows = main_db_query("SELECT *,Winners.Id AS WId,Winners.Score AS WScore FROM Winners LEFT JOIN Players ON Winners.PlayerId=Players.Id WHERE Date>'".$from."' AND Date<'".$to."' ORDER BY Date DESC, Session ASC, Ordre ASC");
echo "<table border=1><tr class=trTitle><td>Id</td><td>Pseudo</td><td>Cadeau</td><td>Date</td><td>Score</td><td>Session</td><td>Ordre</td></tr>";
$r=0;
while($row = db_fetch_array($rows))
{
$r++;
	echo "<tr>";
	echo "<td>".$row[WId]."</td>";
	echo "<td>".$row[NickName]."</td>";
	echo "<td>".$row[Gift]."</td>";
	echo "<td>".$row["Date"]."</td>";
	echo "<td>".$row[WScore]."</td>";
	echo "<td>".$row[Session]."</td>";
	echo "<td>".$row[Ordre]."</td>";
	echo "</tr>";
}
echo "</table>";
echo "Nb de gagnants : ".db_num_rows($rows)."<br>";

?>
<form>
Voir les gagnants :
<br>
de <input type=text name=from value="<?php echo $from ?>"> à <input type=text name=to value="<?php echo $to ?>">
<br>ou pour le mois :
<input type=text name=month value="<?php echo $month ?>">
<br>
<input type=submit name=submit value="Rafraichir">
<br><br>
Id : <input type=text name=WId value="<?php echo $WId ?>"> (0 pour ajouter)
<br>
Ordre : <input type=text name=Ordre value="<?php echo $Ordre ?>">
<br>
Pseudo : <input type=text name=NickName value="<?php echo $NickName ?>">
<br>
Cadeau : <input size=50 type=text name=gift value="<?php echo $gift ?>">
<br>
Date : <input type=text name=aDate value="<?php echo $aDate ?>">
<br>
Session : <input size=50 type=text name=aSession value="<?php echo $aSession ?>">
<br><br>
<input type=submit name=submit value="Infos">
<input type=submit name=submit value="Valider">
<input type=submit name=submit value="Supprimer">
<br><br>
<input type=submit name=submit value="Recuperer"> les
<input type=text name=nWinners value="<?php echo $nWinners ?>">
premiers de l'archive
<select name=archiveDate>
<?php
	# on récupère toutes les dates des archives dispo
	$archives = main_db_query("SELECT DISTINCT(Date) AS ADate FROM ScoresArchive");
	while($archive = db_fetch_array($archives))
		echo "<option>".$archive[ADate]."</option>";
?>
</select>
</form>
