<?php

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

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

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

</style>
</head>
<body>

<?php

	$mois = intval($_GET['mois']);
	$annee = date('Y');
	//$mois = "2";
	
	$tab_mois = array("janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre");
	$mois = $tab_mois[$mois];
	//echo $mois_actif;
	
	echo "Nous sommes le " .date("d-m-Y");
	$ts_du_jour = strtotime(date("d-m-Y"));
	echo '<br><br>';
	
	$table = "reservation" .$annee;
//	$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>';
	}
*/	
//////////////////////////////////////////////////////////////////////////	
	
	$ts_du_jour = strtotime(date("d-m-Y"));

	$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' 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);
		
	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>
					<th class="center" width="1%">Réservation</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'];
				
			
				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};
						$n = 0;			
						
						$test = strpos($datas[$val], " ");
						if($test != 0)
						{ 	//echo $datas[$val];
							//echo '<br>';
							if($datas[$val] != "creneau fermer" && $datas[$val] != "creneau ouvert")
							{
								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>';
							    }
							
							}
						}
						
						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)				
						{		
							echo '
									<td align="center" width="1%">
									<a href="reservation03.php?mois='.$mois.'&jour='.$rendezvous.'">
									<img src="calendar.png" alt="Réserver" title="Réservation"></a>
									</td>';
						}
						else
						{ 
							echo '<td align="center" width="1%">
								  <img src="delete.png" alt="impossible" title="impossible">
								  </td>';
						}	
					 
					 
				
			
			}	// while			

		echo '	<tr bgcolor="#f1f3f5">
				<td colspan="16" class="center">&nbsp;</td>
				</tr>
				</tbody>
				</table>
				</div>  ';
	
	}
	else
	{
		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>