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

	header( 'content-type: text/html; charset=utf-8' );
	
	if( isset($_POST['valide']) && $_POST['valide'] == "Enregister" )
	{
		if(!isset($_POST['options']))
		{
			echo "Attention: Aucune date n'a été choisie !.";
		}
		else
		{
			$options = $_POST['options'];      
			$mois = $_POST['mois']; 
			$mois =	ucfirst($mois);			
			$annee = $_POST['annee'];  
			$table = "reservation" .$annee;
			
			// remise à off de toutes les dates
			$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and mois ='$mois' ";	
			//echo $sql;
			$result = mysqli_query($connexion, $sql );
			$n = mysqli_num_rows($result);
			//echo $n;
			$val = "unactif";
			while($datas = mysqli_fetch_assoc($result))
			{	$id = $datas['id'];
				$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>';
				$res = mysqli_query($connexion, $sql );
				//echo $id;
			}
			
			// mise à jour des selections	

			for( $i=0; $i < count($options) ; $i++)
			{	$indice = $i + 1;
				$jour = $options[$i];
				$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and mois ='$mois' and rendezvous ='$jour' ";	
				//echo $sql;
				$result = mysqli_query($connexion, $sql );
				$datas = mysqli_fetch_assoc ($result);
				//print_r($datas);
				$id = $datas['id'];
				$val = "actif";
				
				$sql = "UPDATE " .$table ." SET mode = '$val' , indice = '$indice' WHERE id ='$id' ";
				//echo $sql;
				mysqli_query($connexion, $sql );	
				
				
				
/*
				if($i<2)	
				{	$sql = "UPDATE " .$table ." SET mode = '$val' , indice = '$indice' , seance3 = '' , seance4 = '' , seance5 = '', seance6 = '' WHERE id ='$id' ";
					//echo $sql;
					mysqli_query($connexion, $sql );	
				}
				else
				{	$sql = "UPDATE " .$table ." SET mode = '$val' , indice = '$indice' , seance4 = '' , seance5 = '' , seance6 = '' , seance7 = '' WHERE id ='$id' ";
					//echo $sql;
					mysqli_query($connexion, $sql );	
				}
*/				
			}//for
			
			echo "La selection a été enregistrée";
			echo '<br><br>';
			
			
		}
		
	}	
		
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Espace réservation</title>
</head>		

<body>

	<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 class="active"><a href="configuration.php"><span>Configuration</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>
	</ul>
	</div>
	<br>
	<br>
	
<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>
	
<?php

	$annee = date('Y');
	$nom_table  = "reservation" .$annee;
	
	$mois = $_GET['mois'];
	echo ucfirst($mois) .' ' .$annee;
	echo '<br><br>';
	
	$liste_mois = array("janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre");
		
	//echo ucfirst($tab_mois[$mois]);
	//echo '<br>';
	//echo '<br>';
	
	$sql = "SELECT * FROM " .$nom_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());
	}
	
	$n = mysqli_num_rows($result);
		
	echo '<form action="" method="POST" >';
	
	echo '<input type="hidden" name="nb_element" value="' .$n .'">';
	echo '<input type="hidden" name="mois" value="' .$mois .'">';
	echo '<input type="hidden" name="annee" value="' .$annee .'">';
	
	echo '<table>';
	echo '<tr>';
	echo '<th>Sem</th>';
	echo '<th>Jour</th>';
    echo '<th>Date</th>';
	echo '<th>Ajouter</th>';
	echo '</tr>';
	
	
	while($datas = mysqli_fetch_assoc ($result))
	{		
		$semaine = $datas['semaine'];
		$rendezvous = $datas['rendezvous'];
		$jour = $datas['jour'];
		$mode = $datas['mode'];	
			
		// pair ou impair ??
		if ($semaine%2 == 1)
		{	echo "<tr bgcolor = '#A6FF4D'>";
		} 
		else
		{
			echo "<tr bgcolor = '#FFAC59'>";
		}
	
		echo "  <td align='center'> $semaine </td>
				<td align='center'> $rendezvous </td>		
				<td align='center'> $jour </td>";

		
		if($mode == "actif")
		{  
			echo  '<td align="center"><input type="checkbox" class="largerCheckbox" name="options[]" checked value="' .$rendezvous  .'"></td>';
		} 
		else
		{
			echo '<td align="center"><input type="checkbox"  class="largerCheckbox" name="options[]" value="' .$rendezvous  .'"></td>';
		}

		echo '</tr>';
	}
			
	echo '<tr>';
	echo '<td colspan="4" align = "center"><input type="submit" name="valide" value="Enregister"/></td>';
	echo '</tr>';
			
	echo '</table>';
	
	echo '</form>';
	
?>

</body>
</html>

