<?php

	require_once 'processes/config.php';
	require_once 'processes/dbh.inc.php';
	require_once 'processes/checkLogin.php';

	if(isset($_POST['id_questionnaire'])) {

		$id_questionnaire = mysqli_real_escape_string($connection, trim($_POST['id_questionnaire']));
		
		
		// Check that questionnaireID is not an empty string
		// And that it's registered in the database
		if($id_questionnaire === "") {
			http_response_code(400);
			echo json_encode(array("error" => "Please specify questionnaire ID."));
			exit();
		} else if(!mysqli_query($connection, "SELECT * FROM questionnair WHERE id = $id_questionnaire;")) {
			http_response_code(400);
			echo json_encode(array("error" => "Given questionnaire ID ($id_questionnaire) not in database."));
			exit();
		}


		$query = "SELECT * FROM `question` WHERE `id` IN (SELECT `id_question` FROM `questionnair_question` WHERE `id_questionnair`=".$id_questionnaire.");";
		$result = mysqli_query($connection, $query);
		
		print '<option disabled selected>Select Question</option>';
		
		while($row = mysqli_fetch_assoc($result)) {
			print '<option value="'.$row['id'].'">'.$row['premise'].'</option>';
		}
		
	}
	