<?php

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

	include('database.php');
	DB_connexion(); 

	$utilisateur = $_SESSION['nom'] .' ' .$_SESSION['prenom'];
	header( 'content-type: text/html; charset=utf-8' );
?>

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

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

th {text-align: left;}

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

<?php

	$mois = $_GET['mois'];
	$annee = date('Y');
	
	echo ucfirst($mois) ." " .$annee;
	echo '<br><br>';
	
	//echo "Nous sommes le " .date("d-m-Y");
	$ts_du_jour = strtotime(date("d-m-Y"));
	//echo '<br><br>';
	
	//$table = "reservation" .$annee;
	$table = $_SESSION['table_reservation'] = "reservation" .$_SESSION['year'] ."_v3";

//////////////////////////////////////////////////////////////////////////	
	
	$ts_du_jour = strtotime(date("d-m-Y"));

	$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and mois ='$mois' and mode = 'actif' ";	
	//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 "Nombre de semaines " .$n;
	echo '<br><br>';
	
	$ts_du_jour = strtotime(date("d-m-Y"));
	
	if($n != 0)
	{
			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%">Semaine</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>
					</tr>';	
	
			while ($datas = mysqli_fetch_assoc ($result))
			{
		     	$id = $datas['id'];
		     	$rendezvous = $datas['rendezvous'];
				$mode = $datas['mode'];
		     	$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'];
				
				$avant = $datas['avant'];
				$apres = $datas['apres'];
			
				echo '
						<tr>
						<td align="center" nowrap="nowrap">'.$semaine.'</td>
						<td align="center" nowrap="nowrap">'.$rendezvous.'</td>';		
					
					for( $i=1; $i <= 8 ; $i++)		
					{    
						${'text'.$i} = "seance".$i;
						$val = ${'text'.$i};
/*						
						if($val == "seance1" || $val == "seance2" || $val == "seance7" || $val == "seance8" )
						{
							
							if(strlen($datas[$val]) == 0)
							{	
								echo '<td  bgcolor ="#84E673" align="center"><a href="ouvrir.php?mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&traitement=fermer " title="Supprimer le créneau">Actif</td>';
							}
							if(strlen($datas[$val]) != 0)
							{	
								echo '<td  bgcolor ="#E6BA39" align="center"><a href="ouvrir.php?mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val  .'&traitement=ouvrir " title="Ouverture d\'un nouveau créneau">Inactif</td>';
							}
							
						}	
						
						else
						{
*/						
						$n = 0;			
						
						$test = strpos($datas[$val], " ");
						if($test != 0)
						{ 	//echo $datas[$val];
							//echo '<br>';
							if($datas[$val] != "creneau fermer" && $datas[$val] != "creneau ouvert")
							{
							      echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
							}
						}

						if($datas[$val] == "")
						{ 
							echo '<td bgcolor ="#84E673" align="center">Libre</td>';
						}
						if($datas[$val] == "unselect")
						{ 
							$mode = "ouvrir";
								echo '	<td  bgcolor ="#E6BA39" align="center">Inactif</td>';
								//echo '	<td  bgcolor ="#E6BA39" align="center"><a href="extension.php?mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&mode=' .$mode  .'" title="Extension">Inactif</td>';
						}
						
						if($ts >= $ts_du_jour)	
						{	
							if($datas[$val] == "creneau fermer")
							{   $mode = "ouvrir";
								echo '<td  bgcolor ="#E6FF39" align="center"><a href="ouvrir_ou_fermer_creneaux.php?mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&mode=' .$mode  .'" title=Ouverture d\'un nouveau créneau">Supplement fermé</td>';
							}
							if($datas[$val] == "creneau ouvert")
							{   $mode = "fermer";
								echo '<td  bgcolor ="#84E673" align="center"><a href="ouvrir_ou_fermer_creneaux.php?mois='.$mois.'&jour='.$rendezvous.'&horaire=' .$val .'&mode=' .$mode  .'" title=Fermeture du créneau">Supplement ouvert</td>';
							}
						}
						else
						{	
							if($datas[$val] == "creneau fermer")
							{   $mode = "ouvrir";
								echo '<td  bgcolor ="#E6FF39" align="center">Supplement fermé</td>';
							}
							if($datas[$val] == "creneau ouvert")
							{   $mode = "fermer";
								echo '<td  bgcolor ="#84E673" align="center">Supplement ouvert</td>';
							}
						}
/*							
							if(strlen($datas[$val]) == 0)
							{	
								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">' .$datas[$val]. '</td>';
										}
									}
									else 
									{
										echo '<td  bgcolor ="#E84A35" align="center">' .$datas[$val]. '</td>';
									}
								}			
							}
/*						}
*/						
						
					}// for	
				
			
			}	// while			

		echo '	<tr bgcolor="#f1f3f5">
				<td colspan="10" class="center">&nbsp;</td>
				</tr>
				</tbody>
				</table>
				</div>  ';
	
	}
	else
	{	
		echo "Ce mois n'est pas configuré.";
	}
	
	
?>
</body>
</html>