<?include_once("../settings.php");?>
<?
    if (isset($_REQUEST['sn']) && $_REQUEST['printer']!=""){
        
        $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['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);
                    $db->insert("tbl_registrant_attendance", $insert);

                    // Sending to Print DB
                    $bind = array (":search1"=>$registrant_id);
		        $results = $db->select("tbl_registrant","registrant_id=:search1",$bind);
		        $deresult =$results[0];
		        
		        $tbind = array(":search1"=>$deresult['table_id']);
		        $tresults = $db->select("tbl_table","tbl_id=:search1",$tbind);
		        $tresult = $tresults[0];
		        
		        if ($tresult['tbl_name']!="")
		            $extra1 = "Table ".$tresult['tbl_name'];
		        else
		            $extra1 = "";
		        $extra2 = "";
        
		        if ($deresult['registrant_salutation']=="Prof" || $deresult['registrant_salutation']=="A/Prof" || $deresult['registrant_salutation']=="HE")
				$fullname = $deresult['registrant_salutation']." ".$deresult['registrant_fullname'];        
		        else
		        	$fullname = $deresult['registrant_fullname'];

		        //print_r($deresult);
		       $message = "Registration Type : ".$role."<br/>Delegate Number : ".$deresult['registrant_sn']."<br/>Name : ".$deresult['registrant_fullname']."<br/>Organisation : ".$deresult['registrant_organisation']."<br/>ID No. : ".$deresult['registrant_idnumber']."<br/>Seating : ".$deresult['registrant_seating']."<br/>Reg Point : ".$deresult['registrant_point']."<br/>Car Plate No. : ".$deresult['registrant_carplate']."<br/>Badge Colourt : ".$deresult['registrant_badge']."<br/>Remarks : ".$deresult['remarks']."<br/><br/>";  
						
		
                    //$fullname = $deresult['registrant_salutation']." ".$deresult['registrant_fullname'];
        	 $var = "user_name=sp&user_password=spspsp&name=".urlencode($fullname)."&sn=".urlencode($deresult['registrant_seatingplan'])."&company=".urlencode($deresult['registrant_company'])."&extra1=".urlencode($extra1)."&extra2=N.A.&printer=".urlencode($_REQUEST['printer'])."&event=PSTA2016";
       
                    //echo $var;

                    //$contents = file_get_contents('https://www.events-sp.com/PRINT/insert.php?'.$var);
                    //$contents = file_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.";
        }
        
       
    }else{
        echo "Please logout from your app and login again.";
    }

?>