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

session_start();
if (isset($_SESSION['adminlogin'])){
	//user in logen 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>	
	
	<fieldset style="width:85%"><legend>HELP</legend>
	<p>You can respond to the customer's feedbacks and questions on this page.</p> 
	</fieldset><br>
</div>

<div id="content">


<?php

// define variables and set to empty values
$RespondErr = "";
$Respond = "";

if ($_SERVER["REQUEST_METHOD"] != "POST")
{

//get the ID from the query string.
$FeedbackID = $_GET['id']; 

//if(!is_numeric($id))
//{ //check is a number.
//echo "Sorry, there appears to have been an error.";
//exit;
//}
}

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  $FeedbackID = $_POST['FeedbackID'];
  $FeedbackState = 'Answered';
  
  if (empty($_POST["Respond"]))
    {$RespondErr = "(Respond is required)";}
  else
    {$Respond = test_input($_POST["Respond"]);}
}

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

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


mysql_select_db("university", $con);


$query=Mysql_query("SELECT * FROM feedbacks WHERE FeedbackID=$FeedbackID ");
$row=mysql_fetch_object($query);
echo mysql_error();

$FeedbackID = $row->FeedbackID;
$FeedbackItem = $row->FeedbackItem;
$FeedbackName = $row->FeedbackName;
$FeedbackSubject = $row->FeedbackSubject;
$FeedbackComment = $row->FeedbackComment;
$FeedbackEmail = $row->FeedbackEmail;
$FeedbackState = $row->FeedbackState;
$FeedbackRespond = $row->FeedbackRespond;

if ($Respond != "")
{

$FeedbackState = 'Answered';

//replace single quotes from text field to insert to sql
$Respond2 = str_replace("'", "''", $Respond);



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


mysql_select_db("university", $con);

//construct a valid SQL statement that will be send to the database server.
$sql="UPDATE feedbacks SET FeedbackState = '$FeedbackState', FeedbackRespond = '$Respond2' 
WHERE FeedbackID = '$FeedbackID' ";

//display the SQL we will sent to server -  for debugging only
//echo $sql;

// run the SQL query on the server (i.e. send the SQL to the database server)

if (!mysql_query($sql,$con))
{
die ('Error: ' . mysql_error());
}

$Message = 'Dear, '. $FeedbackName . "\r\n";
$Message .= "--------------------------------" . "\r\n";
$Message .= "\r\n";

if (!empty($FeedbackItem))
{
$Message .= 'Item: ' . $FeedbackItem . "\r\n";
$Message .= "\r\n";
}
$Message .= 'Email: ' . $FeedbackEmail . "\r\n";
$Message .= "\r\n";
$Message .= 'Request: ' . $FeedbackComment . "\r\n";
$Message .= "\r\n";
$Message .= 'Respond: ' . $Respond . "\r\n";
$Message .= "\r\n";
$Message .= 'Your Sincerely Natty Noteboooks Inc.' . "\r\n";


$From = "istvan.franko@live.co.uk";

$Headers = 'From: ' . 'Natty Notebooks Inc.';
$Headers .= 'Reply-To: '. $FeedbackEmail;



mail($FeedbackEmail,$FeedbackSubject,$Message,$Headers);


echo "<h1>Feedback answered</h1><br>";
echo "<br>";
echo "<b>Mail Sent to: </b>" . $FeedbackEmail . "<hr>";

//close the connection to the database.
mysql_close($con); 

}
?>

<fieldset style="width:60%"><legend>RESPOND</legend>

<table border='0'>

<tr bgcolor='#e1e0e0'><td width='150'><b>ID:</b></td><td width='400'> <?php echo $FeedbackID;?> </td></tr>
<tr bgcolor='#f7f7f7'><td width='150'><b>Item:</b></td><td width='400'> <?php echo $FeedbackItem;?> </td></tr>
<tr bgcolor='#e1e0e0'><td width='150'><b>Name:</b></td><td width='400'> <?php echo $FeedbackName;?> </td></tr>
<tr bgcolor='#f7f7f7'><td width='150'><b>Subject:</b></td><td width='400'> <?php echo $FeedbackSubject;?> </td></tr>
<tr bgcolor='#e1e0e0'><td width='150'><b>Comment:</b></td><td width='400'> <?php echo $FeedbackComment;?> </td></tr>
<tr bgcolor='#f7f7f7'><td width='150'><b>Email:</b></td><td width='400'> <?php echo $FeedbackEmail;?> </td></tr>
<tr bgcolor='#e1e0e0'><td width='150'><b>State:</b></td><td width='400'> <?php echo $FeedbackState;?> </td></tr>

</table>

<form  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<p><span class="error">* required fields</span></p>
Respond: <span class="error">*</span><span class="error"> <?php echo $RespondErr;?> </span><br>
<br><textarea name="Respond" rows="10" cols="67"><?php echo $FeedbackRespond; ?></textarea><br><br>
<input type="hidden" name="FeedbackID" value="<?php echo $FeedbackID;?>">
<input type="submit" name="Submit" value="Send"><br>


</fieldset>




</div>

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


</div>
</body
</html>