<?php 
include("connect.php");
include("session.php");

$fromDate = $_REQUEST['from'];
$toDate = $_REQUEST['to'];
 ?>
 
 <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title><?= $config['CompanyName'] ?></title>
 
     <!-- Bootstrap -->
     <link href="css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="css/bootstrap-combobox.css" />
 
     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
     <!--[if lt IE 9]>
       <script src="http://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
       <script src="http://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
       <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
           <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
           <!-- Include all compiled plugins (below), or include individual files as needed -->
           <script src="js/bootstrap.min.js"></script>
       	 <script src="js/bootstrap-combobox.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
            
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="css/styles.css"/>
   </head>
   <body>
   
       <?php include("navbar.php"); ?>     

   
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="container " style="padding-top: 10px;">
	<div class="panel panel-default">
	  <div class="panel-heading">
	    <h3 class="panel-title">Order History  - <?= $_SESSION['Name']  . " - <i>From: " . date("m/d/Y",strtotime($fromDate)) . " To: " . date("m/d/Y",strtotime($toDate)) . "</i>"?></h3>
	  </div>
	  <div class="panel-body">
	  	<table class="table table-hover">
	  		<thead>
	  			<tr>
	  				<th>Order ID</th>
	  				<?= ($_SESSION['isCustomer'] != true) ? "<th>Customer</th>" : "" ?>
	  				<th>Order Date/Time</th>
	  				<th>PO Number</th>
	  				<th>Order By</th>
	  				<th>Total</th>
	  			</tr>
	  			
	  		</thead>
	    <?php 
	    if ($_SESSION['isCustomer']) {
	    	$query = "SELECT * FROM ORDER_HEAD WHERE AccountNumber LIKE '" . $_SESSION['AccountNumber'] . "' AND DATE(OrderDate) BETWEEN '$fromDate' AND '$toDate' ORDER BY OrderID DESC";	
	    }else {
	    	$query = "SELECT * FROM ORDER_HEAD WHERE Salesman LIKE '" . $_SESSION['SalesmanID'] . "' AND DATE(OrderDate) BETWEEN '$fromDate' AND '$toDate' ORDER BY OrderID DESC";
	    }
	   	$result = mysqli_query($con, $query);
		$ordersByCustomerTotal = 0;
		$ordersBySalesmanTotal = 0;
		$ordersByCustomerCount = 0;
		$ordersBySalesmanCount = 0;
	   	while ($row = mysqli_fetch_array($result)) {
	   		$order = new Order($row['OrderID']);
	   		if($row['SalesmanOrder'] == true){
			  $ordersBySalesmanTotal += $order->OrderTotal;
			  $ordersBySalesmanCount++;
			}else{
			  $ordersByCustomerTotal += $order->OrderTotal;
			  $ordersByCustomerCount++;
			}
	   	?>
	   		<tr onclick="window.location = 'vieworder.php?order=<?= $row['OrderID'] ?>'">
	   			<td><?= $row['OrderID'] ?></td>
	   			<?= ($_SESSION['isCustomer'] != true) ? "<td>" . trim($order->Customer->Name) . "</td>" : "" ?>
	   			<td><?= date("m/d/Y g:i a",strtotime($row['OrderDate'])) ?></td>
	   			<td><?= $row['PONumber'] ?></td>
	   			<td><?= ($row['SalesmanOrder'] == true)?"Salesman":"Customer" ?></td>
	   			<td><?= moneyFormat($order->OrderTotal) ?></td>
	   		</tr>
	   	<?php
	   	}
		$total = $ordersByCustomerTotal + $ordersBySalesmanTotal;
	     ?>
	     </table>
	  </div>
	  <div class="panel-footer" style="height: 80px;" >
	  	
	  	<span  style="float: right; text-align: right;">
	  		<strong>Total Orders: </strong><i><?= moneyFormat($total) ?></i><br />
			<strong>Total Transactions: </strong><i><?= $ordersBySalesmanCount+$ordersByCustomerCount ?></i><br />
			
	  	</span>	
	  </div>
	</div>

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