<?php require_once "header.php"; ?>


<section class="content-header" style="padding: 8px 15px 8px 15px;">
    <div class='box-body' style="margin: 0px 0px;box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;background-color: white;">
    <div class="row" style="padding-left: 10px;padding-right: 10px;">
    			<div class="col-md-6" id="awb-scan">
    			    <div class="search-box">
    			        <label>Scan AWB Number</label>
    				<input placeholder="Scan Awb Number here" type="text" class="form-control" name="awb_scan" id="awb_scan" style="border-radius: 20px;background: cornsilk;"> 
    				<div class="result" style="overflow-y: scroll;max-height: 267px;top: 56%;margin-left: 8%;width: 90%;margin-top:8%;"></div>
    				</div>
    			</div>
    			<div class="col-md-6" style="text-align: right;"> 
        		    
        		    <a href="outgoing-manifest-list.php" class="btn btn-primary btn-sm"><i class="fa fa-arrow-left"></i> Back to Manifest List</a>
        		</div>
    </div>
    <div class="row" style="padding-left: 10px;padding-right: 10px;margin-top:10px;">
    		<div class="col-md-2">
                <label>Origin Branch</label>
            		<select class="form-control select2" name="from_branch" id="from_branch" onchange="StatusUpdateLocation()">
            	        <?php
            	        
            	        $condi='';
            	        
            	        if($session['id']!=1 && $session_branch_ids!='0'){
                            $condi.= " and id IN ($session_branch_ids)";
                        }
                        
                        
            			$statement = $pdo->prepare("SELECT * FROM `tbl_branch` where status='1' $condi");
            			$statement->execute();
            			$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
            			foreach ($result as $row) { ?>
            				<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
            			<?php } ?>
            	    </select>
			</div>
			<div class="col-md-2">
                <label>Destination Branch</label>
            		<select class="form-control select2" name="to_branch" id="to_branch">
            	        <?php
            			$statement = $pdo->prepare("SELECT * FROM `tbl_branch` where status='1'");
            			$statement->execute();
            			$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
            			foreach ($result as $row) { ?>
            				<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
            			<?php } ?>
            	    </select>
			</div>
			<div class="col-md-2">
                <label>Co-Loader Name</label>
            		<select class="form-control select2" name="coloader" id="coloader">
            	        <?php
            			$statement = $pdo->prepare("SELECT * FROM `tbl_coloader` where status='1'");
            			$statement->execute();
            			$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
            			foreach ($result as $row) { ?>
            				<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
            			<?php } ?>
            	    </select>
			</div>
			<div class="col-md-2">
                <label for="required">Driver Name</label>
            		<select class="form-control select2" name="driver_id" id="driver_id" onchange="DriverDetails()" required>
            		    <option value="">Select Driver</option>
            	        <?php
            			$statement = $pdo->prepare("SELECT * FROM `tbl_drivers` where status='1'");
            			$statement->execute();
            			$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
            			foreach ($result as $row) { ?>
            				<option value="<?=$row['name'];?>"
            				data-driver_name="<?=$row['name'];?>"
            				data-driver_mobile_no="<?=$row['mobile_no'];?>"
            				data-vechicle_no="<?=$row['vehicle_no'];?>"
            				><?=$row['name'];?> - <?=$row['mobile_no'];?></option>
            			<?php } ?>
            	    </select>
			</div>
			
			<div class="col-md-2">
                <label for="required">Vehicle No</label>
            	<input type="text" class="form-control" placeholder="Vehicle No" name="vehicle_no" id="vehicle_no" required/>
			</div>
			
			<div class="col-md-2">
                <label for="required">Driver Mobile No</label>
            	<input type="text" class="form-control" placeholder="Driver Mobile No" name="driver_mobile" id="driver_mobile" required/>
			</div>
    </div>
   </div> 
    
    
  <div class='box-body' style="margin: 0px 0px;box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;background-color: white;margin: 10px 0px;">  
    <div class="box-body table-responsive">
						<table id="scaned_table_data" class="table table-bordered table-striped" style="font-size: 12px;">
							<thead>
								<tr>
									<th style="text-align: center;">#</th>
									<th style="min-width:100px;">AWB Number</th>
									<th  style="text-align: center;">No.Of Box</th>
									<th>Date Added</th>
									<th>Status</th>
									<th style="text-align: center;">Weight</th>
									<th>Client Name</th>
									<th>Consignor</th>
									<th>Consignee</th>
									<th>Origin (State=>City->Pincode)</th>
									<th>Destination (State=>City->Pincode)</th>
								</tr>
							</thead>
							<tbody id="scaned_table_data_body">
							    
							</tbody>
						
							
						</table>
	</div>
	</div>
	
	
	
	
	<div class='box-body' style="margin: 0px 0px;box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;background-color: white;">  
    <div class="row" style="padding-left: 10px;padding-right: 10px;">
    		<div class="col-md-2">
				 <label>Status Date</label>
			     <input type="date" class="form-control" name="status_date" id="status_date" value="<?=date("Y-m-d")?>" />
			</div>
			
			
			<div class="col-md-3">
                <label>Status</label>
            		<select class="form-control select2" name="update_status" id="update_status">
            	        <?php
            			$statement = $pdo->prepare("SELECT * FROM `tbl_status` where status='1'");
            			$statement->execute();
            			$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
            			foreach ($result as $row) { ?>
            				<option value="<?=$row['id'];?>" <?php if($row['id']==2){ echo "selected";}?>><?php echo $row['name']; ?></option>
            			<?php } ?>
            	    </select>
			</div>
			<div class="col-md-2">
                <label>Status Update Location</label>
            		<select class="form-control select2" name="status_location" id="status_location">
            	        <?php
            			$statement = $pdo->prepare("SELECT * FROM `tbl_branch` where status='1'");
            			$statement->execute();
            			$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
            			foreach ($result as $row) { ?>
            				<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
            			<?php } ?>
            	    </select>
			</div>
			
			<div class="col-md-3">
			    <label>Status Description</label>
			    <select class="form-control select2" name="status_description" id="status_description">
            	        <?php
            			$statement = $pdo->prepare("SELECT * FROM `tbl_reason` where status='1'");
            			$statement->execute();
            			$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
            			foreach ($result as $row) { ?>
            				<option value="<?php echo $row['reason']; ?>"><?php echo $row['reason']; ?></option>
            			<?php } ?>
            	    </select>
			</div>
			<div class="col-md-1">
			    <label>Action</label><br>
					<button id='btn_submit' class="btn btn-success btn-sm"><i id="update_fa" class="fa fa-paper-plane"></i> Update Status</span> </button>
			</div>
    </div>
 </div> 
    
