<!doctype html>
<html lang=fr>

	<?php									
		session_start(); // On récupére les données de la seession
		if (!isset($_SESSION['Autorisation']))
		{
			header("Location: index.php"); // Si l'acteur de la session n'est pas un étudiant on retourne à l'accueil'
			exit();
		}
	?>

	<head>
		<title>Supprimer une composante</title>
		<meta charset=utf-8> <meta name=viewport content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="nogstyle.css">

		<!--
		<style type="text/css">
			p { text-align:center; }
			form { text-align:center;}
			input.fl-left { float:left;}
			#submit-yes { float:left; margin-left:220px;}
			#submit-no { float:left; margin-left:20px; }
		</style>
	-->
	</head>

	<body>
		<article>
			<h2>Supprimer une composante</h2>
			<?php 
				// On verifie la validité du num de la composante à supprimer
				if ( (isset($_GET['comp'])) && (is_numeric($_GET['comp']))) 
				{
					$comp = $_GET['comp'];
				}
				elseif (isset($_POST['comp']) && (is_numeric($_POST['comp'])))
				{ // Form submission.
						$comp = $_POST['comp'];
				}
				else
				{ // If no valid ID, stop the script.
					echo '<p class="error">Composante non valide.</p>';
					//exit();
				}
				
				require ('connect/mysqli_connect.php');
				
				// Check if the form has been submitted:
				if ($_SERVER['REQUEST_METHOD'] == 'POST')
				{
					if ($_POST['sure'] == 'Oui')   // Delete the record.
					{
						$q = 'DELETE FROM correspondre WHERE numComp='.$comp.' AND NumSP='.$_SESSION['spmodifiee'].';';
						$result = @mysqli_query ($dbcon, $q)or exit('Erreur SQL ! '.$q. '<br />'.mysql_error($dbcon));				
						if (mysqli_affected_rows($dbcon ) == 1)
						{ 
							$numsp=$_SESSION['spmodifiee'];
							header("Location: sp_modif.php?id=$numsp");;			
						} 
						else 
						{ // If the query did not run OK.
							echo '<p class="error">L\'enrerigistrment ne peut être détruit.<br> Il n\'existe pas </p>'; // Public message.
							echo '<p>' . mysqli_error($dbcon ) . '<br />Query: ' . $q . '</p>'; // Debugging message.
						}
					}
					else 
					{		 // No confirmation of deletion.
						echo '<h3>PAs de suppression</h3>';	
						if($_SESSION['Autorisation'] == 1)
						{
							header('location: prof_sp_voir.php?');
						}
						else
						{
							header('location: sp_modif.php?');
						}
					}
				}
				else 
				{ 
				////////////////////////////////////////////////////////////////
				// Show the form.
				///////////////////////////////////////////////////////////////
				
					$sp=intval($_SESSION['spmodifiee']);
					$q = "SELECT numComp FROM correspondre WHERE numComp=$comp AND NumSP=$sp;";
					$result = @mysqli_query ($dbcon , $q);
					if (mysqli_num_rows($result) == 1) 
					{ 
						$row = mysqli_fetch_array ($result, MYSQLI_NUM);
						// Display the record being deleted:
						
				
						// Create the form:
						echo '<form class="centre60" action="sp_compo_supprimer.php" method="post">';
						echo '<p style="text-align: center; font-weight: bold"> Voulez vous supprimer la composante '.$row[0].'?';
						echo '<br/> <br/>	<input id="submit-yes" type="submit" name="sure" value="Oui"> 
							<input id="submit-no" type="submit" name="sure" value="Non">	
							<input type="hidden" name="comp" value="'. $comp .'"</p>
						</form>';
					} 
					else 
					{ // Not a valid user ID.
						echo '<p class="error">This page has been accessed in error.</p>';
						echo '<p>&nbsp;</p>';
					}
				} // End of the main submission conditional.
				mysqli_close($dbcon );
				// echo '<p>&nbsp;</p>';
			?>
		</article>	
	</body>
</html>