<?php

	session_start ();
	
	include('database.php');
	include('fonctions.php');
	DB_connexion();
 	header( 'content-type: text/html; charset=utf-8' );
	
	$message = "";
	$browser = $_SERVER['HTTP_USER_AGENT'];
	//echo $browser;
	
	if (!isset($_SESSION['nom']) && !isset($_SESSION['password']))
	{  
			header("Location: login.php");   
	}
	
	$table = $_SESSION['table_reservation'];
	
	if(isset($_POST['valide']) && $_POST['valide'] == "ok")
	{ 
				$mois = $_POST['mois'];
				$jour = $_POST['jour'];
				$horaire = $_POST['horaire'];
				$client = $_POST['client'];
				
				$annee = date('Y');
								
				$sql = "SELECT * FROM " .$table ." WHERE rendezvous = '$jour' ";	
				//echo $sql;
				mysqli_query($connexion, "SET NAMES 'utf8'");
				$result = mysqli_query($connexion, $sql );
	
				if(!$result)
				{
					die('Impossible d\'exécuter la requête :' . mysql_error());
				}	

				$n = mysqli_num_rows($result);

				//echo "Resultat:  " .$n;
				
				$data = mysqli_fetch_assoc($result);
				
				//echo '<pre>';
				//print_r($data);	
					
				$tab_seance = array();
				$tab_seance[] = "decalage";

				for( $k=1; $k <9 ; $k++)
				{
					$tab_seance[] = "unselect";	
				}	
					
				$indice_debut =  preg_replace('/[^0-9]+/', '', $data['horairedebut']);
				$indice_fin =  preg_replace('/[^0-9]+/', '', $data['horairefin']);
				$val3 = $data['avant'];
				$val4 = $data['apres'];
				
				$tab_tmp = array();
				
		     	$tab_tmp[] = $data['seance1'];
				$tab_tmp[] = $data['seance2'];
				$tab_tmp[] = $data['seance3'];
				$tab_tmp[] = $data['seance4'];
				$tab_tmp[] = $data['seance5'];
				$tab_tmp[] = $data['seance6'];
				$tab_tmp[] = $data['seance7'];
				$tab_tmp[] = $data['seance8'];
				
				//echo '<pre>';
				//print_r($tab_tmp);	

				//echo $indice_debut ."  " .$indice_fin  ."  " .$val3 ."  " .$val4;
				//echo '<br>';			
				
				$supplement_avant_debut = $indice_debut - $val3;
				$supplement_avant_fin = $indice_debut - 1;
				$supplement_apres_debut = $indice_fin + 1;
				$supplement_apres_fin = $indice_fin + $val4;
				
				//echo $supplement_avant_debut ."  " .$supplement_avant_fin ."  " .$supplement_apres_debut ."  " .$supplement_apres_fin;

				for( $l=1; $l <9 ; $l++)
				{
							if( $l >= $indice_debut && $l <= $indice_fin)
							{
									$tab_seance[$l] = "";
							}	
							if( $l >= $supplement_apres_debut && $l <= $supplement_apres_fin)
							{
									$tab_seance[$l] = "creneau fermer";
							}	
							if( $l >= $supplement_avant_debut && $l <= $supplement_avant_fin)
							{
									$tab_seance[$l] = "creneau fermer";
							}	
				}			
			
				//echo '<pre>';
				//print_r($tab_seance);
				
				$indice_horaire =  preg_replace('/[^0-9]+/', '', $horaire);
				
				//echo $indice_horaire;
				//echo '<br>';
				//echo $tab_seance[$indice_horaire];
				
				if( strlen($tab_seance[$indice_horaire]) != 0)
				{ 
					$str = "creneau ouvert";
					$type_creneau = "etendu";
				}
				else
				{ 
					$str = "";
					$type_creneau = "normal";
				}
				
				// mise à jour 
				
				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());
				}		
				
				// mise à jour  gestion 
				$val = substr($horaire, 6);
				
				//$sql = "SELECT id FROM " .$_SESSION['table_gestion'] ." WHERE rendezvous = '$jour' ";	
				
								
				$sql = "SELECT id FROM copy_gestion WHERE jour ='$jour' and seance ='$val' and client ='$client' ";	
				//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 "+++++ " .$n; 
				$data = mysqli_fetch_assoc($result);
				$indice = $data['id'];
				$sql = "DELETE FROM copy_gestion WHERE id = $indice";
				//echo $sql;
				$result = mysqli_query($connexion, $sql );	

				//// mise à jour du compteur de crénaux 
				//if($horaire == "seance3" || $horaire == "seance4" || $horaire == "seance5" || $horaire == "seance6")
				if($type_creneau == "normal")
				{
					$sql = "select normal from " .$table ." WHERE rendezvous ='$jour' ";
					$result = mysqli_query($connexion, $sql );	
					$data = mysqli_fetch_assoc($result);
					if($data['normal'] != 0)
					{	$val = $data['normal'] - 1 ;
						$sql = "UPDATE " .$table ." SET normal = '$val' WHERE rendezvous ='$jour' ";
						$result = mysqli_query($connexion, $sql );
					}
				}	
				
				//if($horaire == "seance1" || $horaire == "seance2" || $horaire == "seance7" || $horaire == "seance8")
				if($type_creneau == "etendu")
				{				
					$sql = "select etendu from " .$table ." WHERE rendezvous ='$jour' ";
					$result = mysqli_query($connexion, $sql );	
					$data = mysqli_fetch_assoc($result);
					if($data['etendu'] != 0)
					{	$val = $data['etendu'] - 1 ;
						$sql = "UPDATE " .$table ." SET etendu = '$val' WHERE rendezvous ='$jour' ";
						$result = mysqli_query($connexion, $sql );
					}
				}
				
