<?php

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

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

	$utilisateur = $_SESSION['nom'] .' ' .$_SESSION['prenom'];
	
?>

<!DOCTYPE html>
<html>
<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}


.titre {
	margin-left:50px;
	margin-top:50px;
	font-size: 26px;
	}


a:link { color: black;
 text-decoration:none; 
} 
a:visited {color: black;
text-decoration:none;}
a:hover {color: black;
text-decoration:none;}
a:active {color: black;
		text-decoration:none;	}

</style>
</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 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>
		<li><a href='#'><span><?php echo $_SESSION['year']; ?></span></a></li>
	</ul>
	</div>
	<br>
	<br>

<?php

	$mois = $_GET['mois'];
	$annee = $_SESSION['year'];
	//$annee = date('Y');
	
	if( $_SESSION['nom'] == "admin")
	{
		if($_SESSION['debug_mode'] == "yes")
		{
			echo 'Mode debug';
			echo '<br><br>';
		}	
	}
	
	
	//echo $mois_actif;
	//echo ucfirst($mois) ." " .$annee;
	//echo '<br><br>';
	
	//echo "Nous sommes le " .date("d-m-Y");
	date_default_timezone_set('Europe/Paris');
	$ts_du_jour = strtotime(date("d-m-Y"));
	
	
	//$table = "reservation_dev" .$annee;
	//$table = $_SESSION['table_reservation']; 
	$table = $_SESSION['table_reservation'] = "reservation" .$_SESSION['year'] ."_v3";
	
	//echo "******* -> " .$table;
	
	
	
	//$table = "reservation" .$annee;  // nouvelle possibilitée
	
//	$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());
//	}

/*	$n = mysqli_num_rows($result);
	echo " ---> " .$n;
	
	$cpt = 0;
	$tab_jour = array();
	$tab_ts = array();
	$tab_cpt_par_semaine = array();
	
	// compte le nombre de reservation par semaine 
	while ($datas = mysqli_fetch_assoc ($result))
	{
		$cpt = 0;
		$tab_jour[] = $datas['jour'];
		$seance1 = $datas['seance1'];
		$seance2 = $datas['seance2'];
		$seance3 = $datas['seance3'];
		$seance4 = $datas['seance4'];
		$seance5 = $datas['seance5'];
		$seance6 = $datas['seance6'];
		$seance7 = $datas['seance7'];
		$seance8 = $datas['seance8'];
		
		if(strlen($seance1) !=0 && $seance1 != "unselect")
		{ $cpt = $cpt + 1;
		}
		if(strlen($seance2) !=0 && $seance2 != "unselect")
		{ $cpt = $cpt + 1;
		}
		if(strlen($seance3) != 0 )
		{ $cpt = $cpt + 1;
		}
		if(strlen($seance4) != 0 )
		{ $cpt = $cpt + 1;
		}
		if(strlen($seance5) != 0 )
		{ $cpt = $cpt + 1;
		}
		if(strlen($seance6) != 0)
		{ $cpt = $cpt + 1;
		}
		if(strlen($seance7) != 0 && $seance7 != "unselect")
		{ $cpt = $cpt + 1;
		}
		if(strlen($seance8) != 0 && $seance8 != "unselect")
		{ $cpt = $cpt + 1;
		}
		
		echo $cpt;
		$tab_cpt_par_semaine[]=$cpt;
	}
	
	for( $i=0; $i < count($tab_jour) ; $i++)		
	{	
         	$tab_ts[] = strtotime($tab_jour[$i]);
			echo $tab_jour[$i]; 
			echo '<br>';
	}
*/	
//////////////////////////////////////////////////////////////////////////	
	///
