<?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">
	<!--Links for navigation-->
	<a href="admin.php"><p style="text-align:center">Back to list</a></p>		

	<br><fieldset style="width:85%"><legend>HELP</legend>
	<p>All feedbacks and questions are listed here.</p>
	<p>Here you can respond them under the
	<span class="red">-Respond-</span> links. </p>
	<p>Scroll through the pages at the bottom with the previous and next links, 
	or you can use the filters above.</p>
	</fieldset><br>
</div>

<div id="content">

<br>
<fieldset style="width:90%"><legend>FEEDBACKS LIST</legend>

<?php

//define variables 
$ID = "%";
$Name = "%";
$Subject = "%";
$State = "%";

//pagination function control
$pagination_switch = 0;

//items per page
$rec_limit = 10;

//get variables from search fields
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  $ID = test_input($_POST["ID"]);
  $Name = test_input($_POST["Name"]);
  $Subject = test_input($_POST["Subject"]);
  $State = test_input($_POST["State"]);
  
  $pagination_switch = 1;
}

function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<?php
$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("university", $con);

/* Get total number of orders */
$sql = "SELECT count(FeedbackID) FROM feedbacks ";
$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 orders
$left_rec = $rec_count - ($page * $rec_limit);

//sql command
$sql="SELECT * FROM feedbacks 
	WHERE 
	FeedbackID LIKE '%$ID%' AND
	FeedbackName LIKE '%$Name%' AND
	FeedbackState LIKE '%$State%' AND
	FeedbackSubject LIKE '%$Subject%'";
	
if ($pagination_switch == 0)
	{
		$sql .= "LIMIT $offset, $rec_limit";
	}

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

//search form
echo "<input type='submit' name='Submit' value='Search'>";
echo "<span class='error'> *You can search through the feedbacks of the following search fields</span>";

//display a table
echo "<table align='center' border='1'>";


//table head
echo "<tr><td width='90'><b>Feedback ID</b></td>
	<td width='150'><b>User Name</b></td>
	<td width='200'><b>Subject</b></td>
	<td width='80' align='center'><b>Link</b></td>
	<td width='80' align='center'><b>State</b></td>
	";


// input form	
echo "
	<tr><td width='90' align='center'><input type='text' size='10' name='ID'></td>
	<td width='150' align='center'><input type='text' size='20' name='Name'></td>
	<td width='200' align='center'><input type='text' size='25' name='Subject'></td>
	<td width='800' align='center'><span class='error'> *search fields</span></td>
	<td width='80' align='center'>
	<select name='State'>
	<option value='%'>All</option>	
	<option value='Answered'>Answered</option>
	<option value='Waiting'>Waiting</option>
	</select></td></tr>
	</form>
	";
	
$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'>";
}


//Feedback ID
echo "<td width='90'>";
echo $row['FeedbackID'];
echo "</td>";

//Feedback Name
echo "<td width='150'>";
echo $row['FeedbackName'];
echo "</td>";


//Feedback Subject
echo "<td width='200'>";
echo $row['FeedbackSubject'];
echo "</td>";

//Respond to feedback
echo "<td width='80' align='center'>";
echo "<a href=\"respond.php?id=";
echo $row['FeedbackID'];
echo "\">";
echo ' Respond ';
echo "</a>";
echo "</td>";

//Feedback State
echo "<td width='80' align='center'>";
echo $row['FeedbackState'];
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=\"feedbacks-list.php?page=$last\">Previous page </a>";
		if ($page < $last_page-1)
		{
			echo "<a style='float: right;' href=\"feedbacks-list.php?page=$page\">Next page </a>";
		}
	}
	else if( $page == 0 )
	{
		echo "<a style='float: right;' href=\"feedbacks-list.php?page=$page\">Next page </a>";
	}
	else if( $left_rec < $rec_limit )
	{
		$last = $page - 2;
		echo "<a href=\"feedbacks-list.php?page=$last\">Previous page </a>";
	}
}

mysql_close($con);

?>

</fieldset>
</div>

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


</div>
</body
</html>