/////// ouverture automatique des créneaux supplémentaires

				nouvelle_allocation($mois);
				
				$site = $_SERVER["SERVER_NAME"];
				if( ($site == "localhost") || ($site == "127.0.0.1") ) 
				{
					$no_active = true;
				}
				else
				{	
					if($formule == "courte")
					{
						switch ($horaire)
						{
							case 	"seance1":
									$heure = "12H20 ";
									break;
							case	"seance2":
									$heure = "12H40 ";
									break;
							case	"seance3":
									$heure = "13H ";
									break;
							case	"seance4":
									$heure = "13H20 ";
									break;	
							case	"seance5":
									$heure = "13H40 ";
									break;
							case	"seance6":
									$heure = "14H ";
									break;
							case	"seance7":
									$heure = "14H20 ";
									break;
							case	"seance8":
									$heure = "14H40 ";
									break;		
						} 
					}		
					
					if($formule == "longue")
					{
						switch ($horaire)
						{
							case 	"seance1":
									$heure = "12H00 ";
									break;
							case	"seance2":
									$heure = "12H30 ";
									break;
							case	"seance3":
									$heure = "13H00 ";
									break;
							case	"seance4":
									$heure = "13H30 ";
									break;	
							case	"seance5":
									$heure = "14H00 ";
									break;
							case	"seance6":
									$heure = "14H30 ";
									break;
							case	"seance7":
									$heure = "15H00 ";
									break;
							case	"seance8":
									$heure = "15H30 ";
									break;		
						} 
					}		
	
					$decompose = explode(" ", $client);
					$nom = $decompose[0]; 
					$prenom = $decompose[1]; 
				
					$sql = "SELECT * FROM client WHERE nom ='$nom' AND prenom ='$prenom' ";	
					
					//echo $sql;
					$result = mysqli_query($connexion, $sql );	
					$data = mysqli_fetch_assoc($result);
					
					$debug_mode = $_SESSION['debug_mode'];
					if($debug_mode == "no")
					{
						email_html("supprimer", $nom , $prenom, $data['email'] , $jour, $heure);
					}	

				}
				
				header("Location: affiche_planning.php?mois=$mois");
				
	 }

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

 //////////////////////////  traitement lors du chargement de la page ///////////////////////////////////////////
	 
	 $mois = $_GET['mois'];	
	 $jour = $_GET['jour'];	
	 $horaire = $_GET['horaire'];	
	 $client = $_GET['client'];	
	 $formule = $_GET['formule'];	
	 
	 if($formule == "courte")
	 {
		switch ($horaire)
		{
			case "seance1":
				$val = "12H20 ";
				break;
			case "seance2":
				$val = "12H40 ";
				break;
			case "seance3":
				$val = "13H00 ";
				break;
			case "seance4":
				$val = "13H20 ";
				break;	
			case "seance5":
				$val = "13H40 ";
				break;
			case "seance6":
				$val = "14H00 ";
				break;
			case "seance7":
				$val = "14H20 ";
				break;
			case "seance8":
				$val = "14H40 ";
				break;		
		} 
	 }
	 
	 if($formule == "longue")
	 {
		switch ($horaire)
		{
			case "seance1":
				$val = "12H00 ";
				break;
			case "seance2":
				$val = "12H30 ";
				break;
			case "seance3":
				$val = "13H00 ";
				break;
			case "seance4":
				$val = "13H30 ";
				break;	
			case "seance5":
				$val = "14H00 ";
				break;
			case "seance6":
				$val = "14H30 ";
				break;
			case "seance7":
				$val = "15H00 ";
				break;
			case "seance8":
				$val = "15H30 ";
				break;		
		} 
	 }
