<?php
##############################################################################
# add_games.php3                                                             #
# ajout de matchs au Kestufoot                                               #
#                                                                            #
# Copyright ViralGames SARL 2000                                             #
##############################################################################

include("../config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_drapeaux.php3");

//echo $SERVER_URL;
# pas de timeout possible qql soit l'action effectuée
set_time_limit($TIME_LIMIT);

# le script continue une fois qu'il est lancé quel que soit l'état de la connection après
ignore_user_abort(true); 

# si aucune date n'a été entrée,on affiche la date actuelle dans les champs texte
$current_european_date = date("Y-m-d H:i:s", my_local_time());
if (!isset($ShowTime))
	$ShowTime = $current_european_date;
if (!isset($Date))
	$Date = $current_european_date;
if (!isset($Taux1))
	$Taux1 = 10;
if (!isset($Taux2))
	$Taux2 = 2;

// on affiche les catégories disponibles dans un tableau
?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Ajouter des matchs</div>
<br>

<?php
$error = "";
switch ($submit)
{
	case "Infos":
		$row = db_fetch_array ( main_db_query ("SELECT * FROM Matchs WHERE MatchId='".$MatchId."'"));
		$CategoryId = $row[Category];
		$Team1 = $row[Team1];
		$Team2 = $row[Team2];
		$Taux1 = $row[Rate1];
		$Taux2 = $row[Rate2];
		$Date = $row[Date];
		$ShowTime = $row[ShowTime];
		//echo $ShowTime;
		$Cadeau = $row[Cadeau];
		$Premium = $row[Premium];
		break;
		
	case "Valider":
		
		if($locked==1)
		{
			echo "<script>alert('Vous ne pouvez pas modifier un prono terminé. Vous ne pouvez que le supprimer');</script>";
		}
		elseif ($MatchId!=0)
		{
			# vérifie si le MatchId existe parmi les paris dejà effectués
			if (db_num_rows(main_db_query("SELECT MatchId FROM Matchs WHERE MatchId='".$MatchId."' AND Result='0'"))>0)
			{
				main_db_query("UPDATE Matchs SET Category='".$CategoryId."', Team1='".$Team1."',Team2='".$Team2."',Rate1='".$Taux1."',Rate2='".$Taux2."',Date='".$Date."',ShowTime='".$ShowTime."', Cadeau='".$Cadeau."' WHERE MatchId='".$MatchId."'");
				$the_MatchId = $MatchId;
			}
		}		
		else
		{
			main_db_query("INSERT INTO Matchs (Team1,Team2,Rate1,Rate2,Date,ShowTime,Category,Cadeau, Premium) VALUES ('".$Team1."','".$Team2."','".$Taux1."','".$Taux2."','".$Date."','".$ShowTime."','".$CategoryId."','".$Cadeau."','".$Premium."')");
			$max_tmp = db_fetch_array(main_db_query("SELECT MAX(MatchId) as MaxId FROM Matchs"));
			$the_MatchId = $max_tmp[MaxId];
			main_db_query("INSERT INTO MatchsStats (MatchId, Win1, Win2, NullGame) VALUES ('$the_MatchId', '0', '0', '0')");
		}
		break;
		
	case "Supprimer":
		# on vérifie que le match n'a pas de résultat sinon la suppression est interdite
		$row = db_fetch_array ( main_db_query ("SELECT * FROM Matchs WHERE MatchId='".$MatchId."'"));
		if ($row['Result'] == "0")
		{
			# on supprime tous les paris sur le match (si les paris n'ont pas été validés)
			main_db_query("DELETE FROM Paris WHERE MatchId='".$MatchId."' AND Done='0'");
	
			main_db_query("DELETE FROM Matchs WHERE MatchId='$MatchId'");
			main_db_query("DELETE FROM MatchsStats WHERE MatchId='$MatchId'");
			$error = "Le match N°$MatchId a été supprimé";
		}
		
		break;
		
	default :
		$error = "";
		break;
}
?>


<table>
<form method="post" name="main">
	<tr>
		<td>
			Match N° :
		</td>
		<td>
			<input type="text" name="MatchId" size=3 value=<?php echo $MatchId ?>> (0 pour ajouter un match)
		</td>
	</tr>
	<tr>
		<td>
			Catégorie:
		</td>
		<td>
			<select name="CategoryId" size=1>
			<?php
			
			$categories = main_db_query("SELECT Categories.*,Containers.Name AS ContainerName FROM Categories LEFT JOIN Containers ON Containers.Id=Categories.Container  WHERE Categories.EndTime>'$current_european_date' ORDER BY Container,Name");
			
			//asort($categories);
			while($categorie = db_fetch_array($categories))
			{
				if ($categorie[CategoryId]==$CategoryId)
					echo "<option value=$categorie[CategoryId] selected>".$categorie[ContainerName]." - ".$categorie[Name]."</option>";
				else
					echo "<option value=$categorie[CategoryId]>".$categorie[ContainerName]." - ".$categorie[Name]."</option>";
			}
				echo "</select>";
				
		?>
		</td>
	</tr>
	<tr>
		<td>
			Equipe 1 :
		</td>
		<td>
			<select name="Team1" size=1>