/*	
	$sql="SELECT * FROM pipo ";
	$result=@mysql_query($sql);
	if (!$result)
	{
		echo "No table exists";
	}	
	
	$sql="SELECT * FROM tarif ";
	$result=@mysql_query($sql);
	echo $result;
	if (!$result)
	{
		echo "kjkjkjkjjlkj";
	}
	else
	{
		echo "bingo";
	}
	
	$dbname   = "test";
	
	$sql = "SHOW TABLES FROM $dbname";
	$result = mysql_query($sql);
	
	$liste_table_DB = array();
	
	$cpt = 0;
	while ($row = mysql_fetch_row($result)) 
	{
		$element = $row[0];
		echo $element;
		echo "<br>";
		//liste_table_DB[cpt] = $element;
		$cpt = $cpt + 1;
		//echo "Table : $row[0]";
		//echo "<br>";
	}
	
	echo '<pre>';
	print_r($liste_table_DB);
	
	
		
	$ts_du_jour = strtotime(date("d-m-Y"));
	
	echo "<br>";
	
	echo $table;
	
	if(!(mysql_query("SELECT * FROM $table")))
    {
		//$str = 'Arnold a dit : "I\'ll be back"';
		$str = " La database " .$_SESSION['year'] ." n'existe pas encors !";
		die($str);
    } 
*/	

	//echo getcwd();
