<?php
	session_start();
	include('database.php');
	DB_connexion(); 
	
	date_default_timezone_set('Europe/Paris');
	
	$J1 = $_POST['J1'];
	$J2 = $_POST['J2'];
	
	$score_J1 = 0;
	$score_J2 = 0;
	$score_nul = 0;

	$inv_score_J1 = 0;
	$inv_score_J2 = 0;
	$inv_score_nul = 0;
	
	$score_total__J1 = 0;
	$score_total_J2 = 0;
	$score_total_nul = 0;
	
	//$sql = "SELECT * FROM parties WHERE w_name = '".$J1."'  and b_name =  '".$J2."'  "   ;
	//$sql = "SELECT * FROM parties_new WHERE w_name = '".$J1."'  and b_name =  '".$J2."'  "   ;
	$sql = "SELECT * FROM "  .$_SESSION['parties'] ." WHERE w_name = '".$J1."'  and b_name =  '".$J2."'  "   ;
	
	
	$results1 = mysql_query($sql);
	$n1 = mysql_num_rows($results1);
			
	while($data = mysql_fetch_assoc($results1)) 
	{
		 $match =  $data['result'];
		 if( $match == "1")
		 {
			$score_J1 = $score_J1 +1; 
		 }
		 if( $match == "2")
		 {
			$score_J2 = $score_J2 +1; 
		 }
		 if( $match == "3")
		 {
			$score_nul = $score_nul +1; 
		 }
	}
	
	//$sql = "SELECT * FROM parties WHERE w_name = '".$J2."'  and b_name =  '".$J1."'  "   ;
	//$sql = "SELECT * FROM parties_new WHERE w_name = '".$J2."'  and b_name =  '".$J1."'  "   ;
	
	$sql = "SELECT * FROM " .$_SESSION['parties'] ." WHERE w_name = '".$J2."'  and b_name =  '".$J1."'  "   ;
	
	
	$results2 = mysql_query($sql);
	$n2 = mysql_num_rows($results2);
	
	while($data = mysql_fetch_assoc($results2)) 
	{
		 $match =  $data['result'];
		 if( $match == "1")
		 {
			$inv_score_J2 = $inv_score_J2 +1; 
		 }
		 if( $match == "2")
		 {
			$inv_score_J1 = $inv_score_J1 +1; 
		 }
		 if( $match == "3")
		 {
			$inv_score_nul = $inv_score_nul +1; 
		 }
		 
		/*$white = $data["w_playerID"]; 
		$w_name = $data["w_name"];
		$black = $data["b_playerID"];
		$b_name = $data["b_name"];
		$w_rating = $data["w_arating"];
		$b_rating = $data["b_arating"];
		$w_brating = $data["w_brating"];
		$b_brating = $data["b_brating"];
		$result = $data["result"];
		$date = $data["date"];
		$date = date("d F Y",$date);*/
		
		//$white ." " .$w_name 	$black 	$b_name $w_rating $b_rating $w_brating $b_brating $result $date 
	}
	
	
	
	$score_total_J1 = $score_J1 + $inv_score_J1;
	$score_total_J2 = $score_J2 + $inv_score_J2;
	$score_total_nul = $score_nul + $inv_score_nul;
	
	$nombre = $n1 + $n2;
	echo "Nombre de rencontres: "  .$nombre;
	if($nombre != 0)
	{
		echo "<br>";
		echo $J1 ." : " .$score_total_J1 ." victoires";
		echo "<br>";
		echo $J2 ." : " .$score_total_J2 ." victoires";
		if($score_total_nul != 0)
		{
			echo "<br>";
			echo $score_total_nul ." match nul";
		}	
	}
		
	//$sql = "SELECT * FROM parties WHERE w_name = '".$J1."'  and b_name =  '".$J2."' or w_name = '".$J2."'  and b_name =  '".$J1."' ORDER by date desc " ;  
	$sql = "SELECT * FROM parties_new WHERE w_name = '".$J1."'  and b_name =  '".$J2."' or w_name = '".$J2."'  and b_name =  '".$J1."' ORDER by date desc " ;  
	
	//$sql = "SELECT * FROM " .$_SESSION['parties'] ." WHERE w_name = " .$J1 ." and b_name = " .$J2 ." or w_name = " .$J2 ."  and b_name = "  .$J1 ." ORDER by date desc " ;  
	//$sql = "SELECT * FROM parties_new " ." WHERE w_name = " .$J1 ." and b_name = " .$J2 ." or w_name = " .$J2 ."  and b_name = "  .$J1 ." ORDER by date desc " ;  
	
	//echo $sql;
	
	//$sql = "SELECT * FROM " .$_SESSION['parties'] ." WHERE w_name = '".$J1."'  and b_name =  '".$J2."' or w_name = '".$J2."'  and b_name =  '".$J1."' ORDER by date desc ";   
	
	
	$results = mysql_query($sql);	
	
	$n = mysql_num_rows($results);
	if($n != 0)
	{	
?>	
		<br>
		<br>
	
		<table width="750" align="center">
			<tr>
			<div id = "alignement"> 
			<td width="15" align='center' bgcolor="#CCCCCC">ID</td>
			<td style="display:table-cell; vertical-align:middle;" width="120" align='center' bgcolor="#CCCCCC">Blanc</td>
			<td style="display:table-cell; vertical-align:middle;" width="120" align='center' bgcolor="#CCCCCC">Noir</td>
			<td width="40" align='center' nowrap="nowrap" bgcolor="#CCCCCC">Score</td>
			<td width="120" align='center' bgcolor="#CCCCCC">Date</td>
			</div>	
			</tr>
<?php	
/*
img {
    vertical-align: middle;
} 
*/
		$id = 1;

		while($data = mysql_fetch_assoc($results)) 
		{	
				$J1 = $data['w_name'];
				$J2 = $data['b_name'];
				$resultat_partie = $data['result'];
				$date = $data['date'];
				$date = date("d F Y",$date);
				$date = strftime('%d-%m-%Y',strtotime($date));
				
				if($resultat_partie == "1")
				{
					$score = "1 - 0";
					$img1 = "<img src='images/like.png' title='Victoire' >";
					$img2 = "<img src='images/unlike.png' title='Défaite' >";
				}
				if($resultat_partie == "2")
				{
					$score = "0 - 1";
					$img1 = "<img src='images/unlike.png' title='Défaite' >";
					$img2 = "<img src='images/like.png' title='Victoire' >";
				}
				if($resultat_partie == "3")
				{
					$score = "0.5 - 0.5";
				}
				
				echo "<tr> 
					  <td align=center width='60'> $id </td> 	
					  <td align=center width='60'> $J1" ;
				echo "    ";
				echo $img1;
				echo "</td> 
					  <td align=center width='60'> $J2 ";
				echo "    ";
				echo $img2;	  
				echo "</td>
					  <td align=center width='120'> $score </td>
					  <td align=center width='100'> $date </td>
					   </tr>";	
				$id = $id + 1;			
		}		
		
		echo "</table>";
	}
