<?include_once("../../settings.php");?>
<form method='post' name='attendance'>
<?
	if (isset($_REQUEST['action']) && $_REQUEST['action']=="take"){
		// Registering attendance
		$SQL = "INSERT INTO tbl_registrant_attendance (registrant_id) VALUES ('".$_REQUEST['registrant_id']."');";
		$qry->queryExecute($SQL);
		// Checking if this person has been replaced
		echo "<script>alert('Delegate attendance taken.');parent.thickBoxDone();</script>";
	}
	if (isset($_REQUEST['action']) && $_REQUEST['action']=="assign"){
		// Registering attendance
		$SQL = "UPDATE tbl_registrant SET tbl_id='".$_REQUEST['table_id']."' WHERE registrant_id='".$_REQUEST['registrant_id']."';";
		$qry->queryExecute($SQL);
		echo "<script>alert('Table assigned. Please take attendance.');</script>";
	}
	if (isset($_REQUEST['action']) && $_REQUEST['action']=="delete"){
		$SQL = "DELETE FROM tbl_registrant_attendance WHERE registrant_id='".$_REQUEST['registrant_id']."';";
		$qry->queryExecute($SQL);
		echo "<script>alert('Delegate attendance has been unregistered.');parent.thickBoxDone();</script>";
	}

	$SQL = "SELECT * FROM tbl_registrant WHERE registrant_id='".$_REQUEST['registrant_id']."';";
	$deresult = $qry->querySelectSingle($SQL);
	echo "<center><font face='Arial' size='-1'>";
	echo "<table style='font-family:verdana;font-size:12px;width:450px;background:#CCCCFF;border:1px solid #000099;padding:3px;'>";
	echo "<tr><td width='30%'><b>Full Name: </b></td><td>".$deresult['registrant_surname']." ".$deresult['registrant_givenname']."</td></tr>";
	echo "<tr><td><b>Company Name: </b></td><td>".$deresult['registrant_organisation']."</td></tr>";
	$SQL = "SELECT tbl_name FROM tbl_table WHERE tbl_id = '".$deresult['tbl_id']."';";
	$tablename = $qry->querySelectSingle($SQL);
	echo "<tr><td><b>Table Number: </b></td><td>".$tablename['tbl_name']."</td></tr>";
	echo "<tr><td><b>Seat Number: </b></td><td>".$deresult['seat_number']."</td></tr>";
	echo "<tr><td><b>Diet: </b></td><td>".$deresult['registrant_diet']."</td></tr>";
	echo "<tr><td><b>Diet Restriction: </b></td><td>".$deresult['registrant_dietremarks']."</td></tr>";
	$replaced = $deresult['registrant_replaced'];
	echo "</table>";
	echo "<input type='hidden' name='registrant_id' value='".$_REQUEST['registrant_id']."'>";
	if ($replaced){
		echo "<br>This person has already been replaced.<br>";
	}else{
		echo "<br><br>";
		if ($deresult['tbl_id']==0){
			$result = gettable();
			if ($result == ""){
				echo "All your tables are full. Please assign manually.";
			}else{
				$SQL = "SELECT * FROM tbl_table WHERE tbl_id='".$result."';";
				//echo $SQL;
				$tresult = $qry->querySelectSingle($SQL);
				echo "Table Allocation : <a href='delegate_attendance_take.php?registrant_id=".$_REQUEST['registrant_id']."&table_id=$result&action=assign'>Assign Table ".$tresult['tbl_name']."</a>";
			}
		}else{
			$SQL = "SELECT COUNT(*) as number FROM tbl_registrant_attendance WHERE registrant_id='".$_REQUEST['registrant_id']."';";
			$cresult = $qry->querySelectSingle($SQL);
			if ($cresult['number']==0){
				echo "<br><a href='delegate_attendance_take.php?registrant_id=".$_REQUEST['registrant_id']."&action=take'>Take Attendance</a>";
			}else{
				echo "<br><a href='delegate_attendance_take.php?registrant_id=".$_REQUEST['registrant_id']."&action=delete'>Delete Attendance</a>";
			}
		}
	}
	echo "</font>";
	echo "</center>";



	function gettable(){
		global $qry;
		$tablenumber = "";
		$SQL = "SELECT * FROM tbl_table WHERE tbl_lock='0' AND tbl_active='1' ORDER BY tbl_order ASC;";
		$results=$qry->queryMultiSelect($SQL);
		foreach ($results as $result){
			$SQL = "SELECT count(*) as number FROM tbl_registrant WHERE tbl_id='".$result['tbl_id']."';";
			$countresult = $qry->querySelectSingle($SQL);
			if ($countresult['number']<$result['tbl_max']){
				$tablenumber = $result['tbl_id'];
				break;
			}
		}
		return $tablenumber;
	}



?>
</form>