<?php
//require_once __DIR__ . '/include/db_config.php';

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

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

// connecting to db
$db = new DB_CONNECT($DB_DATABASE);

/*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['searchQuery'])) {
	$search_query=utf8_decode($_POST['searchQuery']);
	$sql = 'SELECT * from reclamation where MATCH(matricule, nomprenom, objet, intituleUE, annee_acad) AGAINST(CONVERT(:search_query USING utf8) IN BOOLEAN MODE)';
    $statement = $db->prepare($sql);
	$statement->bindParam(':search_query', $search_query, PDO::PARAM_STR);
    $statement->execute();
    if($statement->rowCount()) {
	    //$row_all = $statement->fetchall(PDO::FETCH_ASSOC);
		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";
    }
}
		  
?>