<?php
include "script.php";

if($user['perm'] < 2) {
	header('Location: deco.php');
}

if(isset($_GET['del']) AND !empty($_GET['del']))
{
	if($user['perm'] >= 2) {
		$del = (int) htmlspecialchars($_GET['del']);

		$req = $db->prepare('DELETE FROM admin WHERE id =?');
		$req->execute(array($del));
		header('Location: gestion_des_acces.php');
	}
}


if(isset($_POST['send'])) {
	if(isset($_POST['pseudo']) AND !empty($_POST['pseudo']) AND isset($_POST['mdp']) AND !empty($_POST['mdp']) AND isset($_POST['perm']) AND !empty($_POST['perm'])) {

		if($user['perm'] >= 2) {
			$pseudo = htmlspecialchars($_POST['pseudo']);
			$mdp = htmlspecialchars($_POST['mdp']);
			$perm = htmlspecialchars($_POST['perm']);

			switch ($perm) {
			case "v0":
				$perm_int = 0;
				break;
		    case "v1":
		    	$perm_int = 1;
		        break;
		    case "v2":
		    	$perm_int = 2;
		        break;
			}

			if(strlen($pseudo) <= 50) {
				$req = $db->prepare("SELECT id FROM admin WHERE pseudo = ?");
	            $req->execute(array($pseudo));
	            $pseudo_exist = $req->rowCount();
	            if ($pseudo_exist == 0) {
					$req = $db->prepare("INSERT INTO admin(pseudo, mdp, perm) VALUES(?, ?, ?)");
					$req->execute(array($pseudo, password_hash($mdp, PASSWORD_DEFAULT), $perm_int));
					header('refresh:0');
				} else {
					$error = "pseudo déja utilisé";
				}
			} else {
				$error = "le pseudo ne doit pas dépasser 50 char";
			}
		}
	} else {
		$error = "Tout les champs doivent être complétés";
	}
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Admin / Accès</title>
</head>
<body>
	<main>
		<form method="POST">
			<input type="text" placeholder="identifiant" name="pseudo" class="input_inline">
			<input type="password" placeholder="mot de passe" name="mdp" class="input_inline">
			<select name="perm" class="input_inline">
				<option value="v0">GDA - Perm 0</option>
				<option value="v1">Bureau - Perm 1</option>
				<option value="v2">Admin - Perm 2</option>
			</select>
			<input type="submit" name="send" value="Creer l'acces" class="submit_inline">
		</form>
		<?php
		if(isset($error)) {
			echo "<font color='red'>".$error."</font>";
		}
		?>
		<br><br>
		<table class="acces">
			<tr>
				<th width="10%">ID (db)</td>
				<th width="40%">Identifiant</td>
				<th width="40%">Perm</td>
				<th width="10%">Actions</td>
			</tr>
			<?php
			$req = $db->query('SELECT id, pseudo, perm FROM admin');
			while($admin = $req->fetch()) {
			?>
				<tr>
					<td><?= $admin['id'] ?></td>
					<td><?= $admin['pseudo'] ?></td>
					<td><?php
						switch ($admin['perm']) {
						case 0:
							echo "<font color='green'>GDA</font>";
							break;
					    case 1:
							echo "<font color='orange'>Bureau</font>";
					        break;
					    case 2:
					        echo "<font color='red'>Admin</font>";
					        break;
						}
					?></td>
					<td><a href="?del=<?= $admin['id'] ?>" class="cross">X</a></td>
				</tr>
			<?php } ?>
		</table>
	</main>
</body>
</html>