<?php
include("../session.php");
include("../connect.php");
$user=$_GET["user"];
?>
<html>
<head>
	<script src="javascript/src/js/jscal2.js"></script>
    <script src="javascript/src/js/lang/en.js"></script>
	<link rel="stylesheet" type="text/css" href="javascript/src/css/jscal2.css" />
    <link rel="stylesheet" type="text/css" href="javascript/src/css/border-radius.css" />
    <link rel="stylesheet" type="text/css" href="javascript/src/css/steel/steel.css" />
	<link rel="stylesheet" href="javascript/include/jquery-ui-1.8.14.custom.css" type="text/css" />
    <link rel="stylesheet" href="javascript/jquery.ui.timepicker.css?v=0.2.9" type="text/css" />
	<script type="text/javascript" src="javascript/include/jquery.ui.position.min.js"></script>
    <script type="text/javascript" src="javascript/jquery.ui.timepicker.js?v=0.2.9"></script>
	<!--<script type="text/javascript" src="javascript/include/jquery-1.5.1.min.js"></script>-->
	
	
	<link type="text/css" href="./css/docs.css" rel="stylesheet" media="all" />
	<link type="text/css" href="./css/jquery.ibutton.css" rel="stylesheet" media="all" />
	<script type="text/javascript" src="jquery.min.js"></script>
	<script type="text/javascript" src="./lib/jquery.ibutton.js"></script>
	<script type="text/javascript" src="./lib/jquery.easing.1.3.js"></script>
	<script type="text/javascript" src="./lib/jquery.metadata.js"></script>
	<!---------------------------------------------------------------------------------------------------------------->
<script type="text/javascript">
	function debug(){
		if( window.console && window.console.log ) console.log(arguments);
	}
	$(document).ready(function (){
		$(".group").iButton({
			  init: function (){
				debug("init", arguments);
			}
			, change: function (){
				debug("change", arguments);
			}
			, click: function (){
				debug("click", arguments);
			}
			, disable: function (){
				debug("disable", arguments);
			}
			, destroy: function (){
				debug("destroy", arguments);
			}
		});
		$("#radio_allowRadioUncheck :radio").iButton({allowRadioUncheck: true});
		}
		
</script>

<script type="text/javascript">
function vali(count,a,b,c,d,e){
	var x = count;
	var pID = e;
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
	}
	
	if (document.getElementById("ex03"+x).checked==true){
	var startdate = a;
	var enddate = b;
	var pPrice = c;
	var pcondition = d;
	//on anction
	document.getElementById("a"+x).innerHTML = "<input type='hidden' size='12' name='startdate"+x+"' id='startdate"+x+"'>"+startdate;
	document.getElementById("b"+x).innerHTML = "<input type='hidden' size='12' name='enddate"+x+"' id='enddate"+x+"'>"+enddate;
	document.getElementById("c"+x).innerHTML = "<input type='hidden' name='pPrice"+x+"' id='pPrice"+x+"'>"+pPrice;
	document.getElementById("d"+x).innerHTML = "<input type='hidden' name='pcondition"+x+"' id='pcondition"+x+"'>"+pcondition;
	
	xmlhttp.open('GET','on.php?a='+startdate+'&b='+enddate+'&c='+pPrice+'&d='+pcondition+'&e='+pID+'',true);
	xmlhttp.send();
	
	//alert(startdate+","+enddate+","+pPrice+","+pcondition+","+pID);
	}
	else{
	var startdate = "";
	var enddate = "";
	var pPrice = "";
	var pcondition = "";
	//off action
	document.getElementById("a"+x).innerHTML = "<input type='text' style='cursor:pointer' name='startdate"+x+"' size='12' id='startdate"+x+"' onClick='updateFields(this.id)'/>";
	document.getElementById("b"+x).innerHTML = "<input type='text' style='cursor:pointer' name='enddate"+x+"' size='12' id='enddate"+x+"' onClick='updateFields2(this.id)'>";
	document.getElementById("c"+x).innerHTML = "<input type='text' name='pPrice"+x+"' id='pPrice"+x+"'>";
	document.getElementById("d"+x).innerHTML = "<textarea rows='2' cols='30' style='resize:none' name='pcondition"+x+"' id='pcondition"+x+"' maxlength='500'></textarea>";
	
	xmlhttp.open('GET','off.php?a='+startdate+'&b='+enddate+'&c='+pPrice+'&d='+pcondition+'&e='+pID+'',true);
	xmlhttp.send();
	
	//alert(startdate+","+enddate+","+pPrice+","+pcondition+","+pID);
	} 	
}
</script>
</head>
<body>

