<?php

	session_start ();
	
	include('database.php');
	DB_connexion();
 	header( 'content-type: text/html; charset=utf-8' );
	
	if (!isset($_SESSION['nom']) && !isset($_SESSION['password']))
	{  
			header("Location: login.php");   
	}
	
	if(isset($_POST['valide']) && $_POST['valide'] == "ok")
	{ 
				$mois = $_POST['mois'];
				$jour = $_POST['jour'];
				$horaire = $_POST['horaire'];
				$mode = $_POST['mode'];
							
				$annee = date('Y');
				$table = "reservation" .$annee;
	
				if($mode == "ouvrir")
				{	echo "+++++";
					$str = "creneau ouvert";
				}
			
				if($mode == "fermer")
				{	echo "------";
					$str = "creneau fermer";
				}
				
				mysqli_query($connexion, "SET NAMES 'utf8'");
				$s1 = "UPDATE " .$table ." SET ";		
				$s2 = $horaire ." = '$str'";		
				$s3 = " WHERE rendezvous ='$jour' ";	
				$sql = $s1 . $s2 .$s3;		
				echo $sql;		
				echo '<br>';
				$result = mysqli_query($connexion, $sql );	
				if(!$result)
				{
					die('Impossible d\'exécuter la requête :' . mysql_error());
				}
				
				header("Location: ouvrir_crenaux.php?mois=$mois");
	 }

	 if(isset($_POST['valide']) && $_POST['valide'] == "annule")
	 { 		$mois = $_POST['mois'];
			header("Location: ouvrir_crenaux.php?mois=$mois");
	 }	

 /////////////////////////////////////////////////////////////////////
	 
	 $mois = $_GET['mois'];	
	 $jour = $_GET['jour'];	
	 $horaire = $_GET['horaire'];	
	 $mode = $_GET['mode'];	
	 
	switch ($horaire)
	{
			case "seance1":
				$val = "12H20 ";
				break;
			case "seance2":
				$val = "12H40 ";
				break;
			case "seance3":
				$val = "13H ";
				break;
			case "seance4":
				$val = "13H20 ";
				break;	
			case "seance5":
				$val = "13H40 ";
				break;
			case "seance6":
				$val = "14H ";
				break;
			case "seance7":
				$val = "14H20 ";
				break;
			case "seance8":
				$val = "14H40 ";
				break;		
	} 
	 
	 
?>


<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>		

<link rel="stylesheet" href="menu_style.css" type="text/css">

<body>

<script type="text/javascript">

function fct_ajouter()
{	
	verif = confirm("Voulez vous vraiment ajouter ce créneau ?"); 	

 	if(verif == true) 
	{ document.getElementById('valide').value="ok"; 
	  document.getElementById('mode').value="ouvrir"; 	
    } 	

	if(verif == false) 
	{ document.getElementById('valide').value="stop"; 	  }	
} 


function fct_supprimer()
{	
	verif = confirm("Voulez vous vraiment fermer ce créneau ?"); 	

 	if(verif == true) 
	{ document.getElementById('valide').value="ok"; 
	  document.getElementById('mode').value="fermer"; 	
	} 	

	if(verif == false) 
	{ document.getElementById('valide').value="stop"; 	  }	
} 

function fct_annuler()
{	document.getElementById('valide').value="annule";
} 
</script>

<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>
	</ul>
	</div>
	<br>
	<br>
	
	<form action="" method="post" id="form_supprimer" name="form_supprimer"> 
	<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="400"> 
	<!--<table align="left"  border="0" bgcolor="#D5D5D5" width="400">-->	
	<tr bgcolor="#F1F3F5">

<?php
	if($mode == "ouvrir")
	{
		echo '<td>Ouvrir un créneau:</td>';		
	}
	if($mode == "fermer")
	{
		echo '<td>Fermer un créneau:</td>';		
	}
?>	
	
	</tr>
	</tr>	
	</td>
	</tr>

	<input type="hidden" name="valide" id="valide" value="">
	<input type="hidden" name="jour" id="jour" value="<?php echo $jour; ?>">	
	<input type="hidden" name="horaire" id="horaire" value="<?php echo $horaire; ?>">	
	<input type="hidden" name="mois" id="mois" value="<?php echo $mois; ?>">	
	<input type="hidden" name="mode" id="mode" value="">	
	
	<tr bgcolor="#FFFFFF">	
	<!--<td align="left" valign="top">Id :</td>-->	
	<td align="left" > 	
	<?php echo "Le " .$jour . " à " .$val; ?>
	</td>	
	</tr>

	<tr bgcolor="#FFFFFF">	
	<!--<td align="left" valign="top">Id :</td>-->	
<!--
	<td align="left" > 		
	Password: &nbsp;<input type="password" name="pass" id="pass" size="20" maxlength="20" value="" >	&nbsp;&nbsp;&nbsp; 
	Mode admin :<input type="checkbox" id ="admin" name="admin" value="">
	</td>	
-->	
	</tr>	
	
	<tr bgcolor='#F1F3F5'>
	<td colspan='2' align='center'>	

	<?php	
	if($mode == "ouvrir")
	{
		echo '<input type="submit" name="ajouter" value="Ouvrir" onClick="fct_ajouter()" />';
	}
	if($mode == "fermer")
	{
		echo '<input type="submit" name="supprimer" value="Fermer" onClick="fct_supprimer()" />';  	
	}
?>	
	
	<input type='submit' name='Annuler' value='Annuler' onClick="fct_annuler()" />  	

	</td>	
	</tr>
	</td>
	</tr>
	</table>

	</div>
	</div>
	</form>
	</body>
	</html>