<?php
include "./form.php";
include "./connector.php";

?>

<center>
<div style="min-width:400px; max-width:800px; text-align:left;">
<div class="art-postmetadataheader">
 <h2 class="art-postheader"><span class="art-postheadericon">Generate Portal Pins</span></h2>
                                    </div>
<em>Type the number of pins to generate!</em>	
<br/>								
<?php
$result_form  = "
<table>
		<form action='' method='POST'>
	<tr><td>	Number of Pins:</td><td> <input type='number' name='pin_no' id='pin_no' required /></td></tr>
	<tr><td></td><td>	<input type='submit' name='create_pin' id='create_pin' value='GENERATE'></td></tr>
		</form>
	</table>		
		";
		
	$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(); \">		";	

function random_pin(){
	$alpha_string = "Q,W,E,R,T,Y,U,I,O,P,A,S,D,F,G,H,J,K,L,Z,X,C,V,B,N,M";
	$alpha_array = explode(",", $alpha_string);
	$pin  = $alpha_array[rand(0,count($alpha_array)-1)] . $alpha_array[rand(0,count($alpha_array)-1)] . rand(100000,999999);
	return $pin;
}

function lead_zero($num){
$num_padded = sprintf("%07d", $num);
return $num_padded; // returns 07
}
	
if(isset($_REQUEST['create_pin'])){
		if($_REQUEST['pin_no'] > 0){
			//REQUEST TO GLOBAL
				$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]);
					}
				}
			//END REQUEST TO GLOBAL
			
			$generated_pin = array();
			
			for($i = 1; $i <= $pin_no; $i++){
					//echo $i . "--" . random_pin(). "<br />";
					$current_pin = random_pin();
					$sql = "SELECT * FROM `pins` WHERE pin = '$current_pin';";
					
				if($query = $form2->run_query($sql)){
					if($form2->num_rows($query) == 0){
						$insert_sql = "INSERT INTO `pins` (`pin`, `admission_no`, `usage`, `created`) VALUES ('$current_pin', '', '', now());";
						$form2->run_query($insert_sql);
						$select_id_sql = $sql = "SELECT * FROM `pins` WHERE pin = '$current_pin';";
						$result = $form2->run_query($sql);
						$row = $form2->fetch_assoc($result);
						$pin_id = $row['id'];
						$pin_id = lead_zero($pin_id);
						$generated_pin["$pin_id"] = $current_pin;
						
					}
					else{
						echo "found";			
					}
				}	

			}
			
			if(!empty($generated_pin)){
				//	print_r($generated_pin);
				
				echo "<div id='result_div' style='background:white; box-shadow: 3px 3px 2px black; border:grey 1px solid; padding-bottom:40px;'>";
				
				foreach($generated_pin as $serial => $pin){
				
				$server = $_SERVER['SERVER_NAME'];
				echo "
				<center>
				<div style='border:1px grey dotted; width:300px;text-align:left;'>
				<strong>Result Checking instruction: </strong> <br />
				1. Visit $server/portal. <br />
				2. Click Result Checking button. <br />
				2. Enter the details of Student or pupil. <br />
				3. Enter the card pin. <br />
				<table>
				<tr><td style='text-align:right;'><strong>Serial Number:</strong></td><td>$serial</td></tr>
				<tr><td style='text-align:right;'><strong>Pin:</strong></td><td>$pin</td></tr>
				
				</table>
				</div>
				<br />
				<br />
				</center>
				";
				
				}
				
				echo  "</div>";
				
				echo "<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(); \">		";	

			}
				
			
		}
		else{
			echo "<span style='background:red; color:white;'>Invalid number of pins!</span>";
			echo $result_form;
		}
	}else{
		
		echo $result_form;
	}


?>

</div>
</center>
