<?php
	session_start();
	// include('database.php');
	// DB_connexion(); 
	//include('correlation.php');
	//include('elo_communes.php');
		
	/*if(!isset($_SESSION['logged']) )
	{		header ('Location: interdiction.php');
	}
	
	if($_SESSION['logged']== "no")
	{
		header ('Location: interdiction.php');
	}*/
	
	
	if(!empty($_POST) )
	{
		$_SESSION['sauvegarde_formulaire'] = $_POST ;
     
		$page_actuelle = $_SERVER['PHP_SELF'] ;
		if(!empty($_SERVER['QUERY_STRING']))
		{
			$page_actuelle .= '?' . $_SERVER['QUERY_STRING'] ;
		}
     
		header('Location: ' . $page_actuelle);
		exit;
	}
	
	if(isset($_SESSION['sauvegarde_formulaire']))
	{
		$_POST = $_SESSION['sauvegarde_formulaire'] ;
		unset($_SESSION['sauvegarde_formulaire']);		
	}	
	
	if(isset($_POST['valide']) && $_POST['valide'] == "ok")
	{ 
				$id_partie =  $_POST['id_partie'] - 1;
								
				$sql = "SELECT * FROM parties ORDER by date asc"; 
				$results = mysql_query($sql);
				$nb_parties = mysql_num_rows($results);
							
				while($row = mysql_fetch_assoc($results))
				{
					$blanc = $row['w_name'];
					$noir = $row['b_name'];
					$date = $row['date'];
					$resultat = $row['result'];
					$date_partie = date('d/m/Y', $date);
					
					if($resultat == 1)
					{
						$_score_blanc = 1;
						$_score_noir = 0;
					}
					if($resultat == 2)
					{
						$_score_blanc = 0;
						$_score_noir = 1;
					}
					if($resultat == 3)
					{
						$_score_blanc = 0.5;
						$_score_noir = 0.5;
					}
				
					$table_csv[0] = $date_partie;
					$table_csv[1] = $blanc;
					$table_csv[2] = $noir;
					$table_csv[3] = $_score_blanc;
					$table_csv[4] = $_score_noir;

					$table_fichier[] = $table_csv;
				}
	
				$nom_fichier = "sauvegarde_suppression.txt";
							
				$fp = fopen($nom_fichier, 'w'); 
				for ($i=0; $i < $nb_parties; $i++)
				{
					$str_comma_separated = implode(";", $table_fichier[$i]);
					fputs ($fp, $str_comma_separated);
					if($i != $nb_parties - 1)
					fputs($fp, "\n");
				}
						
				fclose($fp);
		
				// creation du fichier modifie
				
				$nom_fichier = "new.txt";
							
				$fp = fopen($nom_fichier, 'w'); 
				for ($i=0; $i < $nb_parties; $i++)
				{
					
					if($i != $id_partie )
					{
												
						$str_comma_separated = implode(";", $table_fichier[$i]);
						fputs ($fp, $str_comma_separated);
						if($i != $nb_parties - 1)
						{
							fputs($fp, "\n");
						}
					}	
				}
						
				fclose($fp);

		
			   // extraire les joueurs
				$joueur = array();
			   
				$sql = "SELECT * FROM classement ORDER by joueur asc"; 
				$results = mysql_query($sql);
								
				while($row = mysql_fetch_assoc($results))
				{
					$joueur[] = $row['joueur'];
				}
				
				
				
				// regenerer le classement
				mysql_query('TRUNCATE TABLE classement;');
				mysql_query('TRUNCATE TABLE evolution;');
				mysql_query('TRUNCATE TABLE parties;');
	
				stockage_joueurs_DB($joueur);
	
				$file = fopen ($nom_fichier,"r");
		
				$indice =  1;	
				while(! feof($file))
				{
						$tmp = fgets($file);
						$tmp = strtolower($tmp);
						$tmp = str_to_noaccent($tmp);
						$pieces = explode(";", $tmp);
						$donnees[0] = $pieces[0];
						list($jour, $mois, $annee) = explode('/', $donnees[0]);
						$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
						$donnees[0] = $timestamp; 
						$donnees[1] = $pieces[1];
						$donnees[2] = $pieces[2];
						$donnees[3] = $pieces[3];
						$donnees[4] = $pieces[4];
						//echo "<pre>";
						//print_r($donnees);
						algo_elo($donnees, "nostatistique", "sql", $indice );
						
						$indice = $indice + 1;
				}
				
				header("Location: historique.php");
	}			
	