/*		
	$sql = "SELECT * FROM " .$table;
	$result = mysqli_query($connexion, $sql );
	
	if($result == null)
	{
		$table_nom = substr( $table, 0, -3 );
		$str = "&nbsp;&nbsp; &nbsp;&nbsp;    La table " .$table_nom ." n'exite pas configurée!."; 
		echo "<p><font size='5'>";
		die($str);
		echo "</p>";
		echo "<br>";
	}
*/	
	
	$table = $_SESSION['table_reservation'] = "reservation" .$_SESSION['year'] ."_v3";
		// echo $table;
		 
		if(!(mysqli_query($connexion, "SELECT * FROM $table")))
		{
			//$str = 'Arnold a dit : "I\'ll be back"';
			echo "<div class ='titre' id ='titre'>";
			$str = " La database " .$_SESSION['year'] ." n'existe pas encore !";
			die($str);
			echo "</div>";
		} 
	
	
	//echo $mois_actif;
	echo ucfirst($mois) ." " .$annee;
	echo '<br><br>';
		
	$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and formule = 'longue' and mois ='$mois' and mode = 'actif' ";	
	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);
	
	$flag_setting_longue = "";
		
	if($n != 0)
	{
			$flag_setting_longue = "ok";
		
			echo "Scéances de 30 min sur table";
	
			echo '	<div id="list">
					<table align="left" bgcolor="#d5d5d5" border="0" cellpadding="5" cellspacing="1" width="100%">
					<tbody><tr bgcolor="#f1f3f5">
					<th align="center" width="1%">Sem</th>
					<th align="center" width="5%">Date</th>
					<th align="center" width="8%">12H00</th>
					<th align="center" width="8%">12H30</th>
					<th align="center" width="8%">13H</th>
					<th align="center" width="8%">13H30</th>
					<th align="center" width="8%">14H00</th>
					<th align="center" width="8%">14H30</th>
					<th align="center" width="8%">15H00</th>
					<th align="center" width="8%">15H30</th>
					<th class="center" width="1%">Réservation</th>
					</tr>';	
	
			while ($datas = mysqli_fetch_assoc ($result))
			{
		     	$id = $datas['id'];
		     	$rendezvous = $datas['rendezvous'];
				$mode = $datas['mode'];
				$jour = $datas['jour'];
		     	$ts = $datas['ts'];
				$semaine = $datas['semaine'];
		     	$seance1 = $datas['seance1'];
				$seance2 = $datas['seance2'];
				$seance3 = $datas['seance3'];
				$seance4 = $datas['seance4'];
				$seance5 = $datas['seance5'];
				$seance6 = $datas['seance6'];
				$seance7 = $datas['seance7'];
				$seance8 = $datas['seance8'];
				
				switch ($jour)
				{
					case "Lundi":
						 $jour_sem = "LUN ";
						 break;
					case "Mardi":
						 $jour_sem = "MAR ";
						 break;
					case "Mercredi":
						 $jour_sem = "MER ";
						 break;						 
					case "Jeudi":
						 $jour_sem = "JEU ";
						 break;
					case "Vendredi":
						 $jour_sem = "VEN ";
						 break;
				} 
				
				//$mois
				$jour_du_mois =  substr($rendezvous, 0, 2);		
				$date = $jour_sem ." " .$jour_du_mois ." " .ucfirst($mois);
			
				echo '
						<tr>
						<td align="center" nowrap="nowrap">'.$semaine.'</td>
						<td align="center" nowrap="nowrap">'.$date.'</td>';		
					
					for( $i=1; $i <= 8 ; $i++)		
					{    
						${'text'.$i} = "seance".$i;
						$val = ${'text'.$i};
						$n = 0;			
						
						$test = strpos($datas[$val], " ");
						if($test != 0)
						{ 	//echo $datas[$val];
							//echo '<br>';
							if($datas[$val] != "creneau fermer" && $datas[$val] != "creneau ouvert")
							{
								if( $_SESSION['nom'] != "admin")
							    {	
									if($ts < $ts_du_jour)
									{
										echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
									}
									else
									{
										echo '<td  bgcolor ="#E84A35" align="center"><a href="supprimer.php?formule=longue&mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&client='.$datas[$val].'" title="Suppression">' .$datas[$val]. '</td>';
									}
								}
							    //else 
							    //{
							    //  echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
							   // }
							
							    if($_SESSION['nom'] == "admin")
							    {	
									echo '<td  bgcolor ="#E84A35" align="center"><a href="supprimer.php?formule=longue&mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&client='.$datas[$val].'" title="Suppression">' .$datas[$val]. '</td>';
							    }	
								
							
							}
						}
						
						if($datas[$val] == '')
						{	
							echo '<td bgcolor ="#84E673" align="center"></td>';
						}
						if($datas[$val] == "unselect")
						{ 
							echo '	<td  bgcolor ="#E6BA39" align="center">Inactif</td>';
						}
						if($datas[$val] == "creneau fermer")
						{   echo '<td  bgcolor ="#E6FF39" align="center">Supplement fermé</td>';
						}
						if($datas[$val] == "creneau ouvert")
						{  	echo '<td bgcolor ="#84E673" align="center"></td>';
						}
							
/*						else
						{   
					
							if($datas[$val] == "unselect")
							{ 	if($_SESSION['nom'] == "admin")
								{
									echo '<td  bgcolor ="#E6BA39" align="center"><a href="ouvrir.php?mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'" title="Ouverture d\'un nouveau créneau">Inactif</td>';
							    }   
								else
								{
									 echo '	<td  bgcolor ="#E6BA39" align="center">Inactif</td>';
								}	
							}
							else
							{
								
						       if($datas[$val] == $utilisateur || $_SESSION['nom'] == "admin")
							   {	
									if($ts < $ts_du_jour)
									{
										echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
									}
									else
									{
										echo '<td  bgcolor ="#E84A35" align="center"><a href="supprimer.php?mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&client='.$datas[$val].'" title="Suppression">' .$datas[$val]. '</td>';
									}
								}
							    else 
							    {
							      echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
							    }
								
							}			
						}*/
					}// for	
						

////////// bloquage si le jour est derriére nous ...	

					
						if($ts >= $ts_du_jour && $_SESSION['nom'] != "admin" )				
						{		
							echo '
									<td align="center" width="1%">
									<a href="reservation_longue.php?formule=longue&mois='.$mois.'&jour='.$rendezvous.'">
									<img src="calendar.png" border="0" alt="Réserver" title="Réservation"></a>
									</td>';
						}
						if($ts < $ts_du_jour && $_SESSION['nom'] != "admin")	
						{ 
							echo '<td align="center" width="1%">
								  <img src="delete.png" border="0" alt="impossible" title="impossible">
								  </td>';
						}	
					 
						if($_SESSION['nom'] == "admin")
						{
							echo '
									<td align="center" width="1%">
									<a href="reservation_longue.php?formule=longue&mois='.$mois.'&jour='.$rendezvous.'">
									<img src="calendar.png" border="0" alt="Réserver" title="Réservation"></a>
									</td>';
						}
			
			}	// while			

		echo '	<tr bgcolor="#f1f3f5">
				<td colspan="11" class="center">&nbsp;</td>
				</tr>
				</tbody>
				</table>
				</div>  ';
	
	}
	else
	{
//		echo "Ce mois n'est pas configuré.";
		//echo "Ce mois ne comporte pas de scéances ayant une durée de 30 min !.";
		$flag_setting_longue = "none";
	}

	
	/*************************************************/
	/*if($n != 0)
	{
		echo '<br><br>';
		echo "&nbsp;";
		echo '<br><br>';
	}*/
	
	$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and formule = 'courte' and mois ='$mois' and mode = 'actif' ";	
	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);
	
	$flag_setting_courte = "";
		
	if($n != 0)
	{
		/*	echo '<br><br>';
		echo "&nbsp;";
		echo '<br><br>';*/
	
			$flag_setting_courte = "ok";		
			
			echo "Scéances de 20 min";
			
	
			echo '	<div id="list">
					<table align="left" bgcolor="#d5d5d5" border="0" cellpadding="5" cellspacing="1" width="100%">
					<tbody><tr bgcolor="#f1f3f5">
					<th align="center" width="1%">Sem</th>
					<th align="center" width="5%">Date</th>
					<th align="center" width="8%">12H20</th>
					<th align="center" width="8%">12H40</th>
					<th align="center" width="8%">13H</th>
					<th align="center" width="8%">13H20</th>
					<th align="center" width="8%">13H40</th>
					<th align="center" width="8%">14H</th>
					<th align="center" width="8%">14H20</th>
					<th align="center" width="8%">14H40</th>
					<th class="center" width="1%">Réservation</th>
					</tr>';	
	
			while ($datas = mysqli_fetch_assoc ($result))
			{
		     	$id = $datas['id'];
		     	$rendezvous = $datas['rendezvous'];
				$mode = $datas['mode'];
				$jour = $datas['jour'];
		     	$ts = $datas['ts'];
				$semaine = $datas['semaine'];
		     	$seance1 = $datas['seance1'];
				$seance2 = $datas['seance2'];
				$seance3 = $datas['seance3'];
				$seance4 = $datas['seance4'];
				$seance5 = $datas['seance5'];
				$seance6 = $datas['seance6'];
				$seance7 = $datas['seance7'];
				$seance8 = $datas['seance8'];
				
				switch ($jour)
				{
					case "Lundi":
						 $jour_sem = "LUN ";
						 break;
					case "Mardi":
						 $jour_sem = "MAR ";
						 break;
					case "Mercredi":
						 $jour_sem = "MER ";
						 break;						 
					case "Jeudi":
						 $jour_sem = "JEU ";
						 break;
					case "Vendredi":
						 $jour_sem = "VEN ";
						 break;
				} 
				
				//$mois
				$jour_du_mois =  substr($rendezvous, 0, 2);		
				$date = $jour_sem ." " .$jour_du_mois ." " .ucfirst($mois);
			
				echo '
						<tr>
						<td align="center" nowrap="nowrap">'.$semaine.'</td>
						<td align="center" nowrap="nowrap">'.$date.'</td>';		
					
					for( $i=1; $i <= 8 ; $i++)		
					{    
						${'text'.$i} = "seance".$i;
						$val = ${'text'.$i};
						$n = 0;			
						
						$test = strpos($datas[$val], " ");
						if($test != 0)
						{ 	//echo $datas[$val];
							//echo '<br>';
							if($datas[$val] != "creneau fermer" && $datas[$val] != "creneau ouvert")
							{
								if( $_SESSION['nom'] != "admin")
							    {	
									if($ts < $ts_du_jour)
									{
										echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
									}
									else
									{
										echo '<td  bgcolor ="#E84A35" align="center"><a href="supprimer.php?formule=courte&mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&client='.$datas[$val].'" title="Suppression">' .$datas[$val]. '</td>';
									}
								}
							    //else 
							    //{
							    //  echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
							   // }
							
							    if($_SESSION['nom'] == "admin")
							    {	
									echo '<td  bgcolor ="#E84A35" align="center"><a href="supprimer.php?formule=courte&mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&client='.$datas[$val].'" title="Suppression">' .$datas[$val]. '</td>';
							    }	
								
							
							}
						}
						
						if($datas[$val] == '')
						{	
							echo '<td bgcolor ="#84E673" align="center"></td>';
						}
						if($datas[$val] == "unselect")
						{ 
							echo '	<td  bgcolor ="#E6BA39" align="center">Inactif</td>';
						}
						if($datas[$val] == "creneau fermer")
						{   echo '<td  bgcolor ="#E6FF39" align="center">Supplement fermé</td>';
						}
						if($datas[$val] == "creneau ouvert")
						{  	echo '<td bgcolor ="#84E673" align="center"></td>';
						}
							
/*						else
						{   
					
							if($datas[$val] == "unselect")
							{ 	if($_SESSION['nom'] == "admin")
								{
									echo '<td  bgcolor ="#E6BA39" align="center"><a href="ouvrir.php?mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'" title="Ouverture d\'un nouveau créneau">Inactif</td>';
							    }   
								else
								{
									 echo '	<td  bgcolor ="#E6BA39" align="center">Inactif</td>';
								}	
							}
							else
							{
								
						       if($datas[$val] == $utilisateur || $_SESSION['nom'] == "admin")
							   {	
									if($ts < $ts_du_jour)
									{
										echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
									}
									else
									{
										echo '<td  bgcolor ="#E84A35" align="center"><a href="supprimer.php?mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&client='.$datas[$val].'" title="Suppression">' .$datas[$val]. '</td>';
									}
								}
							    else 
							    {
							      echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
							    }
								
							}			
						}*/
					}// for	
						

////////// bloquage si le jour est derriére nous ...	

					
						if($ts >= $ts_du_jour && $_SESSION['nom'] != "admin" )				
						{		
							echo '
									<td align="center" width="1%">
									<a href="reservation_courte.php?formule=courte&mois='.$mois.'&jour='.$rendezvous.'">
									<img src="calendar.png" border="0" alt="Réserver" title="Réservation"></a>
									</td>';
						}
						if($ts < $ts_du_jour && $_SESSION['nom'] != "admin")	
						{ 
							echo '<td align="center" width="1%">
								  <img src="delete.png" border="0" alt="impossible" title="impossible">
								  </td>';
						}	
					 
						if($_SESSION['nom'] == "admin")
						{
							echo '
									<td align="center" width="1%">
									<a href="reservation_courte.php?formule=courte&mois='.$mois.'&jour='.$rendezvous.'">
									<img src="calendar.png" border="0" alt="Réserver" title="Réservation"></a>
									</td>';
						}
			
			}	// while			

		echo '	<tr bgcolor="#f1f3f5">
				<td colspan="11" class="center">&nbsp;</td>
				</tr>
				</tbody>
				</table>
				</div>  ';
	
	}
	else
	{
		//		echo "Ce mois n'est pas configuré.";
		//echo "Ce mois ne comporte pas de scéances ayant une durée de 20 min !.";
		$flag_setting_courte = "none";		

	}
	
	// double condition pour les creneaux de 20 et 30 min
		if(	$flag_setting_longue == "none"	&& $flag_setting_courte == "none"	)
		{
			echo "Ce mois n'est pas configuré.";
		}
	
	
	/************************************************/
	
	$tab_retour = compteur_reservation($mois);
	$cpt = count($tab_retour);
	echo '<br>';
	echo '<br>';
	echo '<br>';
	echo '<br>';
	if($cpt == 0)
	{
		echo "Actuellement, aucun créneau n'a été réservé.";
	}	
	if($cpt == 1)
	{
		echo "Actuellement, 1 créneau est réservé.";
	}
	if($cpt > 1)
	{
		echo "Actuellement, " .$cpt . " créneaux ont été réservés";
	}
	

?>
</body>
</html>