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

$for = $_REQUEST['a'];
$title = "";
$fromDate = "";
$toDate = "";
if($_SESSION['Username'] == "elisamuel40" || $_SESSION['Username'] == "wvalle" || $_SESSION['Username'] == "jcorrea" || $_SESSION['Username'] == "motero" || $_SESSION['Username'] == "arivera" || $_SESSION['Username'] == "rlopez"){
	$showVoid = true;
}else{
	$showVoid = false;
}
switch ($for) {
	case "customer":
		$customer = new Customer($_REQUEST["customer"]);
		$title = " - By Customer: " . $customer->Name;
		$fromDate = isset($_REQUEST['fromDate'])?$_REQUEST['fromDate']:"";
		$toDate = isset($_REQUEST['toDate'])?$_REQUEST['toDate']:"";
		break;
	case "salesman":
		$salesman = new Salesman($_REQUEST['salesman']);
		$title = " - By Salesman: " . $salesman->Name;
		$fromDate = $_REQUEST['fromDate'];
		$toDate = $_REQUEST['toDate'];
		break;
	case "date":
		$title = " - By Date: ";
		$fromDate = $_REQUEST['fromDate'];
		$toDate = $_REQUEST['toDate'];
		break;
	case "pickupID":
		$title = " - By PickupID: ";
		$pickupID = $_REQUEST['pickupID'];
		$fromDate = 'All';
		$toDate = 'All';
		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="PickupHistory<?= $title . "(" . date("m.d.Y",strtotime($fromDate)) . " - " . date("m.d.Y",strtotime($toDate)) . ")" ?>.xls" href="#" onclick="return ExcellentExport.excel(this, 'pickuphistory', 'Pickup History');" 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/Name/Ticket" value="" />
	  			      <div class="input-group-addon" onclick="<? $click ?>">
						
	  			      	<span class="glyphicon glyphicon-search" aria-hidden="true"></span></div>
	  			    </div>
	  	</div>
				<?php if($for == "pickupID"){ ?>
				<h3 class="panel-title">Pickup History<?= $title . " - <i>$pickupID</i>"?></h3><br />
				<?php } else { ?>
	  		    <h3 class="panel-title">Pickup History<?= $title . " - <i>From: " . date("m/d/Y",strtotime($fromDate)) . " To: " . date("m/d/Y",strtotime($toDate)) . "</i>"?></h3><br />
				<?php } ?>
				<form method="get" action="" class="hidden-print">
	  		    	<select class="form-control" name="sort">
	  		    		<option value="PickupID">Pickup ID</option>
	  		    		<option value="Name">Customer Name</option>
	  		    		<option value="SalesmanID">Salesman</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="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>
				
	  </div>
	  <div class="panel-body">
	  	<table class="table table-hover table-condensed" id="pickuphistory">
	  		<thead>
	  			<tr>
	  				<th>Pickup ID</th>
	  				<?= ($for != "customer") ? "<th>Customer</th>" : "" ?>
	  				<th>Pickup Date/Time</th>
	  				<th>Salesman</th>
	  				<th style="text-align: center;">Packages</th>
					<th style="text-align: center;">Via</th>
	  				<th style="text-align: center;">A Recoger</th>
	  				<th style="text-align: center;">@ Warehouse</th>
	  				<th style="text-align: center;">Status</th>
					<?php
						if($showVoid){
					?>
	  				<th style="text-align: center;">Void</th>
					<?php
						}
					?>
	  			</tr>
	  			
	  		</thead>
	    <?php
		
		if (isset($_REQUEST['sort'])) {
	    	if ($_REQUEST['sort'] != "") {
	    		$sort = $_REQUEST['sort'];
	    		$direction = $_REQUEST['direction'];
	    	}
	    }else {
	    	$sort = "PickupID";
	    	$direction = "DESC";
	    }
		
	    if ($for == "customer") {
	    	$query = "SELECT * FROM PICKUP_HEAD, CUSTOMER WHERE PICKUP_HEAD.AccountNumber LIKE '" . $customer->AccountNumber . "' AND DATE(PickupDate) BETWEEN '$fromDate' AND '$toDate' AND CUSTOMER.AccountNumber = PICKUP_HEAD.AccountNumber ORDER BY $sort $direction";	
	    }elseif($for == "salesman") {
	    	$query = "SELECT * FROM PICKUP_HEAD, CUSTOMER WHERE PICKUP_HEAD.Salesman LIKE '" . $salesman->ID . "' AND DATE(PickupDate) BETWEEN '$fromDate' AND '$toDate' AND CUSTOMER.AccountNumber = PICKUP_HEAD.AccountNumber ORDER BY $sort $direction";
	    }elseif ($for == "date") {
	    	$query = "SELECT * FROM PICKUP_HEAD, CUSTOMER WHERE DATE(PickupDate) BETWEEN '$fromDate' AND '$toDate' AND CUSTOMER.AccountNumber = PICKUP_HEAD.AccountNumber ORDER BY $sort $direction";
	    	
	    }elseif ($for == "pickupID") {
			$query = "SELECT * FROM PICKUP_HEAD, CUSTOMER WHERE PickupID = '$pickupID' AND CUSTOMER.AccountNumber = PICKUP_HEAD.AccountNumber";
		}

	   	$result = mysqli_query($con, $query);
	   	?>
	   	
	   	<script type="text/javascript">
	   		var items = new Array();
	   	</script>
	   	
	   	<?php
	   	$index = 0;
	   	while ($row = mysqli_fetch_array($result)) {
			$pickup = new Pickup($row['PickupID']);
	   		$key = md5($row['PickupID'] . "EJAS");
	   		$key = substr($key, -16);
	   		?>
	   		
	   		<script type="text/javascript">
	   			var item = new Object();
	   			item.ID = "<?= $row['PickupID'] ?>";
	   			item.Name = "<?= $row['Name'] ?>";
	   			item.Date = "<?= date("m/d/Y g:i a",strtotime($row['PickupDate']))  ?>";
	   			item.Ticket = "<?= $row['PickupTicket'] ?>";
	   			item.ToPickup = "<?= $row['ToPickup'] ?>";
	   			item.AtWarehouse = "<?= $row['AtWarehouse'] ?>";
	   			item.Status = "<?= $row['PickupStatus'] ?>";
				item.Key = "<?= $key ?>";
	   			items.push(item);
	   		</script>
	   		<tr>
	   			<td><button class="btn btn-primary pickupBtn" onclick="window.open('../pickupRequest.php?pickup=<?= $row['PickupID'] ?>&key=<?= $key ?>')"><?= $row['PickupID'] ?></button></td>
	   			<?= ($for != "customer") ? "<td id=\"" . $row['PickupID'] . "-Customer\">" . $row['Name'] . "</td>" : "" ?>
	   			<td><?= date("m/d/Y g:i a",strtotime($row['PickupDate'])) ?></td>
				<td id="<?= $row['PickupID'] ?>-Salesman"><?= $pickup->Salesman->ID . " - " . $pickup->Salesman->Name ?></td>	   			
				<td style="text-align: center;"><?= $row['Packages'] ?></td>
				<td style="text-align: center;"><?= $row['Via'] ?></td>
				<td style="text-align: center;"><button onclick="<?= ($row['ToPickup'] == "0000-00-00 00:00:00")?"setPickupStatus":"viewPickupStatusModal" ?>('pickup', <?= $index ?>)" class="btn btn-default"><?= ($row['ToPickup'] == "0000-00-00 00:00:00")?"Enviar":date("m/d/Y",strtotime($row['ToPickup'])) ?></button></td>
				<td style="text-align: center;"><button data-key="<?= $key ?>" <?= ($row['ToPickup'] == "0000-00-00 00:00:00")?"disabled":"" ?> onclick="<?= ($row['AtWarehouse'] == "0000-00-00 00:00:00")?"setPickupStatus":"viewPickupStatusModal" ?>('warehouse', <?= $index ?>)" class="btn btn-default"><?= ($row['AtWarehouse'] == "0000-00-00 00:00:00")?"Recibir":date("m/d/Y",strtotime($row['AtWarehouse'])) ?></button></td>
				<td style="text-align: center;"><button <?= ($row['AtWarehouse'] == "0000-00-00 00:00:00")?"disabled":"" ?> onclick="setPickupStatus('status', <?= $index ?>)" class="btn btn-default"><?= ($row['PickupStatus'] == "")?"Finalizar":$row['PickupStatus'] ?></button></td>
				<?php
					if($showVoid){
				?>
				<td style="text-align: right;"><a href="#" class="btn btn-<?= $row['Void']?"primary":"danger" ?>" onclick="<?= $row['Void']?"voidDetails":"promptVoid" ?>('<?= $row['PickupID'] ?>')"><?= $row['Void']?"Void Details":"Void" ?></a></td>
				<?php
					}
				?>
	   		</tr>
	   		
	   	<?php
	   	$index++;
	   	}
	     ?>
	     </table>
	  </div>
	  <div class="panel-footer" >
	  	
	  	<span  style="float: right; text-align: right;">
	  		
	  	</span>	
	  </div>
	</div>

</div>  

<!-- Set PickupStatus Modal -->
<div class="modal fade" id="setPickupStatus" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm" style="width: 500px;">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="setPickupStatusTitle">Mark as Sent for Pickup</h4>
      </div>
      <div class="modal-body">
      <span id="setPickupStatusSubtitle"></span>
        <form>
        <div class="form-group" id="setPickupStatusDateDiv">
        	<label for="setPickupStatusDate">Date:</label>
        		<input class="form-control" type="text" name="setPickupStatusDate" id="setPickupStatusDate" value="" />
        	
        </div>  
        
        <div class="form-group" id="setPickupStatusStatusDiv" style="display: none;">
        	<div id="AtWarehouseNotesDiv">
        		<label for="AtWarehouseNotes">At Warehouse Notes:</label>
        		<span id="AtWarehouseNotes"></span>
        		
        	</div>
        	<label for="setPickupStatusStatus">Status:</label>
    		<select class="form-control" id="setPickupStatusStatus" name="setPickupStatusStatus">
    			<option>Aceptado</option>
    			<option>Aceptado Parcial</option>	
    			<option>Denegado</option>
				<option>Dismissed</option>
				<option>Waiting</option>
    		</select>
    		
    		
        </div>  
        <div id="setPickupStatusNotesDiv">
        	<label for="setPickupStatusNotes">Notes:</label>
        	<textarea id="setPickupStatusNotes" class="form-control"></textarea>
        </div>
        <input type="hidden" name="setPickupStatusAction" id="setPickupStatusAction" value="" />      
        <input type="hidden" name="setPickupStatusID" id="setPickupStatusID" value="" />      
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <button type="button" id="modalSavePickupStatus" class="btn btn-primary">Save</button>
      </div>
    </div>
  </div>
</div>
<link rel="stylesheet" href="../css/jquery.filthypillow.css" type="text/css" />
<script src="../js/jquery.filthypillow.min.js" type="text/javascript"></script>
<script src="../js/moment.js" type="text/javascript">
</script>
<script type="text/javascript">
	var fp = $( "#setPickupStatusDate" ),
	    now = moment().subtract(1,"seconds");
		
	fp.filthypillow( { 
	  maxDateTime: function( ) {
	    return moment().add(2,'days');
	  } ,
	  initialDateTime: function( ) {
	    return moment();
	  }
	} );
	fp.on( "focus", function( ) {
	  fp.filthypillow( "show" );
	} );
	fp.on( "fp:save", function( e, dateObj ) {
	  fp.val( dateObj.format( "MM/DD/YYYY hh:mm A" ) );
	  fp.filthypillow( "hide" );
	} );
	
	function setPickupStatus(action, lineID) {
		clearPickupStatusModal();
		$('#setPickupStatusAction').val(action);
		$('#setPickupStatusID').val(items[lineID].ID);
		$('#setPickupStatusSubtitle').html("<b>Pickup ID:</b> " + items[lineID].ID + "<br /><b>Customer:</b> " + items[lineID].Name + "<br />" + "<b>Pickup Ticket:</b> " + items[lineID].Ticket);
		
		if (action == "pickup") {
			$('#setPickupStatusTitle').text('Mark as Sent for Pickup');
			$('#setPickupStatusDate').val('');
			$('#setPickupStatusNotesDiv').hide();
			$('#AtWarehouseNotesDiv').hide();
			$('#setPickupStatus').modal('toggle')
		}else if (action == "warehouse") {
			window.open('../pickupRequest.php?pickup=' + items[lineID].ID + '&key=' + items[lineID].Key + '&print=true');
			var nowDate = moment().subtract(1,"seconds");
			$('#setPickupStatusDate').val(nowDate.format( "MM/DD/YYYY hh:mm A" ));	
			
			$('#setPickupStatusTitle').text('Mark as Received at Warehouse');
			
			$('#AtWarehouseNotesDiv').show();
			$('#setPickupStatusNotesDiv').show();
			$('#setPickupStatus').modal('toggle')
		}else if (action == "status") {
			if(false){
				return true;
			}else{
				$.get('functions.php?action=getPickupStatusInfo&a=warehouse&pickupID=' + items[lineID].ID,function(data) {
					data = JSON.parse(data);
					$('#AtWarehouseNotes').text(data.AtWarehouseNotes);
					$('#setPickupStatus').modal('toggle')
				});
				$.get('functions.php?action=getPickupStatusInfo&a=status&pickupID=' + items[lineID].ID,function(data) {
					data = JSON.parse(data);
					console.log(data);
					if (data) {
						var list = $('#setPickupStatusStatus option');
						for (var i = 0; i < list.length; i++) {
							if ($(list[i]).text() == data.PickupStatus) {
								$(list[i]).prop('selected', true);
							}
							
						}
						$('#setPickupStatusNotes').text(data.PickupStatusNotes);
					}
				});
				$('#setPickupStatusStatus').attr('readonly', true);
				$('#setPickupStatusNotes').attr('readonly', true);
				$('#setPickupStatusDateDiv').hide();
				$('#setPickupStatusStatusDiv').show();
				$('#setPickupStatusTitle').text('Set Final Status');
				$('#setPickupStatusDate').val('');	
				$('#AtWarehouseNotesDiv').show();
				$('#setPickupStatusNotesDiv').show();
				$('#modalSavePickupStatus').hide();
			}
			
		}
		
	}
	function clearPickupStatusModal() {
		$('#setPickupStatusSubtitle').text('');
		$('#setPickupStatusDate').val('');
		$('#setPickupStatusAction').val('');
		$('#setPickupStatusDateDiv').show();
		$('#setPickupStatusStatusDiv').hide();
		$('#setPickupStatusNotesDiv').hide();
		$('#AtWarehouseNotesDiv').hide();
		
	}
	function savePickupStatus(action, pickupID) {
		if (action == "pickup") {
			var datetime = $('#setPickupStatusDate').val();
			if (datetime == "") {
				alert("Error you must choose a date and time");
				return;
			}else {
				var date = fp.filthypillow('getDate')._d;
				console.log(date);
				date = date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + pad(date.getHours(),2) + ":" + pad(date.getMinutes(),2) + ":00";
				$.get('functions.php?action=setPickupStatus&a=' + action + "&pickupID=" + pickupID + "&value=" + encodeURIComponent(date), function(data) {
					data = JSON.parse(data);
					if (data.result) {
						location.reload();
					}else {
						alert("There has been an error \nMySQL Error: " + data.error);
					}
				
				});
			}
		}else if (action == "warehouse") {
			var datetime = $('#setPickupStatusDate').val();
			if (datetime == "") {
				alert("Error you must choose a date and time");
				return;
			}else {
				var date = fp.filthypillow('getDate')._d;
				console.log(date);
				date = date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + pad(date.getHours(),2) + ":" + pad(date.getMinutes(),2) + ":00";
				
				$.get('functions.php?action=setPickupStatus&a=' + action + "&pickupID=" + pickupID + "&value=" + encodeURIComponent(date) + "&notes=" + encodeURIComponent( $('#setPickupStatusNotes').val() ), function(data) {
					data = JSON.parse(data);
					if (data.result) {
						location.reload();
					}else {
						alert("There has been an error \nMySQL Error: " + data.error);
					}
				
				});
			}
		}else if (action == "status") {
			var status = $('#setPickupStatusStatus').val();
				$.get('functions.php?action=setPickupStatus&a=' + action + "&pickupID=" + pickupID + "&value=" + encodeURIComponent(status) + "&notes=" + encodeURIComponent($('#setPickupStatusNotes').val()), function(data) {
				data = JSON.parse(data);
				if (data.result) {
					location.reload();
				}else {
					alert("There has been an error \nMySQL Error: " + data.error);
				}
			});
		}
	}
	$('#modalSavePickupStatus').on('click', function () {
		var action = $('#setPickupStatusAction').val();
		var pickupID = $('#setPickupStatusID').val();
		savePickupStatus(action, pickupID);
	});
	function pad(num, size) {
	    var s = num+"";
	    while (s.length < size) s = "0" + s;
	    return s;
	}
</script>
<!-- Set PickupStatus Modal End -->     
<!-- View PickupStatusDetails Modal -->
<div class="modal fade" id="viewPickupStatus" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm" style="width: 500px;">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="viewPickupStatusTitle">Sent for Pickup</h4>
      </div>
      <div class="modal-body">
      <span id="viewPickupStatusSubtitle"></span>
       <form class="form">
       	
       	<textarea id="viewPickupStatusNotes" class="form-control"></textarea>

       	
       </form> 
      </div>
      <div class="modal-footer">
      	<button type="button" id="viewPickupStatusSave" class="btn btn-primary">Save</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<!-- Void Pickup Modal -->

<div class="modal fade" id="VoidPickupModal">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="VoidPickupTitle">Void Pickup</h4>
	      </div>
	      <div class="modal-body">
			<div class="form">
				<input type="hidden" name="VoidPickupID" id="VoidPickupID" value="" />
				<div class="form-group">
					<label for="VoidPickupName">Customer</label>
					<input type="text" class="form-control" readonly name="VoidPickupName" id="VoidPickupName" value="" />
				</div>
				<div class="form-group">
					<label for="VoidPickupSalesman">Salesman</label>
					<input type="text" class="form-control" readonly name="VoidPickupSalesman" id="VoidPickupSalesman" value="" />
				</div>
				<div class="form-group">
					<label for="VoidPickupReason">Reason</label>
					<textarea class="form-control" rows="5" id="VoidPickupReason"></textarea>
				</div>
			</div>
			
	      </div>
	      <div class="modal-footer">
			<small>WARNING: THIS CANNOT BE UNDONE!</small>
	        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
	        <button type="button" class="btn btn-success" onclick="voidPickup()">Submit Void</button>
	      </div>
	    </div><!-- /.modal-content -->
	  </div><!-- /.modal-dialog -->
	</div><!-- /.modal -->
	
	<!-- End Void Pickup Modal -->
	<!-- Void Details Modal -->
	
	<div class="modal fade" id="VoidDetailsModal">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="VoidPickupTitle">Void Details</h4>
	      </div>
	      <div class="modal-body">
			<div class="form">
				
				<div class="form-group">
					<label for="VoidByUser">Username</label>
					<input type="text" class="form-control" readonly name="VoidByUser" id="VoidByUser" value="" />
				</div>
				<div class="form-group">
					<label for="VoidDate">Date</label>
					<input type="text" class="form-control" readonly name="VoidDate" id="VoidDate" value="" />
				</div>
				<div class="form-group">
					<label for="VoidReason">Reason</label>
					<textarea class="form-control" readonly rows="5" id="VoidReason"></textarea>
				</div>
			</div>

<!-- End Void Details -->


<script type="text/javascript">
	function clearViewPickupStatusModal() {
		$('#viewPickupStatusTitle').text('');
		$('#setPickupStatusSubtitle').text('');	
		$('#viewPickupStatusNotes').val('');
		$('#viewPickupStatusSave').show('');
		$('#viewPickupStatusSave').unbind('');
	}
	function viewPickupStatusModal(action, lineID) {
		clearViewPickupStatusModal();
		if (action == "pickup") {
			$('#viewPickupStatusTitle').text("Sent to Pickup");	
			$('#viewPickupStatusSave').hide();
			$('#viewPickupStatusSubtitle').html("<b>Pickup ID:</b> " + items[lineID].ID + "<br /><b>Customer:</b> " + items[lineID].Name + "<br />" + "<b>Pickup Ticket:</b> " + items[lineID].Ticket);
			$.get('functions.php?action=getPickupStatusInfo&a=' + action + '&pickupID=' + items[lineID].ID,function(data) {
				data = JSON.parse(data);
				$('#viewPickupStatusSubtitle').append("<br /><b>Username:</b> " + data.ToPickupUser + "<br />\n<b>Name:</b> " + data.FullName + "<br /><b>Date: </b>" + data.ToPickup);
				$('#viewPickupStatus').modal('toggle');
			});
		}else if (action == "warehouse") {
			$('#viewPickupStatusTitle').text("Received at Warehouse");	
			console.log(items[lineID]);
			$('#viewPickupStatusSubtitle').html("<b>Pickup ID:</b> " + items[lineID].ID + "<br /><b>Customer:</b> " + items[lineID].Name + "<br />" + "<b>Pickup Ticket:</b> " + items[lineID].Ticket);
			$.get('functions.php?action=getPickupStatusInfo&a=' + action + '&pickupID=' + items[lineID].ID,function(data) {
				data = JSON.parse(data);
				$('#viewPickupStatusSubtitle').append("<br /><b>Username:</b> " + data.AtWarehouseUser + "<br />\n<b>Name:</b> " + data.FullName + "<br /><b>Date: </b>" + data.AtWarehouse + "<br /><b>Notes: </b>");
				$('#viewPickupStatusNotes').val(data.AtWarehouseNotes);
				$('#viewPickupStatusSave').on("click",function(e) {
					$.get('functions.php?action=savePickupStatusNotes&a=' + action + '&pickupID=' + items[lineID].ID + '&notes=' + encodeURIComponent($('#viewPickupStatusNotes').val()),function(data) {
						data = JSON.parse(data);
						$('#viewPickupStatus').modal('toggle');
					});		
				});
				$('#viewPickupStatus').modal('toggle');
			});
		}else if (action == "status") {
			$('#viewPickupStatusTitle').text("Finished Pickup Process");	
			$('#viewPickupStatusSubtitle').html("<b>Pickup ID:</b> " + items[lineID].ID + "<br /><b>Customer:</b> " + items[lineID].Name + "<br />" + "<b>Pickup Ticket:</b> " + items[lineID].Ticket);
			$.get('functions.php?action=getPickupStatusInfo&a=' + action + '&pickupID=' + items[lineID].ID,function(data) {
				data = JSON.parse(data);
				$('#viewPickupStatusSubtitle').append("<br /><b>Username:</b> " + data.PickupStatusUser + "<br />\n<b>Name:</b> " + data.FullName + "<br /><b>Status: </b>" + data.PickupStatus + "<br /><b>At Warehouse Notes:</b> " + data.AtWarehouseNotes + "<br /><b>Notes: </b>");
				$('#viewPickupStatusNotes').append(data.PickupStatusNotes);
				$('#viewPickupStatus').modal('toggle');
			});
		}
	}
	
	
</script>
<!-- View PickupStatusDetails Modal End -->   
<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+3]).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', '');
		}
		
	}
}
function promptVoid(pickupID){
	//clearVoidPickupModal();
	$('#VoidPickupID').val(pickupID);
	$('#VoidPickupName').val($('#' + pickupID + '-Customer').text());
	$('#VoidPickupSalesman').val($('#' + pickupID + '-Salesman').text());
	$('#VoidPickupModal').modal('show');
}
function clearVoidPickupModal() {
	$('#VoidPickupID').val('');
	$('#VoidPickupName').val('');
	$('#VoidPickupAmount').val('');
	$('#VoidPickupSalesman').val('');
	$('#VoidPickupReason').val('');
}
function voidPickup() {
	var data = new Object();
	data.pid = $('#VoidPickupID').val();
	data.Reason = $('#VoidPickupReason').val();
	
	$.post("functions.php?&action=voidPickup", data, function(returnData) {
		clearVoidPickupModal();
		console.log(returnData);
		returnData = JSON.parse(returnData);
		$('#VoidPickupModal').modal('hide');
		if(returnData.result == "true"){
			location.reload();
		}
	});
}
function voidDetails(id){
	$.get('functions.php?&action=voidPickupDetails&pid=' + id, function(data){
			clearVoidDetails();
			data = JSON.parse(data);
			$('#VoidByUser').val(data.VoidUser);
			$('#VoidDate').val(data.VoidDate);
			$('#VoidReason').val(data.VoidReason);
			$('#VoidDetailsModal').modal('show');
	});
}
function clearVoidDetails(){
	$('#VoidByUser').val('');
	$('#VoidDate').val('');
	$('#VoidReason').val('');
}

</script>

   </body>
   
 </html>