?>


<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<title>Espace réservation</title>
</head>	

<style>

.css_message {	width: 500px;
				border-style: solid;
				border-width: 1px;
				border-color: #1c75c8;
				padding: 3px; 
				background-color: #c5ddf6; 
			}

.ui-widget-overlay {
  background: #000;
}
</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 class='active'><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>

<script type="text/javascript">

/***************************************************/

$(document).ready(function()
{
		$('#bouton_submit').click(effacer); 
		//$('#bouton_annuler').click(annuler); 
});
/***************************************************/

function effacer()
{
	
	$("#dialog-confirm").dialog({
					//autoOpen: false,  
					resizable: false,
					modal: true,
					title: "Confirmation",
					height: 250,
					width: 400,
					buttons: {
							"OK": function ()
							{
								
								document.getElementById('valide').value="ok";
								form_supprimer.submit();
								$(this).dialog('close');
								
								//callback(true);
							},
							"Fermer": function ()
							{
								$(this).dialog('close');
							}
					}
			});
			
			
			$("#dialog-confirm").html('Voulez-vous effacer ce rendez-vous ?.');
			$("#dialog-confirm" ).dialog("open");
			return false;
}

/***************************************************/

function annuler()
{
	  //window.history.back();
	  alert("Retour");
	  //history.back();
}

/***************************************************/


function fct_effacer()
{	
	verif = confirm("Voulez vous vraiment supprimer ce rendez-vous ?"); 	

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

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

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



	
	</head>
	<body>
	
	<!--<form action="" method="post" id="form_supprimer" name="form_supprimer"> -->
	<form action="#" name ="form_supprimer" method="post">
	<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">
	<td><?php echo $client; ?> souhaite annuler un rendez-vous:</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="client" id="client" value="<?php echo $client; ?>">		
	<input type="hidden" name="mois" id="mois" value="<?php echo $mois; ?>">	

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

	<input type="submit" value="Supprimer" class="button" id="bouton_submit" />
	<input type="button" value="Annuler" onClick="history.back()">

	
<!--	<input type="submit" value="Annuler" 	class="button" id="bouton_annuler" /> -->
<!--	<input type='submit' name='Annuler' value='Annuler' onClick="fct_annuler()" /> 
	<!--<input type='submit' name='effacer' value='Supprimer' onClick="fct_effacer()" /> -->
	<!--<input type='submit' name='Annuler' value='Annuler' onClick="fct_annuler()" />  -->	
	
	</td>	
	</tr>
	</td>
	</tr>
	</table>

	</div>
	</div>
	</form>
	
	<div id="dialog-confirm"></div>
	
<?php


				if( $message == "ok")
				{	
					echo '
							<div id="id_message" class="css_message">
							<p>
							<strong>Information</strong>: Modification du mot de passe effectuée avec succés.
							</p>
							<div>';
					$message = "none";
				}
?>	
	
	</body>
	</html>