/*
	$sql = "SELECT * FROM partie WHERE joueur1 = '".$J1."'";
		$results1 = mysql_query($sql);
		$sql = "SELECT * FROM partie WHERE joueur2 = '".$J1."'";
		$results2 = mysql_query($sql);
		$n1 = mysql_num_rows($results1);
		$n2 = mysql_num_rows($results2);
		$number = $n1 + $n2;
		echo "Nombre de réponses: "  .$number;
		
		if($number != 0)
		{
			echo '<div id="affiche_resultat">';
			echo '	<table border="1">
					<tr>
					<th class="center" width="60">Joueur 1</th>
					<th class="center" width="60">Joueur 2</th>
					<th class="center" width="120">Résultat</th>
					<th class="center" width="120">Score</th>
					<th class="center" width="100">Date</th>
					<th class="center" width="100">Heure</th>
					</tr>';
				
			while($data = mysql_fetch_assoc($results1)) 
		    {	
		     	$J1 = $data['joueur1'];
				$J2 = $data['joueur2'];
				$resultat_partie = $data['resultat_partie'];
				$score = $data['score_joueur1'];
				$date = $data['date_partie'];
				$heure = $data['heure_partie'];
				echo "<tr> 
					  <td align=center width='60'> $J1 </td> 
					  <td align=center width='60'> $J2 </td> 
					  <td align=center width='120'> $resultat_partie </td>
					  <td align=center width='120'> $score </td>
					  <td align=center width='100'> $date </td>
					  <td align=center width='100'> $heure </td>	
					  </tr>";
		    }						
			while($data = mysql_fetch_assoc($results2)) 
		    {	
				$J1 = $data['joueur1'];
				$J2 = $data['joueur2'];
				$resultat_partie = $data['resultat_partie'];
				$score = $data['score_joueur2'];
				$date = $data['date_partie'];
				$heure = $data['heure_partie'];
				echo "<tr> 
					  <td align=center width='60'> $J2 </td> 
					  <td align=center width='60'> $J1 </td>
					  <td align=center width='120'> $resultat_partie </td>
					  <td align=center width='120'> $score </td>
					  <td align=center width='100'> $date </td>
					  <td align=center width='100'> $heure </td>	
					  </tr>";	  
		    }		
			echo "</table>";
			echo '</div>';
		}
*/		
	
	
	
	
?> 