<?php
include("connect.php");
if($_GET["search"] == "Search")
{	
	$searchBy = $_GET["searchBy"];
	$keyword = $_GET["keyword"];
	if(($keyword=="")||($searchBy=="")){
	echo "<script language='javascript'>alert('Please insert your keyword and search category.')</script>";
	echo "<script language='javascript'>window.location='rinv.php'</script>"; 
	}else{
	$result = mysql_query("SELECT * FROM receivedinvoice WHERE ".$searchBy." LIKE '%".$keyword."%' ORDER BY rinvNo");
	
}
}
else if($_GET["reset"] == "Reset Result")
{
	$result = mysql_query("SELECT * FROM receivedinvoice ORDER BY rinvNo");
	
}
else
{
	$result = mysql_query("SELECT * FROM receivedinvoice ORDER BY rinvNo");
	
}

$num_results = mysql_num_rows($result);

$total_records = mysql_num_rows($result);
$records_per_page = 10;
$page   = intval($_GET['page']);

$adjacents  = intval($_GET['adjacents']);
if($adjacents<=0) $adjacents = 4;

if($page<=0)  $page  = 1;
//$tpages = ($_GET['tpages']) ? intval($_GET['tpages']) : 20; // 20 by default

if($total_records){
   if($total_records<$records_per_page){
   $tpages = 1;
   }
   else if($total_records%$records_per_page){
   $tpages = (int)($total_records/$records_per_page) + 1;
   }else{
   $tpages = $total_records/$records_per_page;                     
   }
}
else{
   $tpages = 0;
}

//if(isset($_GET["Pages"])){
//	$page = intval($_GET["Pages"]);
//	}else{
//	$page = 1;
//}

$offset = ($page - 1)*$records_per_page;
@mysql_data_seek($result, $offset);
echo "<p>Number of Bill List(s) : ".$num_results."</p>";

echo "<table border='1' cellspacing='0' cellpadding='5' bordercolor='white' width='100%'>";
echo "<tr bgcolor='black' style='color:white'>";
echo "<td>Bill No</td>";
echo "<td>Ref PO</td>";
echo "<td>Supplier Name</td>";
echo "<td>Created Date</td>";
echo "<td>Created By</td>";
echo "<td>View Order</td>";
echo "</tr>";

$j = 1;
if($j<$records_per_page);{
for($i=0; $i<$records_per_page; $i++)
{
	$row = mysql_fetch_array($result);
	if($row["rinvNo"]!=""){
	echo "<tr>";
	echo "<td>".$row["rinvNo"]."</td>";
	echo "<td>".$row["poNo"]."</td>";
	echo "<td>".$row["supName"]."</td>";
	echo "<td>".$row["rinvDate"]."</td>";
	echo "<td>".$row["staffID"]."</td>";
	echo "<td><a href='rinvview.php?rinvNo=".$row["rinvNo"]."'>View</a></td>";
	echo "</tr>";
	}
}
$j++;
}
echo "</table>";

echo "<hr/>";

//create page previous button
echo"<center>";
if(($keyword=="")||($searchBy=="")){
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
}else{
$reload = $_SERVER['PHP_SELF'] . "?keyword=" . $keyword . "&searchBy=" . $searchBy . "&search=Search&tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
}

function paginate_three($reload, $page, $tpages, $adjacents) {
	
	$prevlabel = "&lsaquo; Prev";
	$nextlabel = "Next &rsaquo;";
	
	$out = "<div class=\"pagin\">\n";
	
	// previous
	if($page==1) {
		$out.= "<span>" . $prevlabel . "</span>\n";
	}
	elseif($page==2) {
		$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
	}
	else {
		$out.= "<a href=\"" . $reload . "&amp;page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
	}
	
	// first
	if($page>($adjacents+1)) {
		$out.= "<a href=\"" . $reload . "\">1</a>\n";
	}
	
	// interval
	if($page>($adjacents+2)) {
		$out.= "...\n";
	}
	
	// pages
	$pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
	$pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
	for($i=$pmin; $i<=$pmax; $i++) {
		if($i==$page) {
			$out.= "<span class=\"current\">" . $i . "</span>\n";
		}
		elseif($i==1) {
			$out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
		}
		else {
			$out.= "<a href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n";
		}
	}
	
	// interval
	if($page<($tpages-$adjacents-1)) {
		$out.= "...\n";
	}
	
	// last
	if($page<($tpages-$adjacents)) {
		$out.= "<a href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n";
	}
	
	// next
	if($page<$tpages) {
		$out.= "<a href=\"" . $reload . "&amp;page=" . ($page+1) . "\">" . $nextlabel . "</a>\n";
	}
	else {
		$out.= "<span>" . $nextlabel . "</span>\n";
	}
	
	$out.= "</div>";
	
	return $out;
}
echo paginate_three($reload, $page, $tpages, $adjacents);

echo "</center>";

$db = mysql_close();

?>