<?php

// Connection à la BDD:
try {
  $conn = new PDO("mysql:host=localhost;dbname=savrillo", savrillo, "BHTvo0JfS");
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}


if($_GET["salle"] != NULL) {
	
	$idSalle = $_GET["salle"];
	
	$aliaz = array();
	
	$stmt = $conn->prepare("SELECT nom FROM salleAlias WHERE salleID=?");
	$stmt->execute([$idSalle]);
	$resA = $stmt->fetchAll();
	
	for($i=0;$i<count($resA);$i++)
		$aliaz[$i] = $resA[$i]['nom'];
	
	$locataires = array();
	
	$stmt = $conn->prepare("SELECT personne FROM locataire WHERE salle=?");
	$stmt->execute([$idSalle]);
	$resP = $stmt->fetchAll();
	
	for($i=0;$i<count($resP);$i++)
		$locataires[$i] = $resP[$i]['nom'];
	
	
	$stmt = $conn->prepare("SELECT id, nom, description FROM salles WHERE id=?");
	$stmt->execute([$idSalle]);
	$resS = $stmt->fetchAll();
	
	
	if(count($resS)==0){
		echo "404";
	}else {
		$out = array("id" => $resS[0]["id"], "nom" => $resS[0]["nom"], "description" => $resS[0]["description"], "aliaz" => $aliaz, "locataires" => $locataires);
		echo (json_encode($out));
	}
	
}else {
	echo "Je ne connais pas cette commande ...";
}
