<?php
	session_start ();
	include('database.php');
	
	DB_connexion(); 
	
	if (!isset($_SESSION['nom']) && !isset($_SESSION['password']))
	{  		
		header("Location: login.php");   
	}

	header( 'content-type: text/html; charset=utf-8' );
	
	$table = $_SESSION['table_reservation'];

	if(isset($_GET['submit']))
	{			
		//echo '<pre>';		
		//print_r($_GET);
		
		$nbligne = $_GET['nbligne'];	
		//echo "Lignes :" + $nbligne;
		//echo '<br>';		
		
		$tab_selections = array();
		
		for( $i=0; $i < $nbligne  ; $i++)
		{			
			${'text'.$i} = "cached".$i;
			$val = ${'text'.$i};
			$path_parts = pathinfo($_GET[$val]);
			$path_parts['basename'];
			$tab_selections[] = $path_parts['basename'];					
		}		
			
		//echo '<pre>';			
		//print_r($tab_selections);	
			
		$mois = $_GET['mois']; 		
		$annee = $_GET['annee'];  	
		//$table = "reservation_dev" .$annee;
		
		$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and mois ='$mois' ";		
		//echo $sql;			
		$result = mysqli_query($connexion, $sql );	
		//$n = mysqli_num_rows($result);		
		//echo $n;				
			
		$tab_id = array();		
		$tab_jours = array();			
			
			while($datas = mysqli_fetch_assoc($result))
			{			
					$id = $datas['id'];		
					$tab_id[] = $id;		
					$jour = $datas['rendezvous'];			
					$tab_jours[] = $jour;
					/*	
					$val = "unactif";	
					$sql = "UPDATE " .$table ." SET mode = '$val' , indice = '' ,																
					seance1 = 'unselect' , 														
					seance2 = 'unselect' ,													
					seance3 = 'unselect' , 						
					seance4 = 'unselect' ,		
					seance5 = 'unselect' ,			
					seance6 = 'unselect' ,			
					seance7 = 'unselect' ,				
					seance8 = 'unselect' WHERE id ='$id' ";*/		
					//echo $sql;			
					//echo '<br>';		
					//mysqli_query($connexion, $sql );		
			}
			
			//echo '<pre>';			
			//print_r($tab_id);	
			
			//echo '<pre>';			
			//print_r($tab_jours);	
			
			$record = $nbligne / 2;
			$j = 0;
			
			$indice = 0;
			
			for( $i=0; $i < $record  ; $i++)
			{

					$mode = $tab_selections[$j];  
					$formule = $tab_selections[$j+1];		
					
					if($mode == "yes.png")
					{
						$mode_val = "actif";		
					}
					
					if($mode == "no.png")
					{
						$mode_val = "unactif";	
					}
					
					if($formule == "minus.png")
					{
						$formule_val = "courte";		
					}
					
					if($formule == "plus.png")
					{		
						$formule_val = "longue";			
					}
					
					echo $tab_selections[$j]  . "  -  " . $mode_val . "  -  " .  $formule_val  . "  -  " . $tab_selections[$j+1];
					$j = $j + 2;
					echo '<br>';	
					
					$indice = $indice + 1;		
					
					$id = $tab_id[$i];	
					$sql = "UPDATE " .$table ." SET mode = '$mode_val' , formule = '$formule_val' , indice = '$indice' WHERE id ='$id' ";	
					//echo $sql;
					//echo '<br>';	
					mysqli_query($connexion, $sql );

			}
			
			echo "Données enregistées";	
			echo '<br><br>';
	}
?>	
 <script>
 
	var newsrc = "yes.png";

function toto()
{
		var valeur = document.getElementById("nbligne").value;
	
		for (i = 0; i < valeur; i++)
		{
			var cached = 'cached'+i;
			var elt = 'id'+i;
			document.getElementById(cached).value = document.images[elt].src;
		}
}	 

function change_image(pic)
{	// var o = pic.name	//var o = document.getElementById('pic');	// alert(pic);	//pic = 'id8';
  		if( newsrc == "yes.png" )
		{
			 document.images[pic].src = "yes.png"; 
			 document.images[pic].alt = "Yes"; 
			 newsrc  = "no.png";
		}
		else 
		{    document.images[pic].src = "no.png";
			 document.images[pic].alt = "no"; 
			 newsrc  = "yes.png"; 
		}
}

function change_image_formule(pic)
{	// var o = pic.name	//var o = document.getElementById('pic');	// alert(pic);	//pic = 'id8';
  		if( newsrc == "plus.png" )
		{
			 document.images[pic].src = "plus.png"; 
			 document.images[pic].alt = "Longue"; 
			 newsrc  = "minus.png";
		}
		else 
		{    document.images[pic].src = "minus.png";
			 document.images[pic].alt = "simple"; 
			 newsrc  = "plus.png"; 
		}
}

</script> 
<!--
Image switching with onclick
-->

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
</head>
<body>
<style>

th, td {    padding: 5px;
	   }

input.largerCheckbox{  -moz-appearance: none;
						height: 25px;
						width: 25px;
						zoom: 1.4;
					}

