<?php

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

	include('database.php');
	include('fonctions.php');
	//include('outlook03.php');
	
	include('test_calendar06.php');
	
	DB_connexion(); 
	header( 'content-type: text/html; charset=utf-8' );

	$seance_choix = "";
	$message = "";
	
	if(isset($_GET['mois']) && isset($_GET['jour']))
	{
		$mois = $_GET['mois'];
		$jour = $_GET['jour'];
	}	
	
	if(isset($_POST['submit']))
	{
		if(isset($_POST['verrou']) && $_POST['verrou'] == "ok")
		{	
				$annee = date('Y');
				$table = "reservation" .$annee;
				
				if( $_SESSION['nom'] == "admin")
				{	$client = $_POST['client'];	
					$sql = "SELECT * FROM client WHERE nom ='admin' ";	
					$result = mysqli_query($connexion, $sql );	
					$data = mysqli_fetch_assoc($result);
					$msg = "";
				}
		
				if( $_SESSION['nom'] != "admin")
				{	$nom = $_SESSION['nom'];	
					$prenom = $_SESSION['prenom'];	
					$client = $nom .' ' .$prenom; 				
					$sql = "SELECT * FROM client WHERE nom ='$nom' and prenom ='$prenom' ";	
					mysqli_query($connexion, "SET NAMES 'utf8'");	
					$result = mysqli_query($connexion, $sql );	
					$data = mysqli_fetch_assoc($result);
					$msg = "";
				}
				
				$sql = "SELECT * FROM " .$table  ." WHERE rendezvous ='$jour'";	
				$result = mysqli_query($connexion, $sql );
				$data = mysqli_fetch_assoc($result);
							
				//print_r($_POST['options']);
				$options_text = implode(', ',$_POST['options']);
								
				$tab_horaire = array();
				$tab_position = array();
				$tab_position = tableau_seance($mois,$jour);
				
				//echo '<pre>';
				//echo print_r($tab_position);
				// 3 4 5 6
				//   4 5 6 7
				$reponse = strpos($options_text,"seance1");
				if(strlen($reponse) != 0)
				{	//echo "seance1";
					//echo "<br>";
					$check_dispo = disponible("seance1", $jour);
					if($check_dispo == "libre")
					{
						mysqli_query($connexion, "SET NAMES 'utf8'");	
						$sql = "UPDATE " .$table ." SET seance1 = '$client' WHERE rendezvous ='$jour' ";
						//echo $sql;
						$result = mysqli_query($connexion, $sql );	
						$msg .= "12H20 ";
						$tab_horaire[] = "12H20";
						$val = 1;
						if($tab_position[$val] == "normale")
						{
							mode_normal($jour);
						}	
						if($tab_position[$val] == "etendu")
						{
							mode_etendu($jour);
						}	
						$seance_choix = "1";
						//// 	mise à jour de la table gestion
						sauvegarde_table_gestion($annee,$mois,$jour,$client,$seance_choix);
					}
					else
					{ 
						echo "Database mise à jour: Le créneau de 12H20 n'est plus disponible !.";
						echo '<br>';		
					}		
				}
				
				// 3 4 5 6
				//   4 5 6 7 
				$reponse = strpos($options_text,"seance2");
				if(strlen($reponse) != 0)
				{	//echo "seance2";
					//echo "<br>";
					$check_dispo = disponible("seance2", $jour);
					if($check_dispo == "libre")
					{
						mysqli_query($connexion, "SET NAMES 'utf8'");	
						$sql = "UPDATE " .$table ." SET seance2 = '$client' WHERE rendezvous ='$jour' ";
						//echo $sql;
						$result = mysqli_query($connexion, $sql );	
						$msg .= "12H40 ";
						$tab_horaire[] = "12H40";
						$val = 2;
						if($tab_position[$val] == "normale")
						{
							mode_normal($jour);
						}	
						if($tab_position[$val] == "etendu")
						{
							mode_etendu($jour);
						}	
						$seance_choix = "2";
						//// 	mise à jour de la table gestion
						sauvegarde_table_gestion($annee,$mois,$jour,$client,$seance_choix);
					}
					else
					{ 
						echo "Database mise à jour: Le créneau de 12H40 n'est plus disponible !.";
						echo '<br>';		
					}		
	
				}
					
				// 3 4 5 6
				//   4 5 6 7	
				$reponse = strpos($options_text,"seance3");
				if(strlen($reponse) != 0)
				{	//echo "seance3";
					//echo "<br>";
					$check_dispo = disponible("seance3", $jour);
					if($check_dispo == "libre")
					{
						mysqli_query($connexion, "SET NAMES 'utf8'");	
						$sql = "UPDATE " .$table ." SET seance3 = '$client' WHERE rendezvous ='$jour' ";
						//echo $sql;
						$result = mysqli_query($connexion, $sql );	
						$msg .= "13H ";
						$tab_horaire[] = "13H";
						$val = 3;
						if($tab_position[$val] == "normale")
						{
							mode_normal($jour);
						}	
						if($tab_position[$val] == "etendu")
						{
							mode_etendu($jour);
						}	
						$seance_choix = "3";
						//// 	mise à jour de la table gestion
						sauvegarde_table_gestion($annee,$mois,$jour,$client,$seance_choix);
					}
					else
					{ 
						echo "Database mise à jour: Le créneau de 13H00 n'est plus disponible !.";
						echo '<br>';		
					}		
				}
				
				// 3 4 5 6
				//   4 5 6 7
				$reponse = strpos($options_text,"seance4");
				if(strlen($reponse) != 0)
				{	//echo "seance4";
					//echo "<br>";
					$check_dispo = disponible("seance4", $jour);
					if($check_dispo == "libre")
					{
						mysqli_query($connexion, "SET NAMES 'utf8'");	
						$sql = "UPDATE " .$table ." SET seance4 = '$client' WHERE rendezvous ='$jour' ";
						//echo $sql;
						$result = mysqli_query($connexion, $sql );	
						$msg .= "13H20 ";
						$tab_horaire[] = "13H20";
						$val = 4;
						if($tab_position[$val] == "normale")
						{
							mode_normal($jour);
						}	
						if($tab_position[$val] == "etendu")
						{
							mode_etendu($jour);
						}	
						$seance_choix = "4";
						//// 	mise à jour de la table gestion
						sauvegarde_table_gestion($annee,$mois,$jour,$client,$seance_choix);
					}
					else
					{ 
						echo "Database mise à jour: Le créneau de 13H20 n'est plus disponible !.";
						echo '<br>';		
					}							
				}
				
				// 3 4 5 6
				//   4 5 6 7
				$reponse = strpos($options_text,"seance5");
				if(strlen($reponse) != 0)
				{	//echo "seance5";
					//echo "<br>";
					$check_dispo = disponible("seance5", $jour);
					if($check_dispo == "libre")
					{
						mysqli_query($connexion, "SET NAMES 'utf8'");	
						$sql = "UPDATE " .$table ." SET seance5 = '$client' WHERE rendezvous ='$jour' ";
						//echo $sql;
						$result = mysqli_query($connexion, $sql );	
						$msg .= "13H40 ";
						$tab_horaire[] = "13H40";
						$val = 5;
						if($tab_position[$val] == "normale")
						{
							mode_normal($jour);
						}	
						if($tab_position[$val] == "etendu")
						{
							mode_etendu($jour);
						}	
						$seance_choix = "5";
						//// 	mise à jour de la table gestion
						sauvegarde_table_gestion($annee,$mois,$jour,$client,$seance_choix);
					}
					else
					{ 
						echo "Database mise à jour: Le créneau de 13H40 n'est plus disponible !.";
						echo '<br>';		
					}							
				}
				
				// 3 4 5 6
				//   4 5 6 7
				$reponse = strpos($options_text,"seance6");
				if(strlen($reponse) != 0)
				{	//echo "seance6";
					//echo "<br
					$check_dispo = disponible("seance6", $jour);
					if($check_dispo == "libre")
					{
						mysqli_query($connexion, "SET NAMES 'utf8'");	
						$sql = "UPDATE " .$table ." SET seance6 = '$client' WHERE rendezvous ='$jour' ";
						//echo $sql;
						$result = mysqli_query($connexion, $sql );	
						$msg .= "14H ";
						$tab_horaire[] = "14H";
						$val = 6;
						if($tab_position[$val] == "normale")
						{
							mode_normal($jour);
						}	
						if($tab_position[$val] == "etendu")
						{
							mode_etendu($jour);
						}	
						$seance_choix = "6";
						//// 	mise à jour de la table gestion
						sauvegarde_table_gestion($annee,$mois,$jour,$client,$seance_choix);
					}
					else
					{ 
						echo "Database mise à jour: Le créneau de 14H00 n'est plus disponible !.";
						echo '<br>';		
					}							
				}
				
				// 3 4 5 6
				//   4 5 6 7
				$reponse = strpos($options_text,"seance7");
				if(strlen($reponse) != 0)
				{	//echo "seance7";
					//echo "<br>";
					$check_dispo = disponible("seance7", $jour);
					if($check_dispo == "libre")
					{
						$sql = "UPDATE ".$table ." SET	seance7 = '$client' WHERE rendezvous ='$jour' ";
						//echo $sql;
						mysqli_query($connexion, "SET NAMES 'utf8'");	
						$result = mysqli_query($connexion, $sql );	
						$msg .= "14H20 ";
						$tab_horaire[] = "14H20";
						$val = 7;
						if($tab_position[$val] == "normale")
						{
							mode_normal($jour);
						}	
						if($tab_position[$val] == "etendu")
						{
							mode_etendu($jour);
						}	
						$seance_choix = "7";
						//// 	mise à jour de la table gestion
						sauvegarde_table_gestion($annee,$mois,$jour,$client,$seance_choix);
					}
					else
					{ 
						echo "Database mise à jour: Le créneau de 14H20 n'est plus disponible !.";
						echo '<br>';		
					}							
				}
			
				// 3 4 5 6
				//   4 5 6 7
				$reponse = strpos($options_text,"seance8");
				if(strlen($reponse) != 0)
				{	//echo "seance8";
					//echo "<br>";
					$check_dispo = disponible("seance8", $jour);
					if($check_dispo == "libre")
					{
						$sql = "UPDATE ".$table ." SET	seance8 = '$client' WHERE rendezvous ='$jour' ";
						//echo $sql;
						mysqli_query($connexion, "SET NAMES 'utf8'");	
						$result = mysqli_query($connexion, $sql );	
						$msg .= "14H40 ";
						$tab_horaire[] = "14H40";
						$val = 8;
						if($tab_position[$val] == "normale")
						{
							mode_normal($jour);
						}	
						if($tab_position[$val] == "etendu")
						{
							mode_etendu($jour);
						}	
						$seance_choix = "8";
						//// 	mise à jour de la table gestion
						sauvegarde_table_gestion($annee,$mois,$jour,$client,$seance_choix);
					}
					else
					{ 
						echo "Database mise à jour: Le créneau de 14H40 n'est plus disponible !.";
						echo '<br>';		
					}							
				}
			
			
				if($check_dispo == "libre")
				{
					// recherche l'adresse email du client
					$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);
				
					nouvelle_allocation($mois);
					//allocation($mois);
				
					$str = implode(",", $tab_horaire);
								
					//echo "Confirmation: "  .$client ." vient de réserver " .count($_POST['options']) . " créneau(x)" .' - ' .$str . " pour le " .$jour;
				
					$site = $_SERVER["SERVER_NAME"];
					if( $site != "localhost" ) //|| ($site != "127.0.0.1") ) 
					{
						//outlook($prenom, $data['email'] , $jour, $str);
						$nothing = "none";
					}
					else
					{
						echo "Mode localhost";
					}

					/*else
					{	//if($_SESSION['debug'] == "no_debug")
						//{			
						//echo "Production";
						outlook($prenom, $data['email'] , $jour, $str);
						echo '<br><br>';
						echo "Email envoyé + invitation Calendar";
						//email_html("reserver", $nom , $prenom, $data['email'] , $jour, $msg);
						//}
					}*/
				}
				
				$message = "ok";
				header ("Refresh: 5;URL=affiche_planning.php?mois=$mois"); 
				
				//header("Location: affiche_planning.php?mois=$mois");
				
				
		}
	}
