<?php
include "script.php";

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

$req = $db->query("SELECT * FROM enigma WHERE id = 1");
$r = $req -> fetchAll(PDO::FETCH_ASSOC);

if(isset($_POST['send'])) {
	if($user['perm'] >= 3) {
		if(isset($_POST['answer']) AND !empty($_POST['answer'])) {
			$ans = htmlspecialchars($_POST['answer']);
			$req = $db->prepare("UPDATE enigma SET answer = ? WHERE id = 1");
			$req->execute(array($ans));
		}
		if(isset($_POST['points']) AND !empty($_POST['points'])) {
			$point = (int) htmlspecialchars($_POST['points']);
			$req = $db->prepare("UPDATE enigma SET point = ? WHERE id = 1");
			$req->execute(array($point));
		}
		if(isset($_POST['team']) AND !empty($_POST['team'])) {
			switch (htmlspecialchars($_POST['team'])) {
			case "t":
				$t_int = NULL;
				break;
			case "t0":
				$t_int = 0;
				break;
		    case "t1":
		    	$t_int = 1;
		        break;
			}
			$req = $db->prepare("UPDATE enigma SET team = ? WHERE id = 1");
			$req->execute(array($t_int));
		}
		header('Refresh:0');
	}
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Admin / Enigma</title>
</head>
<body>
    <main>
    	<form method="POST">
			<input type="texte" name="answer" placeholder="answer" class="input_inline">
			<input type="number" name ="points" placeholder="points" class="input_inline">
			<select name="team" class="input_inline">
				<option value="t">NULL</option>
				<option value="t0">Pkpeach</option>
				<option value="t1">Boomario</option>
			</select>
			<input type="submit" name="send" value="Mettre à jour" class="submit_inline">
		</form>
    	<table class="acces">
			<tr>
				<th width="10%">ID (db)</td>
				<th width="40%">Answer</td>
				<th width="20%">Points</td>
				<th width="10%">Team</td>
			</tr>
				<tr>
					<td><?= $r[0]['id'] ?></td>
					<td><?= $r[0]['answer'] ?></td>
					<td><?= $r[0]['point'] ?></td>
					<td><?php
					switch ($r[0]['team']) {
					case "0":
						echo "Pkpeach";
						break;
				    case "1":
				    	echo "Boomario";
				        break;
					}
					?></td>
				</tr>
		</table>
    </main>
</body>
</html>