<?php 

	include("connexion_db.php");

	$resultat = '';

	// Verification de la persistance des données
	if ($_SERVER['REQUEST_METHOD'] === 'POST') {
		$idPatient = $_POST['id_patient'];
		$reponsesFonction = $_POST['reponses'];

	

			$sqlveri = "SELECT date FROM reponse_all r where r.id_patient='".$idPatient."' ORDER by r.date ASC";
			$reqq = cleanPDOQuery($sqlveri);
			$dateSQQQLLL='';
			while($res = $reqq->fetch()){
				$dateSQQQLLL = $res['date'];
			}


			$now = new DateTime();
			$now = $now->format('Y-m-d');
			$resultat = $now;
			if($dateSQQQLLL === $now){
				for ($i=0; $i < count($reponsesFonction); $i++) {
					$sqla = "UPDATE reponse_all SET reponse= '".$reponsesFonction[$i][1]."' WHERE id_question='".$reponsesFonction[$i][0]."' and id_patient='".$idPatient."' and date = '".$now."'";
					$reqa = cleanPDOQuery($sqla);
					$resultat = $sqla;
				}


			}else{	
				for ($i=0; $i < count($reponsesFonction); $i++) {
					$sql = "INSERT INTO reponse_all(id_patient,id_question,reponse,date) VALUES('".$idPatient."','".$reponsesFonction[$i][0]."','".$reponsesFonction[$i][1]."',now())";
					$req = cleanPDOQuery($sql);
					$resultat = $sql;
				}
		}		

	}


	header('Content-Type: application/json');
 	echo json_encode($resultat);
		

 ?>