<?php
session_start();

if (!isset($_SESSION['Autorisation']) or ($_SESSION['Autorisation'] != 2))  //admin
{  
	header("Location: erreur_page.php");
  	exit();
}

?>

<!doctype html>
<html lang=fr>
	<head>
		<title>CIMS Etablissement</title>
		<meta charset=utf-8> 
		<meta name=viewport content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="nogstyle.css">	
	</head>

	<body>
		<header>
			<?php 
				include('includes/header_simple.inc');
				echo'<h3>'.$_SESSION['PrenomAct'].' '.$_SESSION['NomAct'].' '.$_SESSION['NomEtab'].'</h3>';
				include('includes/menu_admin.inc');
			 ?>
		</header>
		
		<article>
		<!--SELECTION DE TOUS LES ETABLISSEMENTS-->
			<?php
		 		require ('connect/mysqli_connect.php'); // Connect to the db.
				$q = "SELECT nomEtab FROM etablissement ORDER BY nomEtab;";	
				$result = mysqli_query ($dbcon, $q); 
			?>
			
			<form class="centre80" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
				<p>
					<label class="label1" for="Nom">Etablissement</label>
					<select name="Etab" >
						<option value="-1"> Tous
						<?php
				 			while ($row = mysqli_fetch_array($result, MYSQLI_BOTH))
							{
								echo '<option value="' .$row[0]. '">';
								echo  $row[0].'</option>';
							}
				 		?>
					</select>

						<td><input id="submit" type="submit" name="submit" value="Sélection">
				</p>				
			</form>	
					
			<?php 
				if ($_SERVER['REQUEST_METHOD'] == 'POST')
				{
					$Etab = $_POST['Etab'];
					
					if($Etab==-1)
					{
						$restriction='';
					}
					else
					{
						$restriction= 'WHERE nomEtab="' .$Etab.'"';
					}
					// This script retrieves records from the users table.
					$q = 'SELECT * FROM etablissement '.$restriction.'ORDER BY nomEtab;';	
					$result = mysqli_query ($dbcon, $q); 
					if ($result)
					{		   // If it ran, display the records.
						$nbrelignes = mysqli_num_rows($result);
						// Table header.
						echo '<table>
						<tr>
						<th><b>Code</b></th>
						<th><b>Nom</b></th>
						<th><b>NomcontactEtab</b></th>
						<th><b>mailContactetab</b></th>						
						<th><b>Adresse</b></th>
						<th><b>CP</b></th>
						<th><b>VilleEtab</b></th>
						<th><b>Dateenr</b></th>
						<th><b>Modif.</b></th>
						</tr>';
						
						// Fetch and display the records:
						while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
						{
							echo '<tr>
							<td>' . $row['codeEtab'] . '</td>
							<td>' . $row['nomEtab'] . '</td>
							<td>' . $row['nomcontactEtab'] . '</td>
							<td>' . $row['mailContactetab'] . '</td>
							<td>' . $row['adresseEtab'] . '</td>
							<td>' . $row['cpEtab'] . '</td>
							<td>' . $row['VilleEtab']. '</td>
							<td>' . $row['DateEnr']. '</td>
							<td><a class="bouton" href="admin_cm_etab.php?id='. $row['codeEtab'] . '"><span class="iconic">p</span></a></td>
							';
						}
						echo '</table>'; // Close the table.
						echo '<p class="centre40"><b>'.$nbrelignes .' établissement(s) enregitré(s) </b></p>';
						mysqli_free_result ($result); // Free up the resources.
					}
					 else 
					 { // If it did not run OK.// Public message:
					
						echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
						// Debugging message:
						echo '<p>' . mysqli_error($dbcon) . '<br><br>Query: ' . $q . '</p>';
					}
					mysqli_close($dbcon); // Close the database connection.
				}
			?>	
			<p style="text-align: center">
				<br/>
				<a class="bouton10"  href="admin_cm_etab.php?id=ajout">Ajouter un établissement</a>
				<br/>
			</p>		
		</article>	
	</body>
</html>