?>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Description" content="Site perso,cours,ouvrages" />
<meta name="Keywords" content="WEBSITE perso " />
<meta name="Distribution" content="Global" />
<meta name="Robots" content="" />
<meta http-equiv="Content-Language" content="fr" />
<meta name="rating" content="general" />
<meta name="author" content="Patrice DELPY" />
<meta name="copyright" content="Copyright 2013" />
<meta name="generator" content="Patoche" />
<title>Patoche WEBSITE - 2013 - Template du site - en cours de développemment ...</title>

<!---------------------------------------------------------->	
<?php
		include('include_css_template_commun.php');
		include('include_css.php');
		include('css_tools_skill.php');
		include('include_javascript.php');

?> 

<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<style type="text/css">
#bloc_historique { float: left;
					width: 831px; 
					padding: 50px 50px 25px 50px;
					}	
					
#navigation_tournoi{
					float: left;
					width: 910px; // 885
					padding: 10px 0px 0px 10px;
		}

label
{   
	display: block;
	width: 150px;
	float: left;
	//padding: 28px;
}

select
{
width: 100px;
}

tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
		
</style>

<script type="text/javascript">




function fct_effacer()
{	
			
		$(function()
					{
								$( "#message_confirmation" ).dialog({
								modal: true,
								title: "Infos",
								buttons: {
											Oui: function()
											{
												$( this ).dialog( "close" );
												start();
													
											},
											Non: function()
											{
												$( this ).dialog( "close" );
													
											}
										 }
									});
					});
		
	 	//return false;


} 

function fct_annuler()
{	
	document.getElementById('valide').value="annule";
	javascript:history.go(-1);
} 

function start()
{
	document.getElementById("image").style.display='block';	
		
	var xhr = getXhr();
				
	xhr.onreadystatechange = function()
	{
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			retour = xhr.responseText;
			document.getElementById("image").style.display='none';	
			
				//affiche = "La nouvelle partie a été enregistrée dans la base.";
				$(function()
				{
							$( "#message_suppression" ).dialog({
							modal: true,
							title: "Infos",
							buttons: {
										Ok: function()
										{
											$( this ).dialog( "close" );
											window.location.reload();
											javascript:history.go(-1);
										}
									 }
								});
				});
				
		}
	}

	xhr.open("POST","traitement_effacer_partie.php",true);
	// ne pas oublier ça pour le post
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	id_partie = document.getElementById('id_partie').value;
	
	//xhr.send("J1="+J1+"&J2="+J2+"&jour="+jour+"&mois="+mois+"&annee="+annee+"&selection_date="+selection_date+"&selection_score="+selection_score);
	xhr.send("id_partie="+id_partie);
	
}			

function getXhr()
{
    var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject)
	{ // Internet Explorer 
	   try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
		   } catch (e) 
		   {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
		   }
	}
	else { // XMLHttpRequest non supporté par le navigateur 
			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
			xhr = false; 
		 } 

	return xhr;
}


</script>

<!---------------------------------------------------------->	
</head>

<body>
<!---------------------------------------------------------->	
	<div class="main"> <!-- main -->
<!---------------------------------------------------------->	

          <div class="header">
                <div class="balise_titre">Patoche</div>
                <div class="balise_citation">
                &quot;Etre conscient que l'on est ignorant est un grand pas vers le savoir&quot;
                </div>
          </div> 
 
          <div class="banniere"></div>  

<!---------------------------------------------------------->
<?php
/*	include("include_barre_login.php");
	echo '<div id="box_icon">';
	include("debug_connexion_membre.php");
	echo '</div>';
	include("include_menu.php");*/
	
	echo '<div id="box_infos">';
			include("include_menu.php");
			
			echo '<div id="box_icon2">';
			include("connexion_membre.php");
			echo	'</div>';
			
			echo '<div id="box_icon3">';
			include("include_barre_login.php");
			echo	'</div>';

	echo	'</div>';
	$_SESSION['page_active'] = basename (__FILE__ );
?>
		

<!---------------------------------------------------------->	
            <div class="contenu"> <!-- contenu -->
<!---------------------------------------------------------->	
	
			<div class="barre_vide"></div>
			
			<div id="navigation_tournoi">
				<ul id="menu_horiz">
				<li class="tournoi_joueur_tag"><a href="ajouter_joueur.php" title="aller à la section">Nouveau</a></li>
				<li class="tournoi_partie_tag"><a href="partie.php" title="aller à la section">Partie</a></li>
				<li class="tournoi_classement_tag"><a href="classement.php" title="aller à la section">classement</a></li>
				<li class="tournoi_liste_joueurs_tag"><a href="liste_joueurs.php" title="aller à la section">Joueurs</a></li>
				<li class="tournoi_stats_tag_selected"><a href="historique.php" title="aller à la section">Historique</a></li>
				<li class="tournoi_securite_tag"><a href="admin.php" title="aller à la section">Admin</a></li>
				</ul>
			</div>
			
			<div id="bloc_historique">
	
			Effacer la partie :   
<?php			

			$id_partie = $_GET["id"] + 1;
			$J1 = $_GET["J1"] ;
			$J2 = $_GET["J2"] ;
			$score = $_GET["score"] ;
			$date_partie = $_GET["date"] ;
			
			echo $J1 ." contre " .$J2 ." " .$score  ." du  " .$date_partie;  

			echo "<br><br><br><br>";
			
?>
		<!--	<form action="" method="post" id="form_supprimer" name="form_supprimer"> -->
			<input type="hidden" name="valide" id="valide" value="">
			<input type="hidden" name="id_partie" id="id_partie" value="<?php echo $id_partie; ?>">	

			<div align= "center">
					<input type='submit' name='effacer' value='Supprimer' onClick="fct_effacer()" />
					<input type='submit' name='Annuler' value='Annuler' onClick="fct_annuler()" />  	
			</div>	

			<img src="loader.gif" style="display: none;" name = "image" id="image">
			
			<div id='message'></div>
			
			<div id="message_confirmation" style="display: none">
			<p>
			<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
			Voulez-vous vraiment supprimer cette  partie .
			</p>
			</div>
			
			<div id="message_suppression" style="display: none">
			<p>
			<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
			La partie vient d'être supprimmer de la base.
			</p>
			</div>
			
			
			<!--</form>	-->
<?php				 
			//echo '<br><br><br><p align=center><a href="effacer.php?id='.$id.'&yes=1">OUI</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="liste_joueurs.php">NON</a></p>';

			/*if($_SESSION['message'] == "ok")
			{	echo '<div id="message_traitement" style="display:block">Joueur supprimer de la base de donnée.</div>';
				$_SESSION['message'] = "none";
			}*/
?>		
 
 
			</div>		

			<br/>
			<br/>
			<br/>
			<br/>
			<br/>

<!---------------------------------------------------------->	
<?php			
    include('include_pied_page.php');
	include('include_publicite.php');
?>
<!---------------------------------------------------------->	
			</div>  <!-- contenu -->
<!---------------------------------------------------------->	
	</div>   <!-- main -->
<!---------------------------------------------------------->	

	<script type="text/javascript">
	//	call_update()
	</script>

</body>
</html>