</section>


<div class="modal fade" id="print_confirm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <center><h4 class="modal-title" id="success_title">Manifest Created Successfully</h4></center>
            </div>
            <div class="modal-body" id="modal-body">
                <center>
                    <a href="outgoing-manifest.php" class="btn btn-warning btn-sm"><i class="fa fa-plus"></i> Create New Out Going Manifest</a>
                    <a class="btn btn-success btn-sm" target="_blank" id="btn_print"><i class="fa fa-print"></i> Print Manifest Copy </a>
                    <a href="outgoing-manifest-list.php" class="btn btn-primary btn-sm"><i class="fa fa-arrow-left"></i> Back to Manifest List</a>
                </center>
            </div>
        </div>
    </div>
</div>
 <style>
        .modal-dialog {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh; 
        }
    </style>

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

<script>
	$(document).ready(function() { 
	    var sound = document.getElementById("scanned_sound"); 
        sound.play(); 
	});
</script>
<audio id="scanned_sound">
  <source src="sound/sound.mp3" type="audio/mpeg">
</audio>
<audio id="scanned_wrong_sound">
  <source src="sound/wrong-sound.mp3" type="audio/mpeg">
</audio>


<script>
const array = [];
const awb_array = [];
    $(document).ready(function(){
        $('#awb_scan').on("keyup", function(e){
       
        if(e.keyCode!=17 && e.keyCode==13){
                var search_text = $(this).val();
                var resultDropdown = $(this).siblings(".result");
               
                if(search_text.length>1){
                    
                    $.get("custom-api/awb-search-api.php", {search_text: search_text}).done(function(data){
                        resultDropdown.html(data);
                        //console.log('ganesan:'+data);
                        if(data==''){
                            var ds='<div><center style="padding:10px;color:red;"> No Data Found...</center><div>';
                            resultDropdown.html(ds);
                            var sound = document.getElementById("scanned_wrong_sound"); 
                            $('.search-box input[type="text"]').select();
                            sound.play();
                        }else{
                            var numItems = $('.drop-down-awb').length;
                            if(numItems==1 && e.keyCode==13){
                                var ids=parseInt($('.drop-down-awb').attr('awb_id'));
                                if(!array.includes(ids)){
                                    product_selection(ids);
                                }else{
                                    $('.search-box input[type="text"]').focus();
                                    $('.search-box input[type="text"]').select();
                                    var ds='<div><center style="padding:10px;color:black;"> This AWB Number Already <b style="color:yellow"> Added </b></center><div>';
                                    $(".result").html(ds);
                                    var sound = document.getElementById("scanned_wrong_sound"); 
                                    sound.play();
                                }
                            } 
                        }
                    });
                } else{
                    resultDropdown.empty();
                }
    }
        });
        
        
        
        
        
        $('#btn_submit').on("click", function(){
        if(array.length>0){
                $("#form_submit").prop('disabled', true);
                var update_status=$('#update_status').val();
                var update_status_name=$("#update_status option:selected").text();
                var status_location=$('#status_location').val();
                var status_location_name=$("#status_location option:selected").text();
                var status_description=$('#status_description').val();
                var status_date=$('#status_date').val();
                
                var from_branch=$('#from_branch').val();
                var to_branch=$('#to_branch').val();
                var to_branch_name=$("#to_branch option:selected").text();
                var coloader=$('#coloader').val();
                var vehicle_no=$('#vehicle_no').val();
                var driver_name=$('#driver_id').val();
                var driver_mobile=$('#driver_mobile').val();
                
                var required_alert = 1 ;
                
                if(driver_name ==''){
                    required_alert = 0 ;
                    alert('Please Select The Driver Name');
                }else if(vehicle_no ==''){
                    required_alert = 0 ;
                    alert('Please Enter The Vehicle No');
                }else if(driver_mobile ==''){
                    required_alert = 0 ;
                    alert('Please Enter The Driver Mobile No');
                }else {
                   var required_alert = 1 ; 
                }
                /*var from_branch=$('#from_branch').val();
                
                var branch_name=$('#branch_name').val();*/
                
                if(required_alert =='1'){
                            $.ajax({
                          type:'POST',
                          url:'custom-api/scanning-outgoing-manifest-update-api.php',
                          data:{updated_ids:array,type:'scanning_status_update_api',from_branch:from_branch,to_branch:to_branch,coloader:coloader,vehicle_no:vehicle_no,driver_name:driver_name,driver_mobile:driver_mobile,status_location_name:status_location_name,update_status:update_status,update_status_name:update_status_name,status_location:status_location,status_description:status_description,status_date:status_date,to_branch_name:to_branch_name},
                          dataType: 'json',
                          beforeSend: function() {
    						$('#update_fa').addClass('fa fa-spinner fa-spin');
    					  },
                          success:function(response){
                              console.log(response);
                              if (response.failure_data && response.failure_data.length > 0) {
                                        alert(response.failure_data);
                                    }
                              $("#form_submit").prop('disabled', false);
                              if(response['error']==false){
                                      $("#update_fa").removeClass("fa fa-spinner fa-spin");
                                      $('#update_fa').addClass('fa fa-paper-plane');
                                     // alert(response['meassge']);
                                       $('#text_update_status').text($("#update_status :selected").text());
                                        $('#scaned_table_data_body').empty();
                                        array.splice(0, array.length);
                                        
                                        $("#btn_print").attr('href', 'manifest-print.php?id=' + response['manifest_id']);
                                        
                                    $("#print_confirm").modal('show');
                                      $('#success_title').text(response['meassge']);
                                      $('#modal-body').show();
                                  }else{
                                       $("#update_fa").removeClass("fa fa-spinner fa-spin");
                                      $('#update_fa').addClass('fa fa-paper-plane');
                                      alert(response['meassge']);
                                      $("#print_confirm").modal('show');
                                      $('#success_title').text(response['meassge']);
                                      $('#modal-body').hide();
                                  }
                    
                          }
                        }); 
                }
        }else{
            alert('scan min one pickup entry to update status');;
        }
    });
    });
    
    
    
    
    function product_selection(id){
                if(array.includes(id)) {
                    var sound = document.getElementById("scanned_wrong_sound"); 
                    sound.play();
                    var ds='<div><center style="padding:10px;color:black;"> This AWB Number Already <b style="color:yellow"> Added </b></center><div>';
                    $(".result").html(ds);
                }else{
                   /* var update_status=$('#update_status').val();
                    var client_id = $('#client_id').val();
                    var branch_name = $('#branch_name').val();*/
                    $.ajax({
                      type:'POST',
                      url:'custom-api/get-awb-information.php',
                      data:{pickup_entry_id:id,type:'pickup_entry_info'},
                      dataType: 'json',
                      success:function(response){
                          $('#scaned_table_data').prepend(response['data']);
                          array.push(id);
                          var sound = document.getElementById("scanned_sound"); 
                          sound.play(); 
                          $(".result").empty();
                      }
                    });
                }
                $('.search-box input[type="text"]').val('');
                $('.search-box input[type="text"]').focus();
            }
            
            
