<? include("../settings.php"); ?>
<? include("session.php"); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>SP Admin Panel</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Include all CSS/JS -->
        <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="css/jquery.dataTables.css" rel="stylesheet">
    <link href="http://cdn.datatables.net/responsive/1.0.1/css/dataTables.responsive.css" rel="stylesheet">
    

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.1.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="http://cdn.datatables.net/responsive/1.0.1/js/dataTables.responsive.js"></script>

    <!-- End of all CSS/JS -->
      <script>
          
      </script>
    
   </head>
<body>


<? include("nav_menu.php"); ?>
    <?
	
	function url_get_contents ($Url) {
		if (!function_exists('curl_init')){ 
			die('CURL is not installed!');
		}
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $Url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$output = curl_exec($ch);
		curl_close($ch);
		return $output;
	}
	
	
		if (isset($_REQUEST['registrant_sn'])){
			$timebind = array(":search1"=>date("Y-m-d H:i:s"));

			$sessions = $db->select("tbl_session","start < :search1 AND end > :search1", $timebind);
			$session = $sessions[0]['session_id'];
			// Searching for valid session
			if ($session > 0){
				$bind = array(":search1"=>$_REQUEST['registrant_sn']);
				$results = $db->select("tbl_registrant","registrant_sn=:search1",$bind);        
				
				// Searching if correct delegate number
				if (count($results)>0){
					$result = $results[0];
					$registrant_id = $result['registrant_id'];

					$bind = array(":search1"=>$registrant_id,":search2"=>$session);
					$results = $db->select("tbl_registrant_attendance","registrant_id=:search1 AND session_id=:search2",$bind);
					
					if (count($results)>0){
						echo "Delegate has already registered.<br/>Please redirect to Front Desk.";
					}else{
						
						$insert=array("registrant_id"=>$registrant_id,"session_id"=>$session,"user_id"=>$_SESSION['user_id']);
						$db->insert("tbl_registrant_attendance", $insert);

						// Sending to Print DB
						$bind = array (":search1"=>$_REQUEST['registrant_sn']);
						$results = $db->select("tbl_registrant","registrant_sn=:search1",$bind);
						
						$deresult =$results[0];
						$fullname = $deresult['registrant_fullname'];

						$bindrole = array(":search1"=>$deresult['role_id']);
						$roles = $db->select("tbl_role","role_id=:search1",$bindrole);
						$role = $roles[0]['role_name'];        
						
						//echo "<script>alert('Delegate attendance taken.');parent.thickBoxDone();</script>";        
						$message = "Role : ".$role."<br/>Registrant Type : ".$deresult['registrant_type']."<br/>Delegate Number : ".$deresult['registrant_sn']."<br/>Salutation : ".$deresult['registrant_salutation']."<br/>Name : ".$deresult['registrant_fullname']."<br/>Organisation : ".$deresult['registrant_organisation']."<br/>Payment Status : ".$deresult['registrant_paid']."<br/>Payment Status : ".$deresult['registrant_paid']."<br/>Badge : ".$deresult['registrant_badge']."<br/>Remarks : ".$deresult['remarks']."<br/><br/>";  
						
						if ($deresult['registrant_salutation']=='Dr' || $deresult['registrant_salutation']=='Prof' || $deresult['registrant_salutation']=='A/Prof')
							$fullname = $deresult['registrant_salutation']." ".$deresult['registrant_fullname'];
						else
							$fullname = $deresult['registrant_fullname'];
						$var = "user_name=sp&user_password=spspsp&name=".urlencode($fullname)."&sn=".urlencode($deresult['registrant_sn'])."&company=".urlencode($deresult['registrant_organisation'])."&extra1=".urlencode($deresult['remarks'])."&extra2=".urlencode($deresult['role_name'])."&printer=".urlencode($_SESSION['user_printer'])."&event=SINCEWCCM-SCAN";
              
						//echo $var;

						$contents = url_get_contents('https://www.events-sp.com/PRINT/insert.php?'.$var);
						//echo "<script>alert('Delegate attendance taken.');parent.thickBoxDone();</script>";        
						echo $message;
					}
				}else{
					print_r($_REQUEST);
					echo "Invalid Delegate Number.<br/>Please redirect to Front Desk.";
				}           
			}else{
				echo "Invalid Event Session.<br/>Please redirect to Front Desk.";
			}
		}
		
		
		
    ?>
      <div class="well">
<div class="content" bgcolor="#FFFFD0">
<div class="col-12 feature">
<div class="panelCheck">
    <div class="panel-heading">
        <h3 class "panel-title"><strong>SP Admin Panel - Scan Delegate</strong></h3>
    </div>

    
    <div class="well well-lg">
        <form class="form-horizontal" method="post" action="" id="scanner" name="scanner">
	
	<div class='form-group'>
		<label for='registrant_sn' class='col-lg-4 control-label'>Scan</label>
			<div class='col-lg-6 text-left'>
				<input type="text" class="form-control" name="registrant_sn" id="registrant_sn">
			</div>
	</div> 
	  <center>
      <button type="submit" name="submit" class="btn btn-info">Submit</button>
      <button type="reset" class="btn btn-default">Cancel</button>
    </center>
            
    </form>        
        
        <script>
			$( "#registrant_sn" ).focus();
			$("input").keypress(function(event) {
				if (event.which == 13) {
					$("#scanner").submit();
				}
			});
		</script>
    </div>
    
<!-- /.modal -->
</div>
</div>
</div>
</div>
</body>
</html>