<?php
require_once __DIR__ . '/include/db_config.php';
 
$prefix = "iitech_";
if(isset($_POST['database'])){
	$DB_DATABASE = $_POST['database'];
}


$filiere = "";
$annee = "";
$academique = "";
$semestre = "";
$niveau = "";
$objet = "";

if(isset($_POST['filiere'])){
	$filiere = $_POST['filiere'];
}
if(isset($_POST['annee'])){
    $annee = $_POST['annee'];
}

if(isset($_POST['academique'])){
    $academique = $_POST['academique'];
}

if(isset($_POST['semestre'])){
    $semestre = $_POST['semestre'];
}

if(isset($_POST['niveau'])){
    $niveau = $_POST['niveau'];
}

if(isset($_POST['objet'])){
    $objet = $_POST['objet'];
}

try {
    $connection = new PDO("mysql:host=localhost;dbname=$DB_DATABASE", DB_USER, DB_PASSWORD);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e)  {
    die("OOPs something went wrong");
}

if(isset($_POST['filiere'])) {
	//$sql = "SELECT * from reclamation where idFiliere=? and annee=? and annee_acad=? and idsemestre=? and niveauFormation =? order by intituleUE asc";
	$sql = "SELECT * from reclamation where idFiliere= :fil and annee= :an and annee_acad= :acad and idsemestre= :id and niveauFormation = :niv and objet= :ob order by intituleUE asc";
    $statement = $connection->prepare($sql);
	$statement->bindParam(':fil', $filiere, PDO::PARAM_STR);
	$statement->bindParam(':an',  $annee, PDO::PARAM_STR);
	$statement->bindParam(':acad', $academique, PDO::PARAM_STR);
	$statement->bindParam(':id', $semestre, PDO::PARAM_STR);
	$statement->bindParam(':niv', $niveau, PDO::PARAM_STR);
	$statement->bindParam(':ob', $objet, PDO::PARAM_STR);
    //$statement->execute(array($filiere, $annee, $academique, $semestre, $niveau));
	$statement->execute();
	
    if($statement->rowCount()) { 
		while($row=$statement->fetch(PDO::FETCH_ASSOC))  {
			$row_all[]=array_map("utf8_encode", $row);
		}
	    header('Content-type: application/json');
   	    echo json_encode($row_all);
    }elseif(!$statement->rowCount()) {
	    echo "no rows";
    }
}
		  
?>