<?php 
include("connect.php");
//include("session.php");
include_once("functions.php");
$order = new Order($_REQUEST['order']);
$realKey = md5($order->OrderID . "WestingPR");
$realKey = substr($realKey, -16); 
$key = $_REQUEST['key'];
if (isset($_REQUEST['key'])) {
	if ($realKey != $key) {
		header("Location: index.php");
	}
}else {
	header("Location: index.php");
}

/*
//validate so the user can't see other customers orders
if ($_SESSION['isCustomer']) {
	$customer = new Customer($_SESSION['AccountNumber']);	
	if ($order->Customer->AccountNumber != $customer->AccountNumber) {
		header("Location: index.php");
	}
}else {
	$salesman = new Salesman($_SESSION['SalesmanID']);
	if ($order->Salesman->ID != $salesman->ID) {
		header("Location: index.php");
	}
}
*/
 ?>
 <html>
 <body style="font-family: sans-serif;">
   <div id="officeOnly" style="float: right; width: 145px; height:50px; border-style: solid; border-width: 1px; text-align: center;"></div>
   
    <!-- Stack the columns on mobile by making one full-width and the other half-width -->
 <img src="<?= $root ?>img/Logo_125.png" style="width: 122px;" /><br />
 <br />
 	<div id="header" style="margin-top: 0px;">
 		<div style="width: 60%;  height: 120px; float: left;">
 			
 			<b>Customer Name: </b><?= $order->Customer->Name; ?><br />
 			<b>City: </b><?= $order->Customer->City; ?><br />
 			<b>Customer Number: </b><?= $order->Customer->AccountNumber ?><br />
 			<b>PO Number: </b><?= $order->PONumber ?><br />
 			<b>Delivery Method: </b><?= $order->DeliveryMethod ?><br />
 		</div>
 		<div style="width: 38%; height: 120px; float: right;">
 			<b>Order Number: </b><span style="font-style: italic;"><?= $order->OrderID ?></span><br />
			<?php
		
			if($order->OfflineID){
			?>
				<b>Offline ID: </b><span style="font-style: italic;"><?= $order->OfflineID ?></span><br>
			<?php
			}
			
			?>
 			<b>Order By: </b><?= ($order->SalesmanOrder == "0")?"Customer":"Salesman" ?><br />
 			<b>Date: </b><?= date("m/d/Y",strtotime($order->OrderDate)) ?><br />
 			<b>Time: </b><?= date("g:i a",strtotime($order->OrderDate)) ?><br />
 			<b>Salesman: </b><?= $order->Salesman->ID . " - " . $order->Salesman->Name ?><br />
 		</div>
 	</div>
 
 <div id="content" style="width: 100%;">
 	<h4 style="text-align: center;">WEB ORDER CONFIRMATION</h4>
 	<table style="border: 1px solid #333; border-collapse: collapse;">
 		<thead>
 			<tr>
 				<th style="border: solid 1px black; width: 5%;">Line<br />Group</th>
 				<th style="border: solid 1px black; width: 60%;">Item</th>
 				<th style="border: solid 1px black; width: 5%;">Order Quantity</th>
 				<th style="border: solid 1px black; width: 5%;">Price</th>
 				<th style="border: solid 1px black; width: 5%;">Extension</th>
 			</tr>
 		</thead>
 		<tbody>
 			<?php
 				$query = "SELECT *, IF(Line = 'MIS', 2, 1) PartOrder FROM ORDER_DETAIL WHERE OrderID = '$order->OrderID' ORDER BY PartOrder, Line, `Group`, SKU";
 				$result = mysqli_query($con, $query);
 				$index = 0;
 				while ($row = mysqli_fetch_array($result)) {
					$itemObj = new Item($row['Group'], $row['Line'], $row['SKU']);
 			?>
 			<tr>
 				<td style="border: solid 1px black; text-align: center;">
 					<?= $row['Line'] ?><br /><?= $row['Group'] ?></td>
 				<td style="border: solid 1px black;"><b><?= $row['SKU'] ?></b><br /><small>
				<?php
						if(substr($order->AccountNumber,0,5) == "10002" && $itemObj->getAttribute("SearsItem")){
							echo "<b>" . $itemObj->getAttribute("SearsItem") . "</b> - ";
						}
						echo $row['Description'];
						
						?>
				<?= $row['Notes'] ?></small></td>
 				<td style="border: solid 1px black; text-align: center;"><?= $row['OrderQty'] ?></td>
 				<td style="border: solid 1px black; text-align: right; padding-right: 5px;">
 					<?= moneyFormat($row['Price']); ?>
 					<?php 
 						if(floatval($row['Core'])>0){
 							echo "<br><i><b>Core:</b> ". moneyFormat($row['Core']) . "</i>";
 						}
 					?>
 					
 				</td>
 				<td style="border: solid 1px black; text-align: right; padding-right: 5px;">
 					<?= moneyFormat($row['OrderQty'] * $row['Price']) ?>
 					<?php 
 						if(floatval($row['Core'])>0){
 							echo "<br><i><b>Core:</b> ". moneyFormat($row['OrderQty'] * $row['Core']) . "</i>";
 						}
 					?>
 				</td>
 			</tr>
 			<?php 
 			$index++;	 
 			}?>
 		</tbody>
 	</table>
 	<div style="float: right; text-align: right; padding-top: 10px; padding-right: 5px; font-size: 1.2em;"><b>Total: </b>
 		<?= moneyFormat($order->OrderTotal) ?>
 	</div>
 	<div style="float: left; padding: 5px; margin-top: 5px; border: 1px solid #333; width: 50%; word-wrap: break-word;"><b>Order Notes: </b><br />
 		 <?= $order->OrderNotes ?>
 	</div>
 	<div id="footer" style="margin-top: 60px;">
 		**This is not an invoice. Tax is not included in the total amount.
 	</div>
 </div>
 </body>
 </html>