<!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/prof on retourne à l'accueil'
			exit();
		}
	?>

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

	<body>
		<article>
			<h2>Supprimer une activité</h2>
			<?php 
				function retour($id)
				{
					echo'<a class="bouton10" href="'.$_SESSION['page'].'">OK</a></h3>';
				}					
				
				
				
				
				
				// On verifie la validité du num de lactivité à supprimer
				if ( (isset($_GET['activite'])) && (is_numeric($_GET['activite']))) 
				{
					$activite = $_GET['activite'];
				}
				elseif (isset($_POST['activite']) && (is_numeric($_POST['activite'])))
				{ 
						$activite = $_POST['activite'];
				}
				else
				{ // If no valid ID, stop the script.
					echo '<p class="error">activiteo non valide.</p>';
					header("Location: index.php");
					exit();
				}
				
				require ('connect/mysqli_connect.php');
			
				if ($_SERVER['REQUEST_METHOD'] == 'POST')
				{
					if ($_POST['sure'] == 'Oui')   // Delete the record.
					{
						$q = 'DELETE FROM correspondre WHERE numActivite='.$activite.' AND NumSP='.$_SESSION['spmodifiee'].';';
						$result = @mysqli_query ($dbcon, $q)or exit('Erreur SQL ! '.$q. '<br />'.mysql_error($dbcon));				

						
						if (mysqli_affected_rows($dbcon ) == 1)
						{ 
							echo'<h3>Activité '.$activite.' supprimée';
							retour($_SESSION['spmodifiee']);	
						}
						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 //NON
					{		 // No confirmation of deletion.
						echo'<h3> Pas de suppression';
						retour($_SESSION['spmodifiee']);
					}

				}
				else 
				{ 
				////////////////////////////////////////////////////////////////
				// Show the form.
				///////////////////////////////////////////////////////////////
				
					$sp=intval($_SESSION['spmodifiee']);
					$q = "SELECT numactivite FROM correspondre WHERE numactivite=$activite 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_act_suppr.php" method="post">';
						echo '<p style="text-align: center; font-weight: bold"> Voulez vous supprimer l\'activité '.$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="activite" value="'. $activite .'"</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>