<?php
include("../session.php");
include("../connect.php");

$user=$_SESSION['agent'];
$words=$_POST["words"];
$by=$_POST["searchBy"];

if($words==""){
	$words=$_GET["words"];
}
if($by==""){
	$by=$_GET["searchBy"];
}
//echo "admin success!!!";

if($by=="id"){
	$data = "select * from company where id like '%$words%' and agentID='$user'";
}else if($by=="cName"){
	$data = "select * from company where cName like '%$words%' and agentID='$user'";
}

$queryCount=mysql_query($data);
$num = mysql_num_rows($queryCount);				
?>
<html>
<head>
<link href="header.css" rel="stylesheet" type="text/css"/>
<link type="text/css" rel="stylesheet" href="overflow.css" />
<link rel="stylesheet" type="text/css" href="facebox/facebox.css" />
<script language="javascript" src="jquery.js"></script>
<script language="javascript" src="facebox/facebox.js"></script>
<script language="javascript">
$(document).ready(function() {
    $('a[rel*=facebox]').facebox();
})
</script>
 
<style type="text/css"> 
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
p.flip2,div.panel2,p.flip3,div.panel3
{
margin:0px;
padding:5px;
text-align:left;
background:#FFFFFF;
border:solid 1px #c3c3c3;
}
div.panel
{
height:70px;
display:none;
}
div.panel2,div.panel3
{
height:70px;
}
</style>
<script type="text/javascript">
function gotoFun(user){
	window.location = "viewCompany.php?user="+user;
}

</script>
</head>
<body>
<div id="content2">
<!-----------------Outer table--------------->
<table width=1000 border=0 cellspacing=10 cellpadding=3 >
<tr>
	<td colspan=2><?php include("head.php");?></td>
	</tr>
<tr>
	<td colspan=2>
	<div class=main>&nbsp;&nbsp;>>
			<a href="<?php echo"agent.php";?>">&nbsp;Home&nbsp;</a>>>
			&nbsp;Listing Company&nbsp;
			</div></td>
	</tr>
<tr>
	<td valign=top colspan=2>
	<!--------------------Inner table 2----------------------->
	<form action="<?php echo"searchResult.php?user=$user";?>" method="POST" >
	<table align=center cellpadding=5 cellspacing=3>
	<tr>
		<td valign=top>
		<b>Search By: </b>
		<select name="searchBy">
		<option value="id">Company ID</option>
		<option value="cName">Company Name</option>
		</select>
		</td>
		<td><input type="text" name="words" /><input type="hidden" name="user" value="<?php echo $user;?>"></td>
		<td><input type="submit" value="Search"> <input type="button" value=" All " onclick="gotoFun(user.value)"></td>
		<td><i>Total result : <?php echo $num;?></i></td>
		</tr>
	</table>
	</form>
	<table class=corners cellspacing=1 cellpadding=10 width=100% align=right>
	<tr>
		<td>
		<?php
//set pages per items
	$per_page = 10;
	
	$query1=mysql_query($data);

	$total=ceil(mysql_num_rows($query1)/$per_page);

	$page =(isset($_GET['page'])) ? (int) $_GET['page'] :1;
	$start=($page-1)*$per_page;
	

//display pages
echo "<table align=center width='80%'>";
echo "<tr class=page><td width='10%'>Pages >></td><td>";
	while($i<=$total){
		echo "<a href='searchResult.php?page=$i&user=$user&words=$words&searchBy=$by' align=center>$i</a>&nbsp ";
		$i++;
	}
echo "</td></tr>";
echo "</table>";
echo "<br/>";
?>	
<table align=center width=90% cellspacing=5 cellpadding=8>
	<tr class=menu>
		<th>ID</th>
		<th>Company Name</th>
		<th>Class</th>
		<th>Join Date</th>
		<th>Expire Date</th>
		<th colspan=4>Action</th>
		</tr>
<?php
//load company info	
$query=mysql_query($data." order by id asc limit $start,$per_page");
if($num==0){
	echo "<tr><td colspan=10 align=center>No Result</td></tr>";
}
	while($display=mysql_fetch_array($query)){
		
	$idid=$display['id'];
	$name=$display['cName'];
	$class=$display['class'];
	$address=$display['address'];
	$telNo=$display['telNo'];
	$email=$display['email'];
	$cPerson=$display['cPerson'];
	$rDate=$display['registerDate'];
	$eDate=$display['expireDate'];
	
	date_default_timezone_set ("Singapore"); 
	$ldate=date("d/m/Y", time());
	
	echo "<tr class=menu2>";
	echo "<td align=center>".$idid."</td>";
	echo "<td align=center>".$name."</td>";
	echo "<td align=center>".$class."</td>";
	echo "<td align=center>".$rDate."</td>";
	echo "<td align=center>".$eDate."</td>";
	echo "<td align=center width=250>";
	echo "<table>
			<tr><td width=50 align=center>
			<a href='editInfo.php?user=$user&id=$idid'>
			<img src='../images/edit.png' width=30 height=30 border=none/><br/><address>Edit</address></a>
			</td>";
	echo "<td align=center width=50><a href='viewProduct.php?user=$user&id=$idid'>
			<img src='../images/product.png' width=30 height=30 border=none/><br/><address>Product</address></a></td>";
	echo "<td align=center width=50><a href='updateLisence.php?user=$user&id=$idid'>
			<img src='../images/update.png' width=30 height=30 border=none/><br/><address>Update</address></a></td>";
	echo "<td align=center width=50>
			<a href='active.php?user=$user&id=$idid' rel='facebox'>
			<img src='../images/active.gif' width=30 height=30 border=none /><br/>
			<address>active</address></a></td>
			</tr></table>";
	echo "</td></tr>";
	}
	
?>	
	</table>
		</td>
		</tr>
	</table>
	<!----------------------End inner table 2-------------------->
	
	</td>
	</tr>
</table>
<!-----------------------End outer table-------------------------->
</div>



</body>
</html>