<?php
include '../autoload.php';

$survey_id = -1;
if(isset($_GET['survey_id']))
	$survey_id = $_GET['survey_id'];

$preview = 0;
if(isset($_GET['preview']))
	$preview = $_GET['preview'];

$resp_id = -1;
if ( isset($_GET['resp_id']))
	$resp_id = $_GET['resp_id'];

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
	if ( array_key_exists('button1', $_POST))
	{
		error_log ( "Button : " .$_POST['button2'] );
		error_log("Yes!!!!");
	}
}

?>


<!DOCTYPE html>

<html>

	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<title>Enquête de satisfaction</title>
		<link rel="stylesheet" href="/eoQUALBootstrap/eoQUAL/SubFW/css/paper.css">

		<?php
			$libmanager = new eoLibrariesManagerQual ();
			$libmanager->init_page();
			$libmanager->render_library ();
		?>

		<script src="/ximlibweb/thirdparty/package/qrcodejs/qrcode.js"></script>

	</head>

	<body class="A4">
		
		<section class="sheet padding-10mm" style="height: auto !important;min-height: 297mm;">

		<form method="post" action="SurveyCompletingValidate.php">

		<?php
			$resp = array ();

			function GetValueFromFieldID ( $field_id )
			{
				global $resp;

				$ret = -1;
				for ( $i=0; $i<count($resp); $i++ )
				{
					if ( $resp[$i]["field_id"] == $field_id )
					{
						$ret = $resp [$i]["value"];
						break;
					}
				}

				return $ret;
			}


			function GetValueTextFromFieldID ( $field_id )
			{
				global $resp;

				$ret = '';
				for ( $i=0; $i<count($resp); $i++ )
				{
					if ( $resp[$i]["field_id"] == $field_id )
					{
						$ret = $resp [$i]["value_text"];
						break;
					}
				}

				return $ret;
			}

			$conn = get_dbconnection();

			// Responses
			if ( $resp_id != -1 )
			{
				global $resp;
				$sql = "select id, field_id, value, value_text from survey_resp_field where resp_id=".$resp_id;

	 			error_log ( "draw_preview 3 : " .$sql );

				$query = pg_query($conn,$sql);

				while ( $row = pg_fetch_row ($query) )
				{
					$id 				= $row [0];
					$field_id 			= $row [1];
					$value 				= $row [2];
					$value_text			= $row [3];

					$res = array (  "id" 				=> $row[0],
									"field_id" 			=> $row[1],
									"value" 			=> $row[2],
									"value_text" 		=> $row[3] );

					array_push ( $resp, $res );
				}
			}
	
			// Survey
			$sql = "select id, title, description from survey where id=".$survey_id;

	 		error_log ( "draw_preview 1 : " .$sql );

			$query = pg_query($conn,$sql);

			$arr = array ();
			if ( $row = pg_fetch_row ($query) )
			{
				$id 			= $row [0];
				$title 			= $row [1];
				$description 	= $row [2];

				echo '<div class="row w-100 m-0 p-0 border_bottom fw-bold border-bottom"><h2>'.$title.'</h2></div>';

				echo '<div class="row w-100 m-0 p-0 ps-3 pe-3 pb-3 pt-3 fw-bold">'.$description.'</div>';

				echo '<div class="row m-0 p-0 ps-3 pe-3 pb-3">';
				echo 	'<div class="col m-0 p-0">';
				echo 	'<i>Pour remplir ce questionnaire à l\'aide de votre smartphone flashez ce QRCode : </i>';
				echo 	'</div>';
				echo 	'<div id="qrcode" class="col-auto m-0 p-0 pe-3">';
				echo 	'</div>';
				echo '</div>';
			}

			
			// Fields
			$sql = "select id, content, type from survey_field where survey_id=".$survey_id." order by position asc";

	 		error_log ( "draw_preview 2 : " .$sql );

			$query = pg_query($conn,$sql);

			while ( $row = pg_fetch_row ($query) )
			{
				echo '<div class="row m-0 p-0 ps-3 pe-3">';

				$id 		= $row [0];
				$content 	= $row [1];
				$type 		= $row [2];

				if ( $resp_id != -1 )
				{
					if ( $type == 7 || $type == 8 )
					{
						$value 				= GetValueTextFromFieldID ( $id );
						$value_ai_rating 	= GetValueFromFieldID ( $id );
						$value_ai_rating	= ($value_ai_rating==-1)? "N/A" : $value_ai_rating;
					}
					else
						$value = GetValueFromFieldID ( $id );
				}

				$name = 'resp_' . $survey_id . '_' . $id . '_' . $type;

				switch ( $type )
				{
					case 1: 	// Header
						echo '<div class="bg-light w-100 m-0 p-0 ps-3 pt-2 pb-2 border rounded-top">'.$content.'</div>';
						break;

					case 2: 	// Yes-No
					{
						$opt0 = "";
						$opt1 = "";

						if ( $resp_id != -1 )
						{
							$opt0 = "disabled";
							$opt1 = "disabled";

							switch ( $value )
							{
								case 0:	$opt0 = "checked disabled";	break;
								case 1:	$opt1 = "checked disabled";	break;
							}
						}
						echo '<div class="d-flex flex-column p-2">';
						echo 		'<div class="row w-100">';
						echo 			'<div class="col flex-grow-1">'.$content.'</div>';
						echo			'<div class="col-auto m-0 p-0 pe-2 text-center"><div class="m-0 p-0 pe-2" style="min-width: 18px;"><input class="form-check-input" type="radio" name="'.$name.'" value="1" '.$opt1.'><label class="form-check-label ms-2" data-langkey="Yes">Oui</label></div></div>';
						echo			'<div class="col-auto m-0 p-0 pe-2 text-center"><div class="m-0 p-0" style="min-width: 18px;"><input class="form-check-input" type="radio" name="'.$name.'" value="0" '.$opt0.'><label class="form-check-label ms-1" data-langkey="No">Non</label></div></div>';
						echo 		'</div>';
						echo '</div>';
					}
					break;

					case 3: 	// 5 smileys
					{
						$opt1 = "";
						$opt2 = "";
						$opt3 = "";
						$opt4 = "";
						$opt5 = "";

						if ( $resp_id != -1 )
						{
							$opt1 = "disabled";
							$opt2 = "disabled";
							$opt3 = "disabled";
							$opt4 = "disabled";
							$opt5 = "disabled";
							switch ( $value )
							{
								case 1 :	$opt1 = "checked disabled";	break;
								case 2 :	$opt2 = "checked disabled";	break;
								case 3 :	$opt3 = "checked disabled";	break;
								case 4 :	$opt4 = "checked disabled";	break;
								case 5 :	$opt5 = "checked disabled";	break;
							}
						}
						echo '<div class="d-flex flex-column p-2">';
						echo  		'<div class="row w-100">';
						echo  			'<div class="col flex-grow-1">'.$content.'</div>';
						echo 			'<div class="col-auto m-0 p-0 pe-2 text-center"><div class="m-0 p-0" style="min-width: 18px;"><input class="form-check-input" type="radio" name="'.$name.'" value="1" '.$opt1.'></div></div>';
						echo 			'<div class="col-auto m-0 p-0 pe-2 text-center"><div class="m-0 p-0" style="min-width: 18px;"><input class="form-check-input" type="radio" name="'.$name.'" value="2" '.$opt2.'></div></div>';
						echo 			'<div class="col-auto m-0 p-0 pe-2 text-center"><div class="m-0 p-0" style="min-width: 18px;"><input class="form-check-input" type="radio" name="'.$name.'" value="3" '.$opt3.'></div></div>';
						echo 			'<div class="col-auto m-0 p-0 pe-2 text-center"><div class="m-0 p-0" style="min-width: 18px;"><input class="form-check-input" type="radio" name="'.$name.'" value="4" '.$opt4.'></div></div>';
						echo 			'<div class="col-auto m-0 p-0 pe-2 text-center"><div class="m-0 p-0" style="min-width: 18px;"><input class="form-check-input" type="radio" name="'.$name.'" value="5" '.$opt5.'></div></div>';
						echo  		'</div>';
						echo  '</div>';
					}
					break;

					case 4: 	// Header Yes-No
					{
						echo '<div class="d-flex flex-column p-2">';
						echo 		'<div class="row w-100">';
						echo 		'<div class="col flex-grow-1 fw-bold">'.$content.'</div>';
						echo		'<div class="col-auto m-0 p-0 pe-2 text-center"><div class="m-0 p-0 pe-2" style="min-width: 18px;"><input class="form-check-input" type="radio" name="'.$name.'" value="1"><label class="form-check-label ms-2" data-langkey="Yes">Oui</label></div></div>';
						echo		'<div class="col-auto m-0 p-0 pe-2 text-center"><div class="m-0 p-0" style="min-width: 18px;"><input class="form-check-input" type="radio" name="'.$name.'" value="0"><label class="form-check-label ms-1" data-langkey="No">Non</label></div></div>';
						echo	'</div>';
						echo '</div>';
					}
					break;

					case 5: 	// Header 5 smileys
					{
						echo '<div class="d-flex flex-column p-2">';
						echo	'<div class="row w-100">';
						echo		'<div class="col flex-grow-1 fw-bold">'.$content.'</div>';
						echo		'<div class="col-auto m-0 p-0 pe-2"><img src="/eoQUALBootstrap/eoQUAL/SubFW/bmp/smiley_1.png" style="height: 18px; width: auto;"></img></div>';
						echo		'<div class="col-auto m-0 p-0 pe-2"><img src="/eoQUALBootstrap/eoQUAL/SubFW/bmp/smiley_2.png" style="height: 18px; width: auto;"></img></div>';
						echo		'<div class="col-auto m-0 p-0 pe-2"><img src="/eoQUALBootstrap/eoQUAL/SubFW/bmp/smiley_3.png" style="height: 18px; width: auto;"></img></div>';
						echo		'<div class="col-auto m-0 p-0 pe-2"><img src="/eoQUALBootstrap/eoQUAL/SubFW/bmp/smiley_4.png" style="height: 18px; width: auto;"></img></div>';
						echo		'<div class="col-auto m-0 p-0 pe-2"><img src="/eoQUALBootstrap/eoQUAL/SubFW/bmp/smiley_5.png" style="height: 18px; width: auto;"></img></div>';
						echo 		'</div>';
						echo '</div>';
					}
					break;

					case 6: 	// Header
						echo '<div class="d-flex flex-column p-2">';
						echo 		'<div class="row w-100">';
						echo 			'<div class="col flex-grow-1 fw-bold">'.$content.'</div>';
						echo 		'</div>';
						echo '</div>';
						break;

					case 7: 	// String
					{
						$opt = "";

						if ( $resp_id != -1 )
							$opt = "disabled";
						echo '<div class="d-flex flex-column m-0 p-2">';
						echo 		'<div class="row w-100 m-0 p-0">';
						echo 			'<div class="col flex-grow-1 m-0 p-0">'.$content.'</div>';
						echo			'<div class="col-auto m-0 p-0 pe-2 text-center"><input class="form-control" type="text" name="'.$name.'" '.$opt.'></div>';
						echo 		'</div>';
						echo '</div>';
					}
					break;

					case 8: 	// Text
					{
						$opt = "";

						if ( $resp_id != -1 )
							$opt = "disabled";
						echo '<div class="d-flex flex-column m-0 p-2">';
						echo 		'<div class="row w-100 m-0 p-0">'.$content.'</div>';
						echo 		'<div class="row flex-grow-1 m-0 p-0">';
						echo			'<textarea class="form-control m-0 p-0" rows="4" name="'.$name.'" '.$opt.'>'.$value.'</textarea>';
						echo 		'</div>';
						if ( $resp_id != -1 )
						{
							echo 		'<div class="row w-100 m-0 p-0">';
							echo 			'<div class="col m-0p-0">';
							echo 			'Notation I.A. : ' .$value_ai_rating;
							echo 			'</div>';
							echo 		'</div>';
						}
						echo '</div>';
					}
					break;
				}
		
	    		echo '</div>';
			}


			if ( $preview == 0 )	
			{
				echo '<div class="row m-0 p-0 ps-2 pe-2 mt-4 pt-3 border-top">';
				echo 	'<div class="col m-0 p-0">Merci pour votre participation. Cliquer sur ce bouton pour nous envoyer vos réponses :</div>';
				echo 	'<div class="col-auto">';
				echo 		'<input type="submit" name="button1" class="ximeo_button" />';
				echo 	'</div>';
				echo '</div>';
			}
			
		?>

		</form>

		</section>

		<script type="text/javascript">
				let qrc = document.getElementById("qrcode");
				if ( qrc != null )
				{
					var qrCode = new QRCode(qrc, {
    					text: "https://www.ximeo.be",
    					width: 64,
    					height: 64,
    					colorDark : "#000000",
    					colorLight : "#ffffff",
    					correctLevel : QRCode.CorrectLevel.H
					});
				}
		</script>

	</body>

</html>