<!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 src="../js/excellentexport.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
       //        $('.grpline').css("width", "100px");
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
   </head>
   <body>
       <?= 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 form-inline">
	  <a class="btn btn-success hidden-print" role="button" style="float: right;" href="#" download="LastCustomerPurchase<?= date('y-m-d') ?>.xls" href="#" onclick="return ExcellentExport.excel(this, 'lastcustomerpurchase', 'Last Customer Purchase');" data-toggle="tooltip" data-placement="left" title="Does not work with Internet Explorer">Export To Excel</a>
	  	<h3 class="panel-title">Customers Without Any Order in the Last <?= $_REQUEST['days'] ?> Days</h3><br />
	  </div>
	  <div class="panel-body">
	  	<table class="table table-hover table-condensed" id="orderhistory">
	  		<thead>
	  			<tr>
	  				<th>Account Number</th>
	  				<th>Name</th>
						<th>Salesman</th>
						<th>Report Date</th>
						<th>Last Order</th>
						<th>Last Login</th>
						<th>Salesman Report</th>
	  			</tr>
	  		</thead>
	    <?php
		/*	
			$query =
		"SELECT
			CUSTOMER.AccountNumber,
			CUSTOMER.Salesman AS Salesman,
			CUSTOMER.Name,
				IF(MAX(ReportDate) > MAX(OrderDate),
					MAX(ReportDate),
					MAX(OrderDate))  AS
			LastActivity,
				DATEDIFF(
					DATE_ADD(NOW(), INTERVAL -4 HOUR),
					IF(MAX(ReportDate) > MAX(OrderDate),
						MAX(ReportDate),
						MAX(OrderDate))) AS
			LastActivityDays,
				MAX(OrderDate) AS
			LastOrderDate,
				DATEDIFF(DATE_ADD(NOW(), INTERVAL -4 HOUR), MAX(OrderDate)) AS
			LastOrderDays,
			LastLogin
		FROM
			ORDER_HEAD,
			CUSTOMER
		LEFT JOIN
			CUSTOMER_ACTIVITY ON(CUSTOMER.AccountNumber = CUSTOMER_ACTIVITY.AccountNumber )
		WHERE
			CUSTOMER.AccountNumber = ORDER_HEAD.AccountNumber
		GROUP BY
			CUSTOMER.AccountNumber
		HAVING
			LastActivity < DATE_ADD(NOW(), INTERVAL - " . $_REQUEST['days'] . " DAY)
		ORDER BY
			CUSTOMER.Salesman, MAX(ORDER_HEAD.OrderDate);
	
	";
	*/
		if($_REQUEST['days'] == "30"){
				$count = 0;
		}elseif($_REQUEST['days'] == "60") {
				$count = 1;
		}else{
				$count = 2;
		}
		
	    $query = "SELECT AccountNumber, LastOrder, count(id) FROM `CUSTOMER_ACTIVITY` GROUP BY AccountNumber, LastOrder HAVING COUNT(id) > $count ORDER BY CUSTOMER_ACTIVITY.Salesman, id ;";
	   	$result = mysqli_query($con, $query);
			$rowCount = mysqli_num_rows($result);
			
	   	while ($row = mysqli_fetch_array($result)) {
				
				
				$query2 = "SELECT * FROM CUSTOMER_ACTIVITY JOIN CUSTOMER USING(AccountNumber) WHERE AccountNumber = '" . $row['AccountNumber'] . "' AND LastOrder = '" . $row['LastOrder'] . "' LIMIT " . ($count) . ",1";
				
				$result2 = mysqli_query($con, $query2);
				$row2 = mysqli_fetch_array($result2);
				$salesman = new Salesman($row2['Salesman']);
				//$reportCount = $row2['ReportCount'];
				//$button = "<input type=\"button\" class=\"btn btn-default\" value=\"None\" />";
				//if($reportCount > 0){
					$button = "<input type=\"button\" onclick=\"loadCustomerSalesmanReport(" . $row2['id'] . ")\" class=\"btn btn-success\" value=\"View\">";	
				//}
	   	?>
	   		<tr>
	   			<td><?= $row['AccountNumber'] ?></td>
	   			<td><?= $row2['Name'] ?></td>
					<td><?= $salesman->ID . '-' . $salesman->Name ?></td>
	   			<td><?= date('m/d/y h:m a', strtotime($row2['ReportDate'])) ?></td>
					<td><?= date('m/d/y h:m a', strtotime($row['LastOrder'])) ?></td>
					<td><?= $row2['LastLogin']!=null?date('m/d/y', strtotime($row2['LastLogin'])):"Never" ?></td>
					<td><?= $button ?></td>
	   		</tr>
	   	<?php
	   	}
	     ?>
	     </table>
	  </div>
	  <div class="panel-footer" style="height: 100px;" >
			Row Count: <?= $rowCount ?>
	  </div>
</div>       
<script type="text/javascript">
</script>

   </body>
   
 </html>