<?php
//session
session_start();
include ("connect.php");
$username = $_SESSION['staff'];

$searchBy = $_GET["searchBy"];
$keyword = $_GET["keyword"];

if($_GET["sort"]!=""){
$sort = $_GET["sort"];
}else{
$sort = num;
}

if($_SESSION['staff']!= null)
{
?>

<html>
<head>
<title>Daily Bookstore - Management System</title>
<!--include css file-->
<link href="bookstaff.css" rel="stylesheet" type="text/css"/>
</head>

<body>

<?php
include ("stafffunctionbar.php");
?>
<center>
<h2>Daily Bookstore Management - Manage Event</h2>
</center>

<div id="mainpage">
<table width="90%">
<tr>
<td width="15%" valign="top">
<fieldset style="margin:0 5 0 0">
<legend><img src="Picture/event.gif" width="30" height="30">Manage Event</legend>
<table>
<tr>
<td valign="top">
<a href="event.php"><img src="Picture/list.gif" width="30" height="30" alt="event list"><a href="event.php">Event List</a><br/>
<br/>
<a href="addEvent.php"><img src="Picture/add.gif" width="30" height="30" alt="add event"><a href="addEvent.php">Add Event</a><br/>
<br/>
</td>
</tr>
</table>
</fieldset>
</td>

<td width="75%">

<table border="0" width="90%">

<tr>
<td>
<?php
include("connect.php");

$result = mysql_query("SELECT * FROM companyevent ORDER BY eventDate DESC");

$num_results = mysql_num_rows($result);

$records_per_page = 5;
if(isset($_GET["Pages"])){
	$pages = intval($_GET["Pages"]);
	}else{
	$pages = 1;
}

$total_records = mysql_num_rows($result);
$total_pages;
//determine how many page need
if($total_records){
   if($total_records<$records_per_page){
   $total_pages = 1;
   }
   else if($total_records%$records_per_page){
   $total_pages = (int)($total_records/$records_per_page) + 1;
   }else{
   $total_pages = $total_records/$records_per_page;                     
   }
}
else{
   $total_pages = 0;
}

$offset = ($pages - 1)*$records_per_page;
@mysql_data_seek($result, $offset);

echo "<p>Number of Event(s) : ".$num_results."</p>";

for ($i=0; $i <$num_results; $i++)
{
	$row = mysql_fetch_array($result);
	if($row["eventTitle"]!=""){
	echo "<table border='0'>";
	
	echo "<tr>";
	echo "<td align='left' style='font-size:15pt;color:red'><b>".$row["eventTitle"]."</b></td>";
	echo "<td rowspan='5' align='center' valign='middle' width='20%'>";
	echo "<img src=/bookstore/bookstaff/".$row["eventImage"]." width='200' height='200'>";
	echo "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<td><b>Date: </b>".$row["eventDate"]."</td>";
	echo "</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td><b>Time: </b>".$row["eventTime"]."</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<td><b>Location: </b>".$row["eventLocation"]."</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td><b>Info: </b><br/>".$row["eventDescription"]."</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td><a href='editevent.php?eventTitle=".$row["eventTitle"]."'>Edit</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='deleteevent.php?eventTitle=".$row["eventTitle"]."'>Delete</a></td>";
	echo "</tr>";
	
	echo "<hr/>";
	}
	echo "</table>";
	}
	
echo "<hr/>";

//create page previous button
echo"<center>";
if($pages>1){
if(($keyword=="")||($searchBy=="")){
echo "<a href='event.php?&Pages=".($pages-1)."'>&nbsp;[Previous]&nbsp;</a>|";
}else{
echo "<a href='event.php?keyword=".$keyword."&searchBy=".$searchBy."&search=Search&Pages=".($pages-1)."'>&nbsp;[Previous]&nbsp;</a>|";
}
}
//create page number button
for($k=1; $k<=$total_pages; $k++){
	if($k!=$pages){
	if(($keyword=="")||($searchBy=="")){
	echo "<a href=\"event.php?&Pages=".$k."\">&nbsp;[".$k."]&nbsp;</a>";
	}else{
	echo "<a href=\"event.php?keyword=".$keyword."&searchBy=".$searchBy."&search=Search&Pages=".$k."\">&nbsp;[".$k."]&nbsp;</a>";
	}
	}else
	{
	echo "&nbsp;[$k]&nbsp;"."";
	}
}
//create page next button
if($pages<$total_pages){
if(($keyword=="")||($searchBy=="")){
echo "|<a href='event.php?&Pages=".($pages+1)."'>&nbsp;[Next]&nbsp;</a>";
}else{
echo "|<a href='event.php?keyword=".$keyword."&searchBy=".$searchBy."&search=Search&Pages=".($pages+1)."'>&nbsp;[Next]&nbsp;</a>";
}
}

$db = mysql_close();

}else{
	echo "<script language='javascript'>alert ('Please login.')</script>";
	echo "<script language='javascript'>window.location='index.php'</script>";
}
?>
<hr/>
</td>
</tr>
</table>
<center>
<div id="bottom">
<br/>
Copyright &#169; with Tian Tian Bookstore 2011. All Rights Reserved.
<br/>
Website:<a href="../bookstore" target="_blank">www.dailybook.com.my
</div>
</center>

</body>
</html>