?>

<html>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!--
<meta http-equiv="Refresh" content="5">
-->
<head>

<style>

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

.ui-widget-overlay {
  background: #000;
}
</style>

<script>


function test()
{
var nbreMillisec = 5000;
setTimeout("test();", nbreMillisec);
window.location.reload()

}
//test();

function auto_refresh(interval) {
	setTimeout("location.reload(true);",interval);
}	

function valider_admin() 
{
	   var verif = true;
	   var msg_erreur = new Array;
   
	   var list_two = document.getElementById("champ_client");
	   var indx = list_two.selectedIndex;
	   if(indx < 0)
	   {	verif = false;
			msg_erreur[1] = "Choisir un client !";
	   }
	   
	   var  element = document.getElementById("id1")
	   if(element == null)
	   { id1 = false;
	   }
	   else
	   { var id1 = document.getElementById("id1").checked;
	   }
	   
	   var  element = document.getElementById("id2")
	   if(element == null)
	   { id2 = false;
	   }
	   else
	   { var id2 = document.getElementById("id2").checked;
	   }
	   
	   var  element = document.getElementById("id3")
	   if(element == null)
	   { id3 = false;
	   }
	   else
	   { var id3 = document.getElementById("id3").checked;
	   }
	   
	   var  element = document.getElementById("id4")
	   if(element == null)
	   { id4 = false;
	   }
	   else
	   { var id4 = document.getElementById("id4").checked;
	   }
	   
	   var element = document.getElementById("id5")
	   if(element == null)
	   { id5 = false;
	   }
	   else
	   { var id5 = document.getElementById("id5").checked;
	   }
	   
	   var  element = document.getElementById("id6")
	   if(element == null)
	   { id6 = false;
	   }
	   else
	   { var id6 = document.getElementById("id6").checked;
	   }
	   
	   var  element = document.getElementById("id7")
	   if(element == null)
	   { id7 = false;
	   }
	   else
	   { var id7 = document.getElementById("id7").checked;
	   }
	   
	    var  element = document.getElementById("id8")
	   if(element == null)
	   { id8 = false;
	   }
	   else
	   { var id8 = document.getElementById("id8").checked;
	   }
	   
	   if(id1 == false && id2 == false && id3 == false && id4 == false && id5 == false && id6 == false && id7 == false && id8 == false)
	   {	verif = false;
			msg_erreur[2] = "Aucun créneau selectionné !";
	   }
	   
	  /* var  element = document.getElementById("admin").checked;
	   if(element == true)
	   {
		   //alert("Mode administrateur");
		   document.getElementById('user').value="admin";
		   alert("admin");
	   }
	   else
	   {  document.getElementById('user').value="";
		  alert("Utilisateur");
	   }*/
	   
	   
	  if(verif == true)
 	  { document.getElementById('verrou').value="ok";
 	  }
 	
	  if(verif == false)
 	  { var affiche = msg_erreur.join("\n");
		alert(affiche);
		document.getElementById('verrou').value="erreur";
 	  }	
  
}
function valider_utilisateur() 
{
	   var verif = true;
	   var msg_erreur = new Array;
	   
	   var  element = document.getElementById("id1")
	   if(element == null)
	   { id1 = false;
	   }
	   else
	   { var id1 = document.getElementById("id1").checked;
	   }
	   
	   var  element = document.getElementById("id2")
	   if(element == null)
	   { id2 = false;
	   }
	   else
	   { var id2 = document.getElementById("id2").checked;
	   }
	   
	   var  element = document.getElementById("id3")
	   if(element == null)
	   { id3 = false;
	   }
	   else
	   { var id3 = document.getElementById("id3").checked;
	   }
	   
	   var  element = document.getElementById("id4")
	   if(element == null)
	   { id4 = false;
	   }
	   else
	   { var id4 = document.getElementById("id4").checked;
	   }
	   
	   var element = document.getElementById("id5")
	   if(element == null)
	   { id5 = false;
	   }
	   else
	   { var id5 = document.getElementById("id5").checked;
	   }
	   
	   var  element = document.getElementById("id6")
	   if(element == null)
	   { id6 = false;
	   }
	   else
	   { var id6 = document.getElementById("id6").checked;
	   }
	   
	   var  element = document.getElementById("id7")
	   if(element == null)
	   { id7 = false;
	   }
	   else
	   { var id7 = document.getElementById("id7").checked;
	   }
	   
	    var  element = document.getElementById("id8")
	   if(element == null)
	   { id8 = false;
	   }
	   else
	   { var id8 = document.getElementById("id8").checked;
	   }
	   
	   if(id1 == false && id2 == false && id3 == false && id4 == false && id5 == false && id6 == false && id7 == false && id8 == false)
	   {	verif = false;
			msg_erreur[2] = "Aucun créneau selectionné !";
	   }
	   
	  /* var  element = document.getElementById("admin").checked;
	   if(element == true)
	   {
		   //alert("Mode administrateur");
		   document.getElementById('user').value="admin";
		   alert("admin");
	   }
	   else
	   {  document.getElementById('user').value="";
		  alert("Utilisateur");
	   }*/
	   
	   
	  if(verif == true)
 	  { document.getElementById('verrou').value="ok";
 	  }
 	
	  if(verif == false)
 	  { var affiche = msg_erreur.join("\n");
		alert(affiche);
		document.getElementById('verrou').value="erreur";
 	  }	
  
}
</script>

