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

// include db connect class
require_once __DIR__ . '/include/new_db_connect.php';

// connecting to db
$bdd = connect($DB_DATABASE);

require_once 'include/fonctions_utiles.php';

$matricule = "";
$nomprenom = "";
$niveau = "";
$annee = "";
$idSemestre = "";
$code_ue = "";
$intitule_ue = "";

$objet = "";
$message = "";

$confirm  = "";

if(isset($_POST['matricule'])){
	$matricule = $_POST['matricule'];
}
if(isset($_POST['username'])){
	$nomprenom = $_POST['username'];
}
if(isset($_POST['niveauFormation'])){
    $niveau = $_POST['niveauFormation'];
}
if(isset($_POST['annee'])){
    $annee = $_POST['annee'];
}
if(isset($_POST['idSemestre'])){
    $idSemestre = $_POST['idSemestre'];
}
if(isset($_POST['code_ue'])){
    $code_ue = $_POST['code_ue'];
}

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

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

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

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

$code     = explode("#", $code_ue);
$intitule = explode("#", $intitule_ue);
$messageinfo = explode("#", $message);
$size = count($code);

$query = "SELECT annee_acad, idFiliere FROM  tabinscription WHERE matricule = :matricule AND annee = :annee AND niveauFormation = :niveau";
$statement = $bdd->prepare($query);
$statement->bindParam(':matricule', $matricule, PDO::PARAM_STR);
$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
$statement->bindParam(':niveau', $niveau, PDO::PARAM_STR);
$statement->execute();
$tab=$statement->fetch(PDO::FETCH_ASSOC);

$annee_acad = $tab['annee_acad'];	 
$idFiliere 	= $tab['idFiliere'];	

date_default_timezone_set('Africa/Porto-Novo');

$date = date('Y-m-d H:i:s', time());

if(empty($confirm)) {
	if($size > 0) {
		for($i =0; $i < $size - 1; $i++){
			$codeue = $code[$i];
			$intituleue = $intitule[$i];
			$messageinf = $messageinfo[$i];
			//$intituleue = mysql_real_escape_string($intituleue);
			$query = "INSERT INTO `reclamation` (`matricule`, `nomprenom`, `annee`, `annee_acad`, `idsemestre`, `niveauFormation`, `idFiliere`, `code_ue`, `intituleUE`, `objet`, `sujet`, `date_save`) 
			VALUES (:matricule, :nomprenom, :annee, :acad, :idSemestre, :niveau, :idfiliere, :codeue, :intituleue, :objet, :messageinf, :date)";
			$statement = $bdd->prepare($query);
			$statement->bindParam(':matricule', $matricule, PDO::PARAM_STR);
			$statement->bindParam(':nomprenom', $nomprenom, PDO::PARAM_STR);
			$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
			$statement->bindParam(':acad', $annee_acad, PDO::PARAM_STR);
			$statement->bindParam(':idSemestre', $idSemestre, PDO::PARAM_STR);
			$statement->bindParam(':niveau', $niveau, PDO::PARAM_STR);
			$statement->bindParam(':idfiliere', $idFiliere, PDO::PARAM_STR);
			$statement->bindParam(':codeue', $codeue, PDO::PARAM_STR);
			$statement->bindParam(':intituleue', $intituleue, PDO::PARAM_STR);
			$statement->bindParam(':objet', $objet, PDO::PARAM_STR);
			$statement->bindParam(':messageinf', $messageinf, PDO::PARAM_STR);
			$statement->bindParam(':date', $date, PDO::PARAM_STR);
			$result = $statement->execute();

			if($result){
				$json['success'] = 1;									
			}else{
				$json['success'] = 0;
			}
			echo json_encode($json);
		}	
	}else{
		$json['success'] = 0;
		echo json_encode($json);
	}
}else{
	$query = "update reclamation set confirm = :confirm  where matricule = :matricule and annee = :annee and 
	annee_acad = :acad and idSemestre = :sem and idFiliere = :id and niveauFormation = :niveau ";

	$statement = $bdd->prepare($query);
	$statement->bindParam(':confirm', $confirm, PDO::PARAM_STR);
	$statement->bindParam(':matricule', $matricule, PDO::PARAM_STR);
	$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
	$statement->bindParam(':acad', $annee_acad, PDO::PARAM_STR);
	$statement->bindParam(':sem', $idSemestre, PDO::PARAM_STR);
	$statement->bindParam(':id', $idFiliere, PDO::PARAM_STR);
	$statement->bindParam(':niveau', $niveau, PDO::PARAM_STR);
	$update = $statement->execute();
	
	if($update){
		$json['success'] = 1;
	}else{
		$json['success'] = 0;
	}
	echo json_encode($json);
}
?>
