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

date_default_timezone_set('Africa/Porto-Novo');
$filiere = "";
$annee = "";
$academique = "";
$semestre = "";
$niveau = "";
$matricule = "";
$date = "";
$codeue = "";
$decision = "";
$commentaire = "";

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['matricule'])){
    $matricule = $_POST['matricule'];
}

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

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

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

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

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'])) {
	$datetraite = date('Y-m-d H:i:s', time());
	$sql = "UPDATE reclamation SET commentaire = :com, decision = :dec, date_traitement = :dt where idFiliere= :fil and annee= :an and annee_acad= :acad and idsemestre= :id and niveauFormation = :niv and 
	matricule = :mat and date_save = :date and code_ue = :code";
    $statement = $connection->prepare($sql);
	$statement->bindParam(':com',  $commentaire, PDO::PARAM_STR);
	$statement->bindParam(':dec',  $decision, PDO::PARAM_STR);
	$statement->bindParam(':dt',   $datetraite, PDO::PARAM_STR);
	$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(':mat',  $matricule, PDO::PARAM_STR);
	$statement->bindParam(':date', $date, PDO::PARAM_STR);
	$statement->bindParam(':code', $codeue, PDO::PARAM_STR);
	$statement->execute();
	$nb = $statement->rowCount();
	if($nb > 0) {
		$json['success'] = 1;
	}else {
		$json['success'] = 0;
	}
	echo json_encode($json);
	
    /*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";
    }*/
}
		  
?>