function removeA(arr) {
    var what, a = arguments, L = a.length, ax;
    while (L > 1 && arr.length) {
        what = a[--L];
        while ((ax= arr.indexOf(what)) !== -1) {
            arr.splice(ax, 1);
        }
    }
    return arr;
}    


function remove(id){
    if (confirm('Confirm if you want to remove this AWB')){
        $('table#scaned_table_data tr#pic_'+id).remove();
        removeA(array,id);
    }
}

function DriverDetails(){
    var driver_id = $("#driver_id").val();
    var driver_name = $("#driver_id option:selected").data("driver_name");
    var driver_mobile_no = $("#driver_id option:selected").data("driver_mobile_no");
    var vehicle_no = $("#driver_id option:selected").data("vechicle_no");

    $("#vehicle_no").val(vehicle_no);
    $("#driver_mobile").val(driver_mobile_no);
    
}

function StatusUpdateLocation(){
    var from_branch = $("#from_branch").val();
    
    $("#status_location").val(from_branch);
    $("#status_location").trigger("change");
}
</script>


<style>
    .search-box{
        font-size: 14px;
    }
    .search-box {
        padding: 0px 10px;
        font-size: 14px;
    }
    .result{
        position: absolute;        
        z-index: 999;
        top: 89%;
        left: 0;
    }
    .search-box, .result{
        width: 100%;
        box-sizing: border-box;
    }
    /* Formatting result items */
    .result div{
        margin: 0;
        background: #d4d4d4;
        padding: 1px 5px;
        border: 1px solid #CCCCCC;
        border-top: none;
        cursor: pointer;
        margin-top: 0px;
    }
    .result div:hover{
        background: #00a65a;
        color: white;
        padding:0px;
        font-weight:800;
    }
    .delete:hover{
        background: red;
        color: white;
        padding:5px;
        font-weight:800;
    }
</style>