<?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,"user_id"=>$_REQUEST['user_printer']);
                    $db->insert("tbl_registrant_attendance", $insert);

                    // Sending to Print DB
                    $bind = array (":search1"=>$_REQUEST['sn']);
					$results = $db->select("tbl_registrant","registrant_sn=:search1",$bind);
					
					$deresult =$results[0];
					
					if ($deresult['registrant_dinner_attend']==1)
						$extra1 = "Dinner";
					else
						$extra1 = "";
        
					if ($deresult['registrant_salutation']=="Prof" || $deresult['registrant_salutation']=="A/Prof" || $deresult['registrant_salutation']=="Dr")
						$fullname = $deresult['registrant_salutation']." ".$deresult['registrant_fullname'];        
					else
						$fullname = $deresult['registrant_fullname'];

					//print_r($deresult);
					if ($deresult['registrant_cocktail_attend']==1)
						$cocktail = "Yes";
					else
						$cocktail = "No";
					if ($deresult['registrant_dinner_attend']==1)
						$dinner = "Yes";
					else
						$dinner = "No";
					//echo "<script>alert('Delegate attendance taken.');parent.thickBoxDone();</script>";        
					$message = "Delegate No : ".$deresult['registrant_sn']."<br/>Registrant Type : ".$deresult['registrant_type']."<br/>Paper Number : ".$deresult['registrant_papernumber']."<br/>Full Name : ".$fullname."<br/>Company : ".$deresult['registrant_organisation']."<br/>Cocktail Attendance : ".$cocktail."<br/>Extra Cocktail  : ".$deresult['registrant_extra_cocktail']."<br/>Dinner Attendance : ".$dinner."<br/>Extra Dinner  : ".$deresult['registrant_extra_dinner']."<br/>Diet : ".$deresult['registrant_diet']."<br/>Badge Colour : ".$deresult['badgecolour']."<br/>Remarks. : $extra1";
			
                    //$fullname = $deresult['registrant_salutation']." ".$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($extra1)."&extra2=N.A.&printer=".urlencode($_REQUEST['user_printer'])."&event=ISCOC2016";
       
                    //echo $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.";
    }

?>