</head>
<!-- pour activer un rafraichissement automatique
<body onload="JavaScript: auto_refresh(5000);">
-->
<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 class="active"><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="#" name ="form_inscription" method="post">
	<input type="hidden" name="verrou" id="verrou" value="">
	
<?php
////////////////  affichage de la liste de selection des utilisateurs seulement pour le mode administrateur
	if($_SESSION['nom'] == "admin")
	{	
		echo '<select size = "10" STYLE="width:300" id = "champ_client" name="client">';

		$sql = 'SELECT * FROM client';
		mysqli_query($connexion, "SET NAMES 'utf8'");
		$result = mysqli_query($connexion, $sql );
		$tab_nom = array();
		$tab_prenom = array();
	
		$ptr = 0;
			
		while ($data = mysqli_fetch_assoc ($result))
		{
			if(strlen($data['prenom']) != 0)
			{
				$tab_nom[$ptr] = $data['nom'];
				$tab_prenom[$ptr] = $data['prenom'];
				$ptr = $ptr + 1;
			}	
		}	

		for( $i=0; $i < count($tab_nom) ; $i++)
		{
			$tmp = $tab_nom[$i] . " " .$tab_prenom[$i];
			echo '<option value="' .$tmp. '">' .$tmp. '</option>';
		}

		echo '</select>
			  <br><br>';
			  
	}
