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

$for = $_REQUEST['a'];
$title = "";
$fromDate = "";
$toDate = "";
if($_SESSION['Username'] == "elisamuel40" ||  $_SESSION['Username'] == "jcorrea"){
	$showVoid = true;
}else{
	$showVoid = false;
}
switch ($for) {
	case "date":
		$title = " Warranties - By Date: ";
		$fromDate = $_REQUEST['fromDate'];
		$toDate = $_REQUEST['toDate'];
		break;
}
 ?>
 
 <!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(){
              // $('.combobox').combobox();
       //        $('.grpline').css("width", "100px");
             });
           </script>

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

   <br />
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="container " style="padding-top: 10px; width: 100%;">
	<div class="panel panel-default">
	  <div class="panel-heading form-inline">
	  <a class="btn btn-success" role="button" style="float: right;" href="#" download="PickupReport<?= $title . "(" . date("m.d.Y",strtotime($fromDate)) . " - " . date("m.d.Y",strtotime($toDate)) . ")" ?>.xls" href="#" onclick="return ExcellentExport.excel(this, 'pickuphistorywarranty', 'Warranty Report');" data-toggle="tooltip" data-placement="left" title="Does not work with Internet Explorer">Export To Excel</a>
	  <?php
						if ($_REQUEST['a'] == "pickupID"){
							$click = "window.location = \"pickuphistory.php?a=pickupID&pickupID=\" + $('[name=filter]').val();";
						}else{
							$click = "filter();";
						}
						
						?>
	  	<div style="margin-right: 10px; float: right;">
	  			<div class="input-group">
	  			      <div id="clearButton" style="display: none;" class="input-group-addon" onclick="clearFilter();">
	  			      	<span class="glyphicon glyphicon-remove" aria-hidden="true"></span></div>
	  			      <input type="text" class="form-control" name="filter" placeholder="Pickup ID/Line/SKU/Customer/WarrantyReason" value="" />
	  			      <div class="input-group-addon" onclick="<? $click ?>">
						
	  			      	<span class="glyphicon glyphicon-search" aria-hidden="true"></span></div>
	  			    </div>
	  	</div>
				
				
				
	  		    <h3 class="panel-title">Pickups Report -<?= $title . " - <i>From: " . date("m/d/Y",strtotime($fromDate)) . " To: " . date("m/d/Y",strtotime($toDate)) . "</i>"?></h3><br />
				
				<form method="get" action="" class="hidden-print">
					<label name="sort">Sort By: </label>
	  		    	<select class="form-control" name="sort">
						<option <?= isset($_REQUEST["sort"])&&$_REQUEST["sort"]=="Line, SKU"?"selected":"" ?> value="Line, SKU">Line, SKU</option>
	  		    		<option <?= isset($_REQUEST["sort"])&&$_REQUEST["sort"]=="PICKUP_HEAD.PickupID"?"selected":"" ?> value="PICKUP_HEAD.PickupID">Pickup ID</option>
	  		    		<option <?= isset($_REQUEST["sort"])&&$_REQUEST["sort"]=="WarrantyReason"?"selected":"" ?> value="WarrantyReason">Warranty Reason</option>
	  		    	</select>
	  		    	<select class="form-control" name="direction">
	  		    		<option <?= (isset($_REQUEST['direction']) && $_REQUEST['direction'] == "ASC")?"selected":"" ?> value="ASC">Ascending</option>
	  		    		<option <?= (isset($_REQUEST['direction']) && $_REQUEST['direction'] == "DESC")?"selected":"" ?> value="DESC">Descending</option>
	  		    	</select>
					<input type="hidden" name="filterSelect" value="<?= isset($_REQUEST["filterSelect"])?$_REQUEST["filterSelect"]:"" ?>" />
					<input type="hidden" name="filterExpression" value="<?= isset($_REQUEST["filterExpression"])?$_REQUEST["filterExpression"]:"" ?>" />
	  		    	<input type="hidden" name="a" value="<?= $_REQUEST["a"] ?>" />
	  		    	<input type="hidden" name="fromDate" value="<?= $_REQUEST["fromDate"] ?>" />
	  		    	<input type="hidden" name="toDate" value="<?= $_REQUEST["toDate"] ?>" />
	  		    	<input type="submit" class="form-control" name="" value="Sort" />
	  		    </form>
				
				<form method="get" action="" class="hidden-print">
					<label name="filterSelect">Filter By: </label>
	  		    	<select class="form-control" name="filterSelect">
						<option <?= isset($_REQUEST["filterSelect"])&&$_REQUEST["filterSelect"]=="PICKUP_DETAIL.Line"?"selected":"" ?> value="PICKUP_DETAIL.Line">Line</option>
	  		    		<option <?= isset($_REQUEST["filterSelect"])&&$_REQUEST["filterSelect"]=="WarrantyReason"?"selected":"" ?> value="WarrantyReason">Warranty Reason (DEF, LIQ, PRE, TRA)</option>
	  		    	</select>
					<label name="filterExpression">Expression</label>
					<input type="text" class="form-control" name="filterExpression" value="<?= isset($_REQUEST["filterExpression"])?$_REQUEST["filterExpression"]:"" ?>">
					<input type="hidden" name="direction" value="<?= isset($_REQUEST["direction"])?$_REQUEST["direction"]:"" ?>" />
					<input type="hidden" name="sort" value="<?= isset($_REQUEST["sort"])?$_REQUEST["sort"]:"" ?>" />
	  		    	<input type="hidden" name="a" value="<?= $_REQUEST["a"] ?>" />
	  		    	<input type="hidden" name="fromDate" value="<?= $_REQUEST["fromDate"] ?>" />
	  		    	<input type="hidden" name="toDate" value="<?= $_REQUEST["toDate"] ?>" />
	  		    	<input type="submit" class="form-control" name="" value="Filter" />
	  		    </form>
				
	  </div>
	  <div class="panel-body">
	  	<table class="table table-hover table-condensed" id="pickuphistorywarranty">
	  		<thead>
	  			<tr>
	  				<th>Pickup ID</th>
					<th>Line</th>
	  				<th>SKU</th>
	  				<th>Qty</th>
					<th>Pickup Date/Time</th>
					<th>Customer Number</th>
					<th>Order Date/Time</th>
					<th>Elapsed Time</th>
					<th>Reason</th>
					<th>Warranty Reason</th>
					<th>Result</th>
	  			</tr>
	  		</thead>
	    <?php
		
		if (isset($_REQUEST['sort']) && $_REQUEST['sort'] != "") {
	    	$sort = $_REQUEST['sort'];
	    	$direction = $_REQUEST['direction'];
	    }else {
	    	$sort = "Line, SKU";
	    	$direction = "ASC";
	    }
		
		if (isset($_REQUEST['filterSelect']) && $_REQUEST['filterExpression'] != "") {
	    	$filterQuery = "AND " . $_REQUEST['filterSelect'] . " LIKE '" . $_REQUEST['filterExpression'] . "'";
	    }else {
	    	$filterQuery = "";
	    }
		//$query = "SELECT * FROM PICKUP_HEAD, CUSTOMER WHERE DATE(PickupDate) BETWEEN '$fromDate' AND '$toDate' AND CUSTOMER.AccountNumber = PICKUP_HEAD.AccountNumber ORDER BY $sort $direction";
		$query = "SELECT PICKUP_HEAD.*, PICKUP_DETAIL.*, ORDER_HEAD.OrderDate, DATEDIFF(PickupDate,ORDER_HEAD.OrderDate) as Elapsed FROM PICKUP_HEAD JOIN PICKUP_DETAIL USING(PickupID)  LEFT JOIN ORDER_HEAD ON(ORDER_HEAD.`OrderID` = PICKUP_DETAIL.`OrderID`) WHERE DATE(PickupDate) BETWEEN '$fromDate' AND '$toDate' $filterQuery AND Reason = 'GAR' AND Void = 0 ORDER BY $sort $direction";
		//echo $query;
	   	$result = mysqli_query($con, $query);
	   	?>
	   	
	   	<script type="text/javascript">
	   	
	   	</script>
	   	<?php
	   	$index = 0;
	   	while ($row = mysqli_fetch_array($result)) {
			$pickup = new Pickup($row['PickupID']);
	   		$key = md5($row['PickupID'] . $config["SaltKey"]);
	   		$key = substr($key, -16);
			$statusResult = $row['Approved'];
			
			if($statusResult == "1"){
				$statusResult = "Approved";
			}elseif($statusResult == "0"){
				$statusResult = "Denied";
			}else{
				$statusResult = "Check";
			}
	   		?>
	   		<tr>
	   			<td><button class="btn btn-primary pickupBtn" onclick="window.open('../pickupRequest.php?pickup=<?= $row['PickupID'] ?>&key=<?= $key ?>')"><?= $row['PickupID'] ?></button></td>
	   			<td><?= $row['Line'] ?></td>
				<td><?= $row['SKU'] ?></td>
				<td><?= $row['Qty'] ?></td>
				
	   			<td><?= date("m/d/Y g:i a",strtotime($row['PickupDate'])) ?></td>
				<td><?= $row['AccountNumber'] ?></td>
				<td><?= $row['OrderDate']?date("m/d/Y h:i a", strtotime($row['OrderDate'])):"" ?></td>
				<td><?= $row['Elapsed'] ?></td>
				<td><?= getReasonText($row['Reason']) ?></td>
				<td><?= getWarrantyReasonText($row['WarrantyReason']) ?></td>
				<td><?= ($statusResult) ?></td>
				
	   		</tr>
	   		
	   	<?php
	   	$index++;
	   	}
	     ?>
	     </table>
	  </div>
	  <div class="panel-footer" >
	  	
	  	<span  style="float: right; text-align: right;">
	  		
	  	</span>	
	  </div>
	</div>

