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

# lit le contenu d'un repertoire
# le met dans un tableau en retour
function getDirArray($sPath) 
{ 
	//Load Directory Into Array 
	$handle=opendir($sPath);
	$r=0;
	while ($file = readdir($handle))
	{
		# on ne veut pas de . et ..
		# on ne prend que les files (par les répertoires par exemple)
		if ($file!="." and $file!=".." and is_file(chop($sPath.$file)))
			$retVal[count($retVal)] = $file;
	}
	
	//Clean up and sort 
	closedir($handle); 

	# on trie
	sort($retVal); 
	
	return $retVal; 
}

?>

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

<?php
$Name = addslashes($Name);
switch($submit)
{
	case "Infos":
		$fruit = db_fetch_array(main_db_query("SELECT * FROM Fruits WHERE Id='".$Id."'"));
		$Name = $fruit[Name];
		$Image = $fruit[Image];
		break;
		
	case "Valider":
		# ajout d'une nouvelle équipe
		if ($Id==0)
		{
			main_db_query("INSERT INTO Fruits (Name,Image) VALUES ('".$Name."','".$Image."')");
		}
		else
		{
			main_db_query("UPDATE Fruits SET Name='".$Name."',Image='".$Image."' WHERE Id='".$Id."'");
		}
		$Name = "";
		$Image = "";
		break;
		
	case "Supprimer":
		main_db_query("DELETE FROM Fruits WHERE Id='".$Id."'");
		$Name = "";
		$Image = "";
		break;
}
if ($Image=="") $Image="flags/";
$fruits = main_db_query("SELECT * FROM Fruits ORDER BY Name ASC");
?>
<div class=universBold>Liste des maillots</div>
<table border=1>
	<tr class=trTitle>
		<td>N°</td>
		<td>Nom</td>
		<td>Image</td>
		<td>Image</td>
	</tr>
<?php
while($fruit = db_fetch_array($fruits))
{
	echo "<tr>";
	echo "<td>".$fruit[Id]."</td>";
	echo "<td>".$fruit[Name]."</td>";
	echo "<td>".$fruit[Image]."</td>";
	echo "<td><img src='".$IMAGES_DIR.$fruit[Image]."'></td>";
	echo "</tr>";
}
echo "</table>";
?>
<br><hr>
<form>
<table>
	<tr>
		<td>
			N° :
		</td>
		<td>
			<input type=text size=3 name=Id value="<?php echo $Id ?>"> (0 pour ajouter une équipe)
		</td>
	</tr>
	<tr>
		<td>
			Nom :
		</td>
		<td>
			<input type=text name=Name value="<?php echo $Name ?>">
		</td>
	</tr>
	<tr>
		<td>
			Image :
		</td>
		<td>
			<select name="Image">
			<?php
				// contenu du répertoire "$GAME_DIR.Images/flags/" :
				$fichier = getDirArray($GAME_DIR."Images/flags/");
				for($r=0;$r<count($fichier);$r++)
				{
					if ($fichier[$r] != "." && $fichier[$r] != "..")
					{
						echo "<option value=flags/".$fichier[$r];
						if (("flags/".$fichier[$r])==$Image)
							echo " selected";
						echo ">".$fichier[$r];
						//echo "<br>".$fichier[$r];
					}
				}
			?>
			</select>
		</td>
	</tr>
</table>
	<br><br>
	<input type=submit name=submit value="Infos">&nbsp;&nbsp;&nbsp;&nbsp;
	<input type=submit name=submit value="Valider">&nbsp;&nbsp;&nbsp;&nbsp;
	<input type=submit name=submit value="Supprimer">
	<br><br>
	<a class=under href="<?php echo $PHP_SELF ?>">Rafraichir la page</a>
</form>