<?php require_once 'header.php'; ?>
<?php if($session_id==1 || $allPermissions['delivery_status']['read']=="1"){ ?>


<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="booking-list.php" class="btn btn-primary btn-sm"><i class="fa fa-arrow-left"></i> Back to Booking List</a>
        		</div>
    </div>
    
    
    <div class="row" style="padding-left: 10px; padding-right: 10px; margin-top:10px;">
        


			
			<div class="col-md-2">
                <label>Delivery Date <span class="required"></span></label>
            	<input type="date" class="form-control"  name="delivery_date" id="delivery_date" value="<?=date('Y-m-d')?>" requierd />
			</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 style="text-align: center;">To Pay</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-1">
			    <label>Action</label><br>
			    <?php if($session_id==1 || $allPermissions['delivery_status']['update']=="1"){ ?>
					<button id='btn_submit' class="btn btn-success btn-sm"><i id="update_fa" class="fa fa-paper-plane"></i> Update Status</span> </button>
			 <?php }?>
			 </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">Delivery Created Successfully</h4></center>
            </div>
            <div class="modal-body" id="modal-body">
                
            </div>
        </div>
    </div>
</div>
 <style>
        .modal-dialog {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh; 
        }
    </style>
    <?php }?>
<?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>
     $(document).ready(function(){
         
        $('#driver_id').on("change", function(){
          
          var vehicle_no =  $("#driver_id option:selected").attr("data-vehicle_no");
          var driver_name = $("#driver_id option:selected").attr("data-name");
          var driver_mobile_no = $("#driver_id option:selected").attr("data-mobileno");
          
           
           
           $('#driver_name').val(driver_name);
           $('#driver_mobile_no').val(driver_mobile_no);
           $('#vehicle_no').val(vehicle_no);
           
        });
        
        
        $('#delivery_boy_id').on("change", function(){
          
          var delivery_boy_name = $("#delivery_boy_id option:selected").attr("data-name");
          var delivery_boy_mobile_no = $("#delivery_boy_id option:selected").attr("data-mobileno");
          
           
           
           $('#delivery_boy_name').val(delivery_boy_name);
           $('#delivery_boy_mobile_no').val(delivery_boy_mobile_no);
           
        });
     });
</script>
<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, status:'delivery_update'}).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 delivery_date = $('#delivery_date').val();
               $.ajax({
                          type:'POST',
                          url:'custom-api/delivery-status-update-api.php',
                          data:{updated_ids:array,type:'delivery_status_delivered',
                                delivery_date:delivery_date},
                          dataType: 'json',
                          beforeSend: function() {
    						$('#update_fa').addClass('fa fa-spinner fa-spin');
    					  },
                          success:function(response){
                              $("#form_submit").prop('disabled', false);
                              if(response['error']==false){
                                  /*window.location.href="index.php";*/
                                     /* $("#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', 'delivery-run-sheet-print.php?id=' + response['run_sheet_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 Shipment to add delivery run sheet');;
        }
    });
    });
    
    
    
    
    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-runsheet-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);
    }
}
</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>