<?php
include("../session.php");
include("../connect.php");
$user=$_GET["user"];
?>
<html>
<head>
<style>
	a#pag {color:black;font-family: Century Gothic, Segoe UI; text-decoration:none; font-size:13px}
</style>
</head>
<body>
<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>
<!------------------------------------------------------------------>
<div id="header"  style="background-color:#FF2626">
	<center>
		<h1 style="margin-bottom:0">Easy shop Management</h1>
	</center>
</div>
<!------------------------------------------------------------------>
<form name="searchForm" method="post">

<table border="0" cellspacing="5" cellpadding="5" align="right">
<tr>
	<td align="right">
		<input type="hidden" name="searchBy" value="promotion.pname"/>
		<input type="text" name="keywords">&nbsp;&nbsp;
		<input type="submit" name="search" value="Search Name" style = "background-color: #9191FF; border: 0px; color: #FBFDFB; width:100; height:23; font-family: Segoe UI">
		<input type="submit" name="showAll" value="ShowAll" style = "background-color: #9191FF; border: 0px; color: #FBFDFB; width:70; height:23; font-family: Segoe UI">
	</td>
</tr>
</form>
</table>
</br>
</br>
</br>

<!------------------------------------------------------------------>
<center>
<table border = "1" cellpadding = "5" cellspacing = "2" width = "90%" style = "border:solid;border-color:white">
	<tr>
		<td style="background-color:#76888f"><h2 align='center' style="color:white;font-size:1cm">Promotion System Control</h2></td>
	</tr>
</table>
</center>
<?php

if($_POST["search"] == "Search Name"){
	$searchBy = $_POST["searchBy"];
	$keywords = $_POST["keywords"];
	$result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE ".$searchBy." LIKE '%".$keywords."%' AND product.companyID='$user' AND product.status='available' ORDER BY promotion.pID");
}
else if($_POST["showAll"] == "ShowAll"){
	$limit = 5; //max displayed per page
    $start = $_GET['start']; //get start variable
    if(isset($_POST["showAll"]))
	{	$start = 0;}
 	else
 	{	if (!$start) {$start = 0;}}
	
	$sql = $result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE product.companyID='$user' AND product.status='available' ORDER BY promotion.pID");
	$record_count = mysql_num_rows($sql);
	$max_pages = $record_count / $limit;
	
	$result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE product.companyID='$user' AND product.status='available' ORDER BY promotion.pID LIMIT $start,$limit");
}
else{
	$limit = 5; //max displayed per page
    $start = $_GET['start']; //get start variable
    if(isset($_POST["showAll"]))
	{	$start = 0;}
 	else
 	{	if (!$start) {$start = 0;}}
	
	$sql = $result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE product.companyID='$user' AND product.status='available' ORDER BY promotion.pID");
	$record_count = mysql_num_rows($sql);
	$max_pages = $record_count / $limit;
	
	$result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE product.companyID='$user' AND product.status='available' ORDER BY promotion.pID LIMIT $start,$limit");
}

echo "<table align = 'center' border = '1' cellpadding = '5' cellspacing = '2' width = '90%' style = 'border:solid;border-color:white'>";
echo "<tr style='background-color:#76888f'>";
//copy add

echo "<th id = 'table_data' width='10%'> Product ID</th>";
echo "<th id = 'table_data' width='20%'> Product Name</th>";
echo "<th id = 'table_data' width='13%'> Promotion Start Date</th>";
echo "<th id = 'table_data' width='13%'> Promotion End Date</th>";
echo "<th id = 'table_data' width='17%'> Normal Price</th>";
echo "<th id = 'table_data' width='17%'> Promotion Price</th>";
echo "<th id = 'table_data' width='10%'>Action</th>";
//until here
echo "</tr>";
while($row = mysql_fetch_array($result)){
	
	$ID=$row['id'];
	$pname1=$row['pName'];
	$pprice1=$row['price'];
	$checkpic = $ID.".";
	echo"<tr style='background-color:#BDBDBD'>";
	echo "<td align=center><input type='hidden' name='id' value='".$row["pID"]."'>" . $row['pID']."</td>";
	echo "<td align=center><input type='hidden' name='name' value='".$row["pname"]."'>" . $row['pname']."</td>";
	echo "<td align=center><input type='hidden' name='type' value='".$row["startdate"]."'>" . $row['startdate']."</td>";
	echo "<td align=center><input type='hidden' name='price' value='".$row["enddate"]."'>" . $row['enddate']."</td>";
	echo "<td align=center><input type='hidden' name='detail' value='".$row["normalprice"]."'>" .'RM '. $row['normalprice']."</td>";
	echo "<td align=center><input type='hidden' name='detail' value='".$row["promotionprice"]."'>" .$row['promotionprice']."</td>";
	echo"<td align=center><a href='promotionADD.php?user=$user&pid=$ID&pname=$pname1&pprice=$pprice1' id='link' rel='facebox'>Add Promotion</a> </br><a href='viewproduct.php?user=$user' id='link'>Edit promotion</a></td>";
	echo "</tr>";

}
	
echo "</table>";
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



echo "<div id='pageNavi'>";
//setup prev and next variables
$prev = $start - $limit;
$next = $start + $limit;

//show prev button
echo "<p align='center'>";
if (!($start<=0))
       {echo "<a href='viewproduct.php?user=$user&start=$prev' id='pag'>Previous</a> ";}

//show page numbers and set variable for first page
$p=1;
for ($x=0;$x<$record_count;$x=$x+$limit)
{
 if ($start!=$x)
    {echo "<a href='viewproduct.php?user=$user&start=$x' id='pag'>$p</a> ";}
 else
    {echo "<a href='viewproduct.php?user=$user&start=$x' style='color: #ff2626; font-family: Segoe UI Light' id='pag'><b>Page $p </b></a> ";}
 $p++;
}

//show next button
if (!($start>=$record_count-$limit))
       {echo " <a href='viewproduct.php?user=$user&start=$next' id='pag'>Next</a>";}
echo "</p>";

echo "</div>";

//$result->free();

?>

<!------------------------------------------------------------------>
<table width="100%">
<tr>
	<td align="center">
		<a href="company.php?user=<?php echo $user?>">
			<font size="4cm" color="purple">Back</font>
		</a>
	</td>
</tr>
</table>
</body>
</html>
<?php 
include("../closeconnection.php");
?>