<?php
	asort($TEAM_NAMES);
	while(list($k,$v)=each($TEAM_NAMES))
	{
		if ($Team1==$v)
			echo "<option selected>".$v;
		else
			echo "<option>".$v;
	}
		echo "</select>";
	echo "</td></tr>";
	echo "<tr>";
		echo "<td>Equipe 2 :</td>";
		echo "<td><select name=\"Team2\" size=1>";

	reset($TEAM_NAMES);
	while(list($k,$v)=each($TEAM_NAMES))
	{
		if ($Team2==$v)
			echo "<option selected>".$v;
		else
			echo "<option>".$v;
	}

	echo "</select>";

?>
		</td>
	</tr>
	<tr>
		<td>
			Coef. pari exact : 
		</td>
		<td>
			<input type="text" name="Taux1" size=2 value="<?php echo $Taux1 ?>">
		</td>
	</tr>
	<tr>
		<td>
		    Coef. pari gagnant : 
		</td>
		<td>
			<input type="text" name="Taux2" size=2 value="<?php echo $Taux2 ?>">
		</td>
	</tr>
	<tr>
		<td>
		    Date/Heure d'affichage :
		</td>
		<td>
			<input type="text" name="ShowTime" value="<?php echo $ShowTime ?>"> AAAA-MM-JJ HH:MM:SS
		</td>
	</tr>
	<tr>
		<td>
			Date/Heure de fin de pari :
		</td>
		<td>
			<input type="text" name="Date" value="<?php echo $Date ?>"> AAAA-MM-JJ HH:MM:SS
		</td>
	</tr>
<!--	<tr>
		<td>
			Offrir un cadeau pour ce match :
		</td>
		<td>
			<input type="checkbox" name="Cadeau" value="1" <?php if ($Cadeau==1) echo "checked"; ?>>
		</td>
	</tr>
	<tr>
		<td>
			Match Premium League :
		</td>
		<td>
			<input type="checkbox" name="Premium" value="1" <?php if ($Premium==1) echo "checked"; ?>>
		</td>
	</tr>-->
	<tr>
	<td colspan=2>
	<?php
	if (time_diff_now($Date) > 0)
	{
		echo "<input type='hidden' name='locked' value='1'><b>LOCKED</b>";
	}
	else
	{
		echo "<input type='hidden' name='locked' value='0'>";
	}
	?>
	<br><br>
	<input name=submit type=submit value="Infos">&nbsp;&nbsp;&nbsp;
    <input name=submit type=submit value="Valider"><img width=150 height=1 src="<?php echo $IMAGES_DIR ?>spacer.gif">
	<input name=submit type=submit value="Supprimer"> (Lire attentivement l'aide pour la suppression)
	
	</td>
	</tr>
	</form>
</table>


<br><br><br><br>

<table border=1>
	<tr class=trTitle>
		<td align=center colspan=9>Liste des matchs en cours dans la base</td>
	</tr>
	<tr class=trTitle>
		<td>N°</td>
		<td>Catégorie</td>
		<td>Equipe 1</td>	
		<td>Equipe 2</td>		
		<td>Date/Heure d'affichage</td>		
		<td>Date/Heure de fin de pari</td>
<!--		<td>Kdo</td>
		<td>Premium</td>-->
		<td>Nb</td>
	</tr>
<?php
$rows=main_db_query("SELECT * FROM Categories");
while($row=db_fetch_array($rows))
	$tabCategories[$row['CategoryId']] = $row['Name'];

$rows=main_db_query("SELECT *,Matchs.ShowTime as MatchShowTime FROM Matchs WHERE Result='0' ORDER BY Date ASC, MatchId ASC");
$r=0;
while($row=db_fetch_array($rows))
{
//	$color = ($r % 2 == 0)? "#FFFFFF":"#E8E8E8";
	$r++;

	if (time_diff_now($row[MatchShowTime]) > 0 and time_diff_now($row[Date]) < 0)
		echo "<tr bgcolor=yellow>";
	elseif (time_diff_now($row[MatchShowTime]) > 0 and time_diff_now($row[Date]) > 0)
		echo "<tr bgcolor=#FF8040>";
	else
		echo "<tr>";
	
	$nBetsOn = db_fetch_array(main_db_query("SELECT COUNT(MatchId) AS total FROM Paris WHERE MatchId='".$row[MatchId]."'"));
	
	//echo "<tr bgcolor=".(($row[Premium]==1)?"#FFBB00":$color).">";
		echo "<td>".$row[MatchId]."</td>";
		echo "<td>".$tabCategories[$row[Category]]."</td>";
		echo "<td>".$row[Team1]."</td>";
		echo "<td>".$row[Team2]."</td>";
		echo "<td>".$row[MatchShowTime]."</td>";
		echo "<td><b>".$row[Date]."</b></td>";
		//echo "<td>".$row[Cadeau]."</td>";
		//echo "<td>".$row[Premium]."</td>";
		echo "<td>".$nBetsOn[total]."</td>";
	echo "</tr>";
}
echo "<tr class=trTitle><td colspan=9>&nbsp;$error</td></tr></table>";
?>

</body>
</html>
