<?php
/*
* @author Istvan Franko
* @copyright 2014
*/

session_start();
if (isset($_SESSION['adminlogin'])){
	//user in logged in. Show a message on the screen
} else {
	//redirect the user to the login form.
	session_destroy();
	header('Location: index.php');
}
?>

<html>
 
<head>
<title>Natty Notebooks Inc.</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>



<div id="container">

<div id="header">
	<! header picture in css >
</div>
	
<div id="navigation">
	<?php include 'admin-nav-bar.php'; ?>
</div>
	

<div id="aside">
	<?php include 'filter.php'; ?>
	
	<br><fieldset style="width:85%"><legend>HELP</legend>
	<p>All of our products are listed here.</p>
	<p>Here you can modify and delete the items with
	<span class="red">-Update-</span> or <span class="red">-Delete-</span> links. </p>
	<p>Scroll through the pages at the bottom with the previous and next links, 
	or you can use the filters at the top.</p>
	</fieldset><br>
</div>

<div id="content">
<br>
<fieldset style="width:95%"><legend>LIST OF ITEMS</legend>

<?php

//define variables 
$OrderBy = "ItemID";
$Category = "%";
$Brand = "%";
$Software = "%";
$Cpu = "%";
$Mem = "%";
$Hdd = "%";
$Screen = "%";

//pagination function control
$pagination_switch = 0;

//items per page
$rec_limit = 8;


//get variables from filter module
if ($_SERVER["REQUEST_METHOD"] == "POST")
	{
	$OrderBy = test_input($_POST["OrderBy"]);
	$Category = test_input($_POST["Category"]);
	$Brand = test_input($_POST["Brand"]);
	$Software = test_input($_POST["Software"]);
	$Cpu = test_input($_POST["Cpu"]);
	$Mem = test_input($_POST["Mem"]);
	$Hdd = test_input($_POST["Hdd"]);
	$Screen = test_input($_POST["Screen"]); 

	$pagination_switch = 1;
	}

//data validation
function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

//sql connection settings
$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("university", $con);

/* Get total number of records */
$sql = "SELECT count(ItemID) FROM natty ";
$retval = mysql_query( $sql, $con );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

//how many pages have
$last_page = ceil($rec_count / $rec_limit);

if( isset($_GET{'page'} ) )
{
   $page = $_GET{'page'} + 1;
   $offset = $rec_limit * $page ;
}
else
{
   $page = 0;
   $offset = 0;
}

//count left records
$left_rec = $rec_count - ($page * $rec_limit);


//sql query
$sql = "SELECT * FROM natty 
	WHERE ItemCategory LIKE '%$Category%' AND
	ItemBrand LIKE '%$Brand%' AND
	ItemOpSys LIKE '%$Software%' AND
	ItemCpu LIKE '%$Cpu%' AND
	ItemMem LIKE '%$Mem%' AND
	ItemHdd LIKE '%$Hdd%' AND
	ItemScreenSize LIKE '%$Screen%' 
	ORDER BY $OrderBy ";
	
if 	($pagination_switch == 0)
	{ 
	$sql .= "LIMIT $offset, $rec_limit";
	}

$retval = mysql_query( $sql, $con );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}

//table
echo "<table border='1' cellpadding='5'>";

//table header
echo "
	<tr><td width='200' align='center'><b>Name</b></td>
	<td width='70' align='center'><b>Picture</b></td>
	<td width='70' align='center'><b>Price</b></td>
	<td width='90' align='center'><b>Update Link</b></td>
	<td width='90' align='center'><b>Delete Link</b></td>
	<td width='100' align='center'><b>In Catalogue</b></td></tr>
	";
//variable to background colour
$i=0;

//loop for sql result
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{

//loop for rows background colour change
if ($i % 2 == 0)
{
echo "<tr bgcolor='#e1e0e0'>";
}
else
{
echo "<tr bgcolor='#f7f7f7'>";
}

//item title
if ($row['ItemSpecialOffer'] != '0')
{ 
echo "<td width='160' align='right' background='../images/special_offer.png' ";
echo "style='background-repeat:no-repeat;background-position:left top;' ><br><br>";
}
else
{
echo "<td width='160'>";
}
echo "<p>";
echo "<a href=\"details.php?id=";
echo $row['ItemID'];
echo "\">";
echo $row['ItemName'];
echo "</a>";
echo "</p></td>";

//item thumbnail
echo "<td width='70' align='center'>";
echo "<a href=\"../images/";
echo $row['ItemPicture1'];
echo "\">";
echo "<img width='60' src=\"../images/";
echo $row['ItemPicture1'];
echo "\" >";
echo "</a>";
echo "</p>";
echo "</td>"; 

//Item price
echo "<td width='70' align='center'>";
echo "&#163;" . $row['ItemPrice'];
echo "</td>";


//Update item
echo "<td width='90' align='center'>";
echo "<a href=\"update-form.php?id=";
echo $row['ItemID'];
echo "\">";
echo ' Update ';
echo "</a>";
echo "</td>";

//Delete item
echo "<td width='90' align='center'>";
echo "<a href=\"delete.php?id=";
echo $row['ItemID'];
echo "\">";
echo ' Delete ';
echo "</a>";
echo "</td>";

//Item in catalogue
echo "<td width='100' align='center'>";
if ($row['ItemAvailable'] != '0'){ echo "Yes";} else { echo "No";}
echo "</td>";

echo "</tr>";
$i++;
}
echo "</table><br>";

//Previous and next page links
if 	($pagination_switch == 0)
{

	if( $page > 0 )
	{
		$last = $page - 2;
		echo "<a href=\"admin.php?page=$last\">Previous page </a>";
		if ($page < $last_page-1)
		{
			echo "<a style='float: right;' href=\"admin.php?page=$page\">Next page </a>";
		}
	}
	else if( $page == 0 )
	{
		echo "<a style='float: right;' href=\"admin.php?page=$page\">Next page </a>";
	}
	else if( $left_rec < $rec_limit )
	{
		$last = $page - 2;
		echo "<a href=\"admin.php?page=$last\">Previous page </a>";
	}
}

//close connection
mysql_close($con);

?>

</fieldset>
</div>

<div id="footer">
<p align="center">Copyright &#169; Istvan Franko, 2014</p>
</div>
	

</div>
</body>
</html>