<?php
// this defines some of your basic setup
include_once 'config.php';
// the facebook client library
include_once $BIB.'facebook.php';
include_once $BIB.'bib_generic.php3';
include_once $BIB.'bib_quiz_facebook.php3';
include_once $BIB.'bib_facebook_user.php3';
include_once $BIB.'bib_prono.php3';
include_once $BIB.'bib_drapeaux.php3';



include 'header.php';


$paris = get_bet($user);

if ($_POST["NbMatch"]>0) 
{
	$msg = array();
	
	for($i=1;$i<=$_POST["NbMatch"];$i++)
	{
		$matchId = $_POST['MatchId'.$i];
		$bet = $_POST[$matchId."team1"]."-".$_POST[$matchId."team2"];
		
		if(!isset($paris[$matchId]))
		{
			$query = "INSERT INTO Prono_Paris(uid,MatchId,Bet,Date) VALUES (".$user.",".$matchId.",'".$bet."','".date("Y-m-d H:i:s")."')";
			main_db_query($query);
			$msg[$matchId] = "(enregistré)";
		}
		elseif($bet != $paris[$matchId]["Team1"]."-".$paris[$matchId]["Team2"])
		{
			$query = "UPDATE Prono_Paris SET Bet='".$bet."' ,Date='".date("Y-m-d H:i:s")."' WHERE uid=".$user." AND MatchId=".$matchId;
			main_db_query($query);
			$msg[$matchId] = "(modifié)";
		}		
	}
	$paris = get_bet($user);	
}



$query = "SELECT * FROM Prono_Categories";
$categories = main_db_query($query);
while($categorie =  db_fetch_assoc($categories))
{
	echo "<div id='quiz'>\n";
	echo "<h2>".$categorie["Name"]."</h2>\n";
	echo "<div id='questions'>\n";	
	echo "<form method='post' >\n";
	echo "<table>\n";

	$query = "SELECT * FROM Prono_Matchs WHERE Category =".$categorie["CategoryId"];
	$matchs = main_db_query($query);
	$i = 0;
	while($match =  db_fetch_assoc($matchs))
	{			
			$i++;
			echo "<input type='hidden' name='MatchId".$i."' value='".$match["MatchId"]."'>\n";
			
			echo "<tr>";
			echo "<td align='right'>".$match["Team1"]."</td>\n";
			echo "<td><IMG SRC='".$IMAGES_DIR.flag($match["Team1"])."' alt='".$match["Team1"]."' ></td>\n";
			echo "<td><input type='text' class='inputtext' name='".$match["MatchId"]."team1' size='2'  value=".$paris[$match["MatchId"]]["Team1"]."></td>\n";
			echo "<td>-</td>\n";
			echo "<td> <input type='text' class='inputtext' name='".$match["MatchId"]."team2' size='2' value=".$paris[$match["MatchId"]]["Team2"]."></td>\n";
			echo "<td><IMG SRC='".$IMAGES_DIR.flag($match["Team2"])."' alt='".$match["Team2"]."' ></td>\n";
			echo "<td>".$match["Team2"]."</td>\n";
			
			echo "<td>".$msg[$match["MatchId"]]."</td>\n";
			
			echo "</tr>\n";				
	}
	echo "</table>";
	echo "<input type='hidden' name='NbMatch' value='".$i."'>\n";
	echo "<input value='enregistrer' type='submit' class='inputBouton'/>\n";
	echo "</form>\n";
	echo "</div>\n";
	
	echo "</div>\n";
}


include 'footer.php';
?>