<?php require_once('header.php'); ?>

<?php if($allPermissions['tracking']['readShipment']=="1"){ ?>


	<script>
	$(document).ready(function() {
		$('#export_excel').click(function() {
			var arr_len = array.length;
			if(arr_len != 0) {
				var url = 'excel/excel-all-shipment-tracking.php';
				var form = $('<form action="' + url + '" method="post">' + '<input type="text" name="awb_number" value="' + array + '" />' + '</form>');
				$('body').append(form);
				form.submit();
			} else {
				alert('No AWB Number is Selected');
			}
		});
	});
	</script>
	<script>
	$(document).ready(function() {
		// Initialize select2
		$("#destination").select2();
	});
	</script>
	<script>
	const array = [];
	var count = 0;
	$(document).ready(function() {
		/***********    For fetching customer address and phone number ***********/
		$('#awb').keyup(function() {
			process();
		});
	});

	function setText(element) {
		var value = $(element).text();
		var userid = $(element).val();
		$("#awb").val(value);
		process();
		$("#searchResult").empty();
	}

	function process() {
		var cust_id = $('#awb').val();
		var awb_id = $('#i').val();
		var bulk_type=$('#bulk_type').val();
// 		alert(bulk_type);
		if(cust_id != ""){
			$.ajax({
				type:'POST',
				url:'bulk_track_awb.php',
				data: {
					id: cust_id,
					type:bulk_type
				    },
				dataType: 'json',
				success: function(response) {
					$('#f').text('');
					$('#s').text('');
					if(response[0]['id'] == 0) {
						$('#s').text(response[0]['status']);
						$('#f').text('');
						$("#searchResult").empty();
					}
					if(response[0]['status'] == 2) {
						var len = response.length;
						$("#searchResult").empty();
				// 		alert(bulk_type);
						for(var i = 0; i < len; i++) 
						{
							var id = response[i]['id'];
							var awb_number = response[i][bulk_type];
							$("#searchResult").append("<li value='" + awb_number + "'>" + awb_number + "</li>");
						}
						$("#searchResult li").bind("click", function() {
							setText(this);
						});
					}
					if(response[0]['status'] == "Success") {
						if(array.includes(response[0]['id'])) {
							$('#f').text('AWB Number Already Added');
							$('#s').text('');
							$("#searchResult").empty();
						} else {
							$('#awb').val('');
							if(awb_id == "") {
								var sd = response[0]['id'];
							} else {
								var sd = awb_id + ',' + response[0]['id'];
							}
							$('#s').text(response[0]['status']);
							var k = response[0]['id'];
							array.push(k);
							count++;
							$('#myTable').append('<tr id=' + count + '><td><button class="btn btn-sm btn-danger" onclick="remove(' + k + ',' + count + ')">X</button></td><?php if($clientType=="Admin"){ ?><td>' + response[0]['3pl_awb'] + '</td><?php } ?><td>' + response[0]['cscs_awb'] + '</td><td>' + response[0]['ref_no'] + '</td><td>' + response[0]['pics'] + '</td><td>' + response[0]['address'] + '</td><td>' + response[0]['phone'] + '</td><td>' + response[0]['c_weight'] + '</td><td>' + response[0]['client_id'] + '</td><td>' + response[0]['created'] + '</td><td>' + response[0]['tstatus'] + '</td><td>' + response[0]['tracking_location'] + '</td><td>' + response[0]['tracking_date'] + '</td></tr>');
							$("#searchResult").empty();
							/*$('#manifest').append('<tr><td>Its Workig</td></tr>');*/
						}
					}
				}
			});
		}
	}
	</script>
	<script>
	function remove(k, count) {
		var len = array.length;
		len = len - 1;
		for(var i = len; i >= 0; i--) {
			if(array[i] == k) {
				var i_val = i;
				array.splice(i_val, 1);
				$('table#my_data tr#' + count).remove();
				break;
			}
		}
	}
	</script>
	<section class="content-header">
		<div class="content-header-left">
			<h2>View Shipment Tracking</h2> </div>
	</section>
	<br>
	<section>
		<center>
			<h4 style="blue">Single Shipment Tracking</h4></center>
		<form action="single-tracking.php" method="POST">
			<div class="row">
				<div class="col-md-2"></div>
				<div class="col-md-6">
				    <div class="form-group">
				        <div class="col-md-4">
				    <select id="type" class='form-control' name="type">
				        <?php if($clientType=="Admin"){ ?><option value='3pl_awb' selected>3PL AWB</option><?php } ?>
				        <option value='cscs_awb'>AWB</option>
				        <option value='ref_no'>Ref No</option>
				    </select>  
				    </div>
				    <div class="col-md-6">
	        			    <input type="text" name="track_id" class="form-control" placeholder="Enter AWB Number">     
				        </div>
				        <div class="col-md-2">
				            <center>
						<input type="submit" class="btn btn-success" value="Search">
					</center>
				</div>
				    </div>
				</div>
				<div class="col-md-2"></div>
			</div>
			<br>
			<br>
			<br> </form>
	</section>
	<section>
		<center>
			<h4 style="blue">Bulk Shipment Tracking and Export Data</h4></center>
		<br>
		<br>
		<div class="row">
		    <div class="col-md-2">
		        </div>
			<div class="col-md-3">
			    <select id="bulk_type" class='form-control' name="bulk_type">
				      <?php if($clientType=="Admin"){ ?>  <option value='3pl_awb' selected>3PL AWB</option><?php } ?>
				        <option value='cscs_awb'>AWB</option>
				        <option value='ref_no'>Ref No</option>
				    </select>  
			</div>
			<div class="col-md-4">
				<input placeholder="Enter AWB Number or Scan AWB Number" type="text" class="form-control" name="awb" id="awb">
				<br>
				<ul id="searchResult"></ul>
				<center><span style="color:green;" id="s"></span><span style="color:red;" id="f"></span></center>
				<input type="hidden" class="form-control" name="i" id="i"> </div>
			<div class="col-md-3"> </div>
		</div>
	</section>
	<section class="content">
		<div class="row">
			<div class="col-md-12">
				<div class="box box-info">
					<div class="box-body table-responsive">
						<table id="my_data" class="table table-bordered table-striped">
							<thead>
								<tr>
									<th>Action</th>
									<?php if($clientType=="Admin"){ ?><th>3PL AWB </th><?php } ?>
									<th>CSCS AWB </th>
									<th>Ref Number</th>
									<th>Quantity</th>
									<th>Address</th>
									<th>Mobile</th>
									<th>Weight</th>
									<th>Client Name</th>
									<th>Entry Date</th>
									<th>Status</th>
									<th>Current Location</th>
									<th>Date</th>
								</tr>
							</thead>
							<tbody id="myTable"> </tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
		<div class="content-footer-right" style="margin-left:45%">
			<button id='export_excel' class="btn btn-danger btn-sm">Export Excel</button>
		</div>
	</section>
	<?php }else{ ?>
<center><h4 style="color:red;">You don't have permission to view this page </h4></center>
<?php } ?>

	<?php require_once('footer.php'); ?>