///////////////////////////////////////	
	
	echo 'Mode administrateur - Réservation pour le : ' .$jour .'<br>';
	
	$annee = date('Y');
	$table = "reservation" .$annee;
	$sql = "SELECT * FROM " .$table  ." WHERE rendezvous ='$jour' ";	
	//echo $sql;
	$result = mysqli_query($connexion, $sql );
	$n = mysqli_num_rows($result);	
	$data = mysqli_fetch_assoc($result);
	
	$item1 = "";
	$item2 = "";
	$item3 = "";
	$item4 = "";
	$item5 = "";
	$item6 = "";
	$item7 = "";
	$item8 = "";
	
	echo '<br>';
	
	//if(strlen($data['seance1']) == 0)
	if($data['seance1'] =='' || $data['seance1'] == 'creneau ouvert' )
	{	$item1 = "libre";
		echo '<input type="checkbox" id ="id1" name="options[]" value="seance1">12H20';
	}	
	else
	{  $item1 = "pris";
	}
	
	//if(strlen($data['seance2']) == 0)
	if($data['seance2'] =='' || $data['seance2'] == 'creneau ouvert' )
	{	$item2 = "libre";
		echo '<input type="checkbox" id ="id2" name="options[]" value="seance2">12H40';
	}	
	else
	{  $item2 = "pris";
	}
	
	//if(strlen($data['seance3']) == 0)
	if($data['seance3'] =='' || $data['seance3'] == 'creneau ouvert' )
	{	$item3 = "libre";
		echo '<input type="checkbox" id ="id3" name="options[]" value="seance3">13H';
	}	
	else
	{  $item3 = "pris";
	}

	//if(strlen($data['seance4']) == 0)
	if($data['seance4'] =='' || $data['seance4'] == 'creneau ouvert' )
	{	$item4 = "libre";
		echo '<input type="checkbox" id ="id4" name="options[]" value="seance4">13H20';
	}	
	else
	{  $item4 = "pris";
	}	
	
	//if(strlen($data['seance5']) == 0)
	if($data['seance5'] =='' || $data['seance5'] == 'creneau ouvert' )
	{	$item5 = "libre";
		echo '<input type="checkbox" id ="id5" name="options[]" value="seance5">13H40';
	}	
	else
	{  $item5 = "pris";
	}
	
	//if(strlen($data['seance6']) == 0)
	if($data['seance6'] =='' || $data['seance6'] == 'creneau ouvert' )
	{	$item6 = "libre";
		echo '<input type="checkbox" id ="id6" name="options[]" value="seance6">14H';
	}	
	else
	{  $item6 = "pris";
	}
	
	//if(strlen($data['seance7']) == 0)
	if($data['seance7'] =='' || $data['seance7'] == 'creneau ouvert' )
	{	$item7 = "libre";
		echo '<input type="checkbox" id ="id7" name="options[]" value="seance7">14H20';
	}	
	else
	{  $item7 = "pris";
	}

	//if(strlen($data['seance8']) == 0)
	if($data['seance8'] =='' || $data['seance8'] == 'creneau ouvert' )
	{	$item8 = "libre";
		echo '<input type="checkbox" id ="id8" name="options[]" value="seance8">14H40';
	}	
	else
	{  $item8 = "pris";
	}	
	
	
	if($item1 == "pris" && $item2 == "pris" && $item3 == "pris" && $item4 == "pris" && $item5 == "pris" && $item6 == "pris" && $item7 == "pris" && $item8 == "pris")
	{ 	echo "Plus de créneaux disponible à cette date !.";
		echo '<br>';
	}
	
	
?>	

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

<?php
	if($_SESSION['nom'] == "admin")
	{	
		echo '<input type="submit" name="submit" value="Validation" onclick="valider_admin()"/>';
	}
	else
	{	
		echo '<input type="submit" name="submit" value="Validation" onclick="valider_utilisateur()"/>';
	}
?>
	</form>

	<br>
<?php

	
	if( $message == "ok")
	{	
		echo '
				<div id="id_message" class="css_message">
				<p>
				<strong>Information</strong>: ';
		echo    $client ." vient de réserver " .count($_POST['options']) . " créneau(x)" .' - ' .$str . " pour le " .$jour;
		echo 	'<br><br>';
		echo 	"Pas d' email envoyé";
		echo '	</p>
				<div>';
				
		$message = "";
	}


//	echo '<a href="selection_mois.php?mois='.$mois .' ">Retour au planning </a>';
						
 ?> 

</body>
</html>