<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
include "./form.php";
include "./connector.php";

	function lead_zero($num){
	$num_padded = sprintf("%07d", $num);
	return $num_padded; // returns 07
	}



	if(isset($_REQUEST['view_result'])){
		$type = $_REQUEST;
		$a=$type;
		if(is_array($a)){
		$value_name = array_keys($a);
		$posts = $value_name;
			foreach ($posts as $value) {///
				$name = $value;	
				$$value = mysqli_real_escape_string($form2->con(), $type[$name]);
				$value = mysqli_real_escape_string($form2->con(), $type[$name]);
			}
		}

	//////////
	$print_button = "<input type='button' class='btn btn-primary btn-sm print_button_class_a' value='PRINT' onclick=\"if(document.getElementById('result_div')){}else{this.style.display = 'none';window.print();} content = document.getElementById('result_div').innerHTML; x=window.open(); x.document.open;  x.document.write(content + '<script>window.print();</script>'); x.document.close(); \">		";	

		$sql = "SELECT * FROM `online_result` WHERE CONVERT(`admission_no` USING utf8mb4) = '$adm_no' AND CONVERT(`session` USING utf8mb4) = '$session' AND CONVERT(`term` USING utf8mb4) = '$term' ORDER BY id DESC LIMIT 1;";

		if($query = $form2->run_query($sql)){
			if($form2->num_rows($query) > 0){
					while($row = $form2->fetch_assoc($query)){
							$type = $row;
							$a=$type;
							if(is_array($a)){
							$value_name = array_keys($a);
							$posts = $value_name;
								foreach ($posts as $value) {///
									$name = $value;	
									$$value = $type[$name];
									$value = $type[$name];
								}
							}
							$r_id = $id;
							$student_result_head = "
							<div>
							<center>
							$print_button
							<div id='result_div' style='background:white; box-shadow: 3px 3px 2px black; border:grey 1px solid; padding-bottom:40px;'>";
							
							$student_result = "$result_data";

							$student_result_foot = "</div>
							
							
							$print_button			
							</center>
							
							<div>
							";		
					}
					
					
						$sql2 = "SELECT * FROM `pins` WHERE pin = '$pin_key';";
						
						if($query2 = $form2->run_query($sql2)){
							if($form2->num_rows($query2) > 0){
								$row = $form2->fetch_assoc($query2);
								$pin_id = $row['id'];
								$admission_no = $row['admission_no'];
								$result_id = $row['result_id'];
								$usage = $row['usage'];
								$max_usage = 20;
	//							$pin_id = lead_zero($pin_id);
								if($admission_no == "" || $admission_no == $adm_no){
									if($result_id == "" || $result_id == $r_id){
										if($max_usage - 1 >= $usage){
											$new_usage = $usage + 1;
											
											echo "<div>Checked $new_usage time(s) out $max_usage allowed time(s)</div>";
											
											echo $student_result_head;
											echo base64_decode($student_result);
											echo $student_result_foot;
											
											$update_sql = "UPDATE `pins` SET 
															`admission_no` = '$adm_no',
															`result_id` = '$r_id',
															`usage` = '$new_usage',
															`last_used` = now()
															WHERE `id` = '$pin_id';";
											$form2->run_query($update_sql);
										}else{
											echo "<span style='color:red;'>Maximum Pin usage exceeded!</span>";
											//echo $result_form;
										}
									}else{
										echo "<span style='color:red;'>Pin is already used by you for another result!</span>";
										//echo $result_form;
									}							
								}
								else {
									echo "<span style='color:red;'>Pin is already used by another person!</span>";
									//echo $result_form;
								}
							}
							else{
								echo "<span style='color:red;'>Invalid pin!</span>";
								//echo $result_form;
							}
						} else{

						}
					
					
					
					
			}
			else{
				echo "<span style='color:red;'>No result found!</span>";
				//echo $result_form;
			}
			
			
			
		}

		}else{
			
		//	//echo $result_form;
		}


?>

</div>
</center>
