<?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="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
       <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
       <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
           <script src="https://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">Estimate 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>Estimate ID</th>
						<?= ($_SESSION['isCustomer'] != true) ? "<th>Customer</th>" : "" ?>
	  				<th>Estimate Date/Time</th>
	  				<th>PO Number</th>
	  				<th>Processed</th>
	  				<th>Total</th>
	  			</tr>
	  			
	  		</thead>
	    <?php 
	    if ($_SESSION['isCustomer']) {
	    	$query = "SELECT EstimateID FROM ESTIMATE_HEAD WHERE AccountNumber LIKE '" . $_SESSION['AccountNumber'] . "' AND DATE(EstimateDate) BETWEEN '$fromDate' AND '$toDate' ORDER BY EstimateID DESC";	
	    }else {
      
	    	//$query = "SELECT EstimateID FROM ESTIMATE_HEAD WHERE Salesman LIKE '" . $_SESSION['SalesmanID'] . "' AND DATE(EstimateDate) BETWEEN '$fromDate' AND '$toDate' ORDER BY EstimateID DESC";
      $chooseSalesmanID = $_SESSION['SalesmanID'];
      if($chooseSalesmanID == "AS" || $chooseSalesmanID == "HC" || $chooseSalesmanID == "CC" || $chooseSalesmanID == "ES" || $chooseSalesmanID == "FM" || $chooseSalesmanID == "EM" || $chooseSalesmanID == "AG"|| $chooseSalesmanID == "KO"){
							$chooseSalesmanID = "%"	;
						}
      $query = "SELECT EstimateID FROM ESTIMATE_HEAD WHERE Salesman LIKE '" . $chooseSalesmanID . "' AND DATE(EstimateDate) BETWEEN '$fromDate' AND '$toDate' ORDER BY EstimateID DESC";
	    }
	   	$result = mysqli_query($con, $query);
		$estimatesCount = 0;
		$estimatesProcessed = 0;
		$estimatesNotProcessed = 0;
		$estimatesProcessedTotal = 0;
		$estimatesNotProcessedTotal = 0;
	   	while ($row = mysqli_fetch_array($result)) {
	   		$estimate = new Estimate($row['EstimateID']);
	   		if($estimate->Processed == true){
			  $estimatesProcessed++;
				$estimatesProcessedTotal += $estimate->EstimateTotal;
			}else{
			  $estimatesNotProcessed++;
				$estimatesNotProcessedTotal +=$estimate->EstimateTotal;
			}
	   	?>
	   		<tr onclick="window.location = '<?= $estimate->getViewLink(false) ?>'">
	   			<td><?= $estimate->EstimateID ?></td>
	   			<?= ($_SESSION['isCustomer'] != true) ? "<td>" . trim($estimate->Customer->Name) . "</td>" : "" ?>
	   			<td><?= date("m/d/Y g:i a",strtotime($estimate->EstimateDate)) ?></td>
	   			<td><?= $estimate->PONumber ?></td>
	   			<td><?= $estimate->Processed?"Yes":"No" ?></td>
	   			<td><?= moneyFormat($estimate->EstimateTotal) ?></td>
	   		</tr>
	   	<?php
	   	}
		
	     ?>
	     </table>
	  </div>
	  <div class="panel-footer" style="height: 80px;" >
	  	
	  	<span  style="float: right; text-align: right;">
	  		<strong>Total Processed Estimates: </strong><i><?= moneyFormat($estimatesProcessedTotal) ?></i><br />
				<strong>Total Not Processed Estimates: </strong><i><?= moneyFormat($estimatesNotProcessedTotal) ?></i><br />
			<strong>Total Transactions: </strong><i><?= $estimatesCount ?></i><br />
			
	  	</span>	
	  </div>
	</div>

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