label {	display: inline-block;
		width: 200px;
		float: left;
	  }
.increase {  width:30px; 
		     height:30px;
		  }

/*input[type='checkbox']{    -webkit-appearance:none;    width:30px;    height:30px;    background:white;    border-radius:5px;    border:2px solid #555;}
*/


</style><link rel="stylesheet" href="menu_style.css" type="text/css">
	<div id='cssmenu'>	
    <ul>	
    	<li><a href='home.php'><span>Home</span></a></li>
        <li><a href='selection_planning_mois.php'><span>Planning</span></a></li>
        <li><a href='selection_gestion_mois.php'><span>Gestion</span></a></li>	
        <li><a href='selection_statistique_mois.php'><span>Statistiques</span></a></li>
<?php	
		if($_SESSION['nom'] == "admin")
		{ 	
			echo '<li><a href="deplacer_un_rendezvous_admin.php"><span>Déplacer</span></a></li>';
			echo '<li><a href="configuration.php"><span>Configuration</span></a></li>';
		}
		else
		{
			echo '<li><a href="deplacer_un_rendezvous_user.php"><span>Déplacer</span></a></li>';
		}
?>		
		<li><a href='change_password.php'><span>Changer password</span></a></li>	
        <li><a href='logout.php'><span>Déconnexion</span></a></li>	
        <li><a href='#'><span><?php echo $_SESSION['nom'] .' ' .$_SESSION['prenom'] .' connecté' ?></span></a></li>
		<li><a href='#'><span><?php echo $_SESSION['year']; ?></span></a></li>
   	</ul>
    </div>
    <br>
    <br>
<?php
			$mois = $_GET['mois'];		
		
			$annee = date('Y');			
			//$nom_table  = "reservation_dev" .$annee;	
			echo ucfirst($mois) .' ' .$annee;		
			echo '<br><br>';
			
			$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and mois ='$mois'" ;
			//echo $sql;		
			$result = mysqli_query($connexion, $sql );	
			
			if(!$result)
			{	
				die('Impossible d\'exécuter la requête :' . mysql_error());
			}
			
			$nbligne = mysqli_num_rows($result);	
			
			$total_nbligne = $nbligne * 2;
			
?>			
            
            <form action="#" name ="form_gestion" method="get">

<?php		
			echo '<input type="hidden" name="nbligne" id="nbligne" value="' .$total_nbligne .'">';
			echo '<input type="hidden" name="mois" id="mois" value="' .$mois .'">';			
			echo '<input type="hidden" name="annee" id="annee" value="' .$annee .'">';

			echo '<table>';
			echo '<tr>';	
			echo '<th>Id</th>';
			echo '<th>Sem</th>';
			echo '<th>Jour</th>';
			echo '<th>Date</th>';			
			echo '<th>Selection</th>';	
			echo '<th>Type</th>';	
	
			echo '</tr>';
			
			for( $i=0; $i < $nbligne * 2 ; $i++)	
			{
				$cached = 'cached' .$i;			
				echo "<input type='hidden' name='$cached' id='$cached' value=''>";
			}
			$cpt = 0;			
			$index = 1;
			
			while($datas = mysqli_fetch_assoc ($result))
			{
				$semaine = $datas['semaine'];
				$rendezvous = $datas['rendezvous'];
				$jour = $datas['jour'];
				$mode = $datas['mode'];	
				$formule = $datas['formule'];	
				$objet = 'id' .$cpt;
				
				// pair ou impair ??				
				if ($semaine%2 == 1)			
				{					
					echo "<tr bgcolor = '#A6FF4D'>";	
				} 
				else		
				{	
					echo "<tr bgcolor = '#FFAC59'>";				
				}
				
				echo "  <td align='center'> $index </td>		
						<td align='center'> $semaine </td>	
						<td align='center'> $rendezvous </td>	
						<td align='center'> $jour </td>";		
						
				if($mode == "actif")
				{
					echo '<td align="center"><img src="yes.png" alt="non"" id=' .$objet .' onclick="change_image(' .$cpt .')" /></td>';	
				} 
				else		
				{	
					echo '<td align="center"><img src="no.png" alt="non"" id=' .$objet .' onclick="change_image(' .$cpt .')" /></td>';
				}
				
				$cpt = $cpt + 1;
				$objet2 = 'id' .$cpt;
				
				if($formule == "courte")
				{
					echo '<td align="center"><img src="minus.png" alt="non"" id=' .$objet2 .' onclick="change_image_formule(' .$cpt .')" /></td>';	
				} 
				else		
				{	
					echo '<td align="center"><img src="plus.png" alt="non"" id=' .$objet2 .' onclick="change_image_formule(' .$cpt .')" /></td>';
				}
				
				
				echo '</tr>';
				$cpt = $cpt + 1;
				$index = $index + 1;
			}
			
			echo '<tr>';
			echo '<td colspan="4" align = "center">	
				  <input type="submit" name="submit" value="Enregistrer" onclick = "toto()";/>	
				  </td>';			
		  	echo '</tr>';	
			echo '</table>';	
			echo '</form>';?>
</body>
</html>