<!DOCTYPE html>
<html>
	<head>
		<title>Gestion personnel</title>
		<?php $titre = 'Gestion personnel'; ?>
		<meta charset="UTF-8">
		<?php include 'session.inc.php'; check_login(); ?>
		<link rel="stylesheet" href="style/style.css" />
	</head>

	<body>
    	<?php include("menu.php"); ?>
    	<div  id='contenu'>
    		<?php
    	
				error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
				Include('../database.params.inc.php');
				$link = pg_connect("host=$machine user=$user password=$pwd dbname=$db")or die("Erreur de Connection !<br />".pg_last_error() ) ;


				switch ( $_POST['action'] ) {
					case "add" :
						$login = htmlentities(trim($_POST['login']), ENT_QUOTES);
						$mdp = htmlentities(trim($_POST['mdp']), ENT_QUOTES);
						$fct = htmlentities(trim($_POST['fct']), ENT_QUOTES);

						$mdp_hash = password_hash($mdp, PASSWORD_DEFAULT);
				
						if ( $login!="" && $mdp!="" && $fct!=""){
							$query = "INSERT INTO PERSONNEL(login, pass_hash, fonction) VALUES('$login','$mdp_hash','$fct')";
							$result = pg_query($link, $query) or die("Erreur SQL !<br />$query<br />".pg_last_error());
						}
						else{
							header('Location: ges_perso.php');
							exit();
						}
					break;

					case "mod" :
						$login1 = htmlentities(trim($_POST['login1']), ENT_QUOTES);
						$mdp1 = htmlentities(trim($_POST['mdp1']), ENT_QUOTES);
						$fct1 = htmlentities(trim($_POST['fct1']), ENT_QUOTES);

						$mdp_hash1 = password_hash($mdp1, PASSWORD_DEFAULT);

						$id1 = $_POST['id1'];

						if ( $login1!=""){
							$query = "UPDATE PERSONNEL SET login='$login1' WHERE id_personnel = '$id1'";
							$result = pg_query($link, $query) or die("Erreur SQL !<br />$query<br />".pg_last_error()) ;
						}
						else if ( $mdp_hash1!=""){
							$query = "UPDATE PERSONNEL SET pass_hash='$mdp_hash1' WHERE id_personnel = '$id1'";
							$result = pg_query($link, $query) or die("Erreur SQL !<br />$query<br />".pg_last_error()) ;
						}
						else if ( $fct1!=""){
							$query = "UPDATE PERSONNEL SET fonction='$fct1' WHERE id_personnel = '$id1'";
							$result = pg_query($link, $query) or die("Erreur SQL !<br />$query<br />".pg_last_error()) ;
						}
						else{
							header('Location: ges_perso.php');
							exit();	
						}
					break;

					case "sup" :
						$id2 = $_POST['id2'];
						$query = "DELETE FROM PERSONNEL WHERE id_personnel = '$id2' ;";
						$result = pg_query($link, $query) or die("Erreur SQL !<br />$query<br />".pg_last_error()) ;
					break;
					
					default :
						header('Location: ges_perso.php');
						exit();
				}

				header('Location: ges_perso.php');
				exit();
			?>
		</div>
	</body>
</html>