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

session_start();

if (!isset($_SESSION['cart'])) 
{
  $_SESSION['cart']=array(); 
}

if (!isset($_SESSION['loggedin'])) 
{
  $_SESSION['loggedin'] = '0'; 
}
?>

<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 'navigation-bar.php'; ?>
	</div>
	
<div id="aside">
	<!--Link for navigation-->
	<a href="product-list.php"><p style="text-align:center">Back to list</a></p>	

	<fieldset style="width:85%"><legend>HELP</legend>
	<p>If you provided all requested information we will record your order.</p> 
	<p>We will also send confirmation email to you.</p>
</div>

<div id="content">

<br>
<fieldset style="width:95%"><legend>CONFIRM</legend>
<br>
<p><img width="120" src="images/shopping-cart.png" alt="Shopping Cart" style="float:left">


<?php

//read session array to local array
while (list ($key, $val) = each ($_SESSION['cart'])) 
{
$cart[$key] = $val;
}

//get  the values posted from the form 
if(!empty($_POST)) {
$OrderNumber = $_POST['OrderNumber'];
$totalprice = $_POST['totalprice'];

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

mysql_select_db("university", $con);

//get order informations to local variable
$query=Mysql_query("SELECT * FROM natty_orders WHERE OrderNum=$OrderNumber ");
$row=mysql_fetch_object($query);
echo mysql_error();

$OrderItemInCart=$row->OrderItemInCart;
$OrderName=$row->OrderName;
$OrderEmail=$row->OrderEmail;



//change order state to Live in database
$state = "Live";

$sql="UPDATE natty_orders SET OrderState='$state' 
WHERE OrderNum='$OrderNumber' ";

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



//loop to get ordered items details from database
for ($x=0; $x != $OrderItemInCart; $x++)
{
$y=$x+1;
$FromMbro = 'OrderFromMbro' . $y;
$FromStock = 'OrderFromStock' . $y;
$OrderFromMbro[$x]=$row->$FromMbro;
$OrderFromStock[$x]=$row->$FromStock;
}


//loop to get item details from database
for ($x=0; $x != $OrderItemInCart; $x++)
{
$query=Mysql_query("SELECT * FROM natty WHERE ItemID=$cart[$x] ");
$row=mysql_fetch_object($query);
echo mysql_error();

$InMbro[$x]=$row->ItemInMbro;
$InStock[$x]=$row->ItemInStockton;
$ItemName[$x]=$row->ItemName;
$ItemPrice[$x]=$row->ItemPrice;
}

//calculate new quantity
for ($x=0; $x != $OrderItemInCart; $x++)
{
$NewInMbro[$x]=$InMbro[$x] - $OrderFromMbro[$x];
$NewInStock[$x]=$InStock[$x] - $OrderFromStock[$x];


//update available items in stores
$sql="UPDATE natty SET ItemInMbro=$NewInMbro[$x], ItemInStockton=$NewInStock[$x]
WHERE ItemID=$cart[$x] ";

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

//send confirmation email to customer

$Message = "Dear, ". $OrderName . "\r\n";
$Message .= "--------------------------------" . "\r\n";
$Message .= "Email: " . $OrderEmail . "\r\n";
$Message .= "\r\n";
$Message .= "Thanks for your order." . "\r\n";
$Message .= "\r\n";
$Message .= "Your order number is: " . $OrderNumber . "\r\n";
$Message .= "\r\n";
$Message .= "The ordered item/s: " . "\r\n";
$Message .= "\r\n";

for ($x=0; $x != $OrderItemInCart; $x++)
{
	if ($OrderFromMbro[$x] != 0)
	{
		$Message .= $OrderFromMbro[$x] . " qty " . $ItemName[$x] . " from Middlesbrough shop." . "\r\n";
	}
	
	if ($OrderFromStock[$x] != 0)
	{
		$Message .= $OrderFromStock[$x] . " qty " . $ItemName[$x] . " from Stockton shop." . "\r\n"; 
	}
	
$Message .= "\r\n";
$Message .= "Unit price: £" . $ItemPrice[$x] . "\r\n";
$Message .= "---------------------------------------------------" . "\r\n";
}
$Message .= "\r\n";
$Message .= "Your cart total is: £" . $totalprice . "\r\n";
$Message .= "\r\n";
$Message .= 'Your Sincerely Natty Noteboooks Inc.' . "\r\n";

$Headers = 'From: ' . 'Natty Notebooks Inc.';
$Headers .= 'Email: istvan.franko@live.co.uk';


$Subject = 'Order confirmation';

mail($OrderEmail,$Subject,$Message,$Headers);

echo "<p>Thanks for your order</p><hr/><br>";
echo "<p>We sent a confirmation email to you.</p>";	

mysql_close($con);
}

//all data saved to sql, so we can empty the cart session
for ($x=0; $x != $OrderItemInCart; $x++)
{
unset($_SESSION['cart'][$x]);
}

?>


</fieldset>

</div>

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


</div>

</body>
</html>