<script type="text/javascript">
$(document).ready(function (){
  $(":checkbox").iButton();
});
</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='10%'> Normal Price (RM)</th>";
echo "<th id = 'table_data' width='10%'> Promotion Price (RM)</th>";
echo "<th id = 'table_data' width='14%'> Condition</th>";
echo "<th id = 'table_data' width='10%'>Action</th>";
//until here
echo "</tr>";
$aa = 1;
while($row = mysql_fetch_array($result)){

	if($row['status'] == 'on'){
		echo"<tr style='background-color:#BDBDBD'>";
		echo "<td align=center><input type='hidden' name='id".$aa."' id='id".$aa."' value='".$row["pID"]."'>" . $row['pID']."</td>";
		echo "<td align=center><input type='hidden' name='pname' id='pname' value='".$row["pname"]."'>" . $row['pname']."</td>";
		echo "<td align=center><div id='a".$aa."'><input type='hidden' name='startdate".$aa."' id='startdate".$aa."' size='12' value='".$row["startdate"]."'>" .$row['startdate']."</div></td>";
		echo "<td align=center><div id='b".$aa."'><input type='hidden' name='enddate".$aa."' id='enddate".$aa."' size='12' value='".$row["enddate"]."'>" . $row['enddate']."</div></td>";
		echo "<td align=center><input type='hidden' name='nPrice' id='nPrice' value='".$row["normalprice"]."'>" . $row['normalprice']."</td>";
		echo "<td align=center><div id='c".$aa."'><input type='hidden' name='pPrice".$aa."' id='pPrice".$aa."' value='".$row["promotionprice"]."'>" .$row['promotionprice']."</div></td>";
		echo "<td align=center><div id='d".$aa."'><input type='hidden' name='pcondition".$aa."' id='pcondition".$aa."' value='".$row["pcondition"]."'>" . $row['pcondition']."</div></td>";
		echo"<td align=center style='cursor:pointer'>
					<div id='metadata' class='group'>
					<div class='row'>
					<input type='checkbox' id='ex03".$aa."' name='status' class='{labelOn: 'Yes', labelOff: 'No'}' onchange='vali($aa,startdate".$aa.".value,enddate".$aa.".value,pPrice".$aa.".value,pcondition".$aa.".value,id".$aa.".value)' checked='checked'/>
					</div>
					</div>
			</td>";
		echo "</tr>";
	}
	else{
		echo"<tr style='background-color:#BDBDBD'>";
		echo "<td align=center><input type='hidden' name='id".$aa."' id='id".$aa."' value='".$row["pID"]."'>" . $row['pID']."</td>";
		echo "<td align=center><input type='hidden' name='pname' id='pname' value='".$row["pname"]."'>" . $row['pname']."</td>";
		echo "<td align=center><div id='a".$aa."'><input type='text' style='cursor:pointer' name='startdate".$aa."' size='12' id='startdate".$aa."' onClick='updateFields(this.id)'/></div></td>";
		echo "<td align=center><div id='b".$aa."'><input type='text' style='cursor:pointer' name='enddate".$aa."' size='12' id='enddate".$aa."' onClick='updateFields2(this.id)'></div></td>";
		echo "<td align=center><input type='hidden' name='nPrice' id='nPrice' value='".$row["normalprice"]."'>".$row['normalprice']."</td>";
		echo "<td align=center><div id='c".$aa."'><input type='text' name='pPrice".$aa."' id='pPrice".$aa."'></div></td>";
		echo "<td align=center><div id='d".$aa."'><textarea rows='2' cols='30' style='resize:none' name='pcondition".$aa."' id='pcondition".$aa."' maxlength='500'></textarea></div></td>";
		echo"<td align=center style='cursor:pointer'>
					<div id='metadata' class='group'>
					<div class='row'>
					<input type='checkbox' id='ex03".$aa."' name='status' class='{labelOn: 'Yes', labelOff: 'No'}' onchange='vali($aa,startdate".$aa.".value,enddate".$aa.".value,pPrice".$aa.".value,pcondition".$aa.".value,id".$aa.".value)'/>
					</div>
					</div>
			</td>";
		echo "</tr>";	
		
	}$aa++;
}
	
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();

?>
<script type="text/javascript">//<![CDATA[

      var cal = Calendar.setup({
          onSelect: function(cal) { cal.hide() },
		  min: Calendar.dateToInt(new Date()),
          showTime: true
		  
		  
      });
	  function updateFields(str){
      cal.manageFields(str, str, "%d-%m-%Y");
	  }
    //]]></script>
	    <script type="text/javascript">//<![CDATA[

      var cal = Calendar.setup({
          onSelect: function(cal) { cal.hide() },
		   min: Calendar.dateToInt(new Date()),
          showTime: true
		  
      });
	  function updateFields2(str){
      cal.manageFields(str, str, "%d-%m-%Y");
	  }
    //]]></script>
<!------------------------------------------------------------------>
<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");
?>