</div>  

<script type="text/javascript">
	
	
	function pad(num, size) {
	    var s = num+"";
	    while (s.length < size) s = "0" + s;
	    return s;
	}
</script>


   
<script type="text/javascript">

$('[name=filter]').on('keypress', function(e) {
	if (e.which == 13) {
		
		<?= $click ?>
	}

});
var hoverID = "";
$('.pickupBtn').hover(
	function() {
		hoverID = $(this).text();
		$(this).text('View');
		$(this).removeClass('btn-primary');
		$(this).addClass('btn-success');
	},
	function() {
		$(this).text(hoverID);
		$(this).removeClass('btn-success');
		$(this).addClass('btn-primary');
});
function clearFilter() {
	$('[name=filter]').val("");
	$('#clearButton').css('display', 'none');
	$('tr').css('display', '');
	
}
function filter() {
	var	name = $('[name=filter]').val();
	filterByName(name);
}
function filterByName(name) {
	$('#clearButton').css('display', '');
	var columns = $('th').length;
	
	for (var i = 0; i < $('td').length; i=i+columns) {
	
		var text = $($('td')[i]).text() + " " + $($('td')[i+1]).text() + " " + $($('td')[i+2]).text() + " " + $($('td')[i+5]).text() + " " + $($('td')[i+9]).text();
		
		var result = text.toUpperCase().indexOf(name.toUpperCase());
		if (result < 0) {
			$($('tr')[i/columns+1]).css('display', 'none');
		}else {
			$($('tr')[i/columns+1]).css('display', '');
		}
		
	}
}

</script>

   </body>
   
 </html>