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

<?php 
if(isset($_GET['id'])){
    $ids = $_GET['id'];
    
    $statements = $pdo->prepare("SELECT * FROM tbl_delivery_run_sheet WHERE id=?");
    $statements->execute(array($_REQUEST['id']));
    $resultss = $statements->fetchAll(PDO::FETCH_ASSOC);
    $totalss = $statements->rowCount();
    
    if($totalss>0){
        $drs_no = $resultss[0]['drs_no'];
        $delivery_boy_id = $resultss[0]['delivery_boy_id'];
        $delivery_boy_name = $resultss[0]['delivery_boy_name'];
        $delivery_boy_mobile_no = $resultss[0]['delivery_boy_mobile_no'];
        $vehicle_no = $resultss[0]['vehicle_no'];
        $date = $resultss[0]['date'];
        $pickup_entry_ids = $resultss[0]['shipment_ids'];
        $shipment_count = $resultss[0]['shipment_count'];
    }

}
?>

<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>
        
        <div class="row">
    		<div class="col-md-4" style="text-align: left;">
    			<h4><i class="fa fa-map-marker"></i> Update Delivery Runsheet Details</h4>
    		</div>
    		<div class="col-md-8" style="text-align: right;"> 
    		    <a href="delivery-run-sheet.php" class="btn btn-primary btn-sm"><i class="fa fa-arrow-left"></i> Back to DRS List</a>
    		</div>
    	</div> 
	
	
   </div> 
    
    
<form id="form_details">
  <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 data-sortable="false" id="checkAll" style="min-width:50px;"><i class="fa fa-check"></i><span id="checkbox_section"></span></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>Update Status</th>
									<th>Remarks</th>
								</tr>
							</thead>
							<tbody id="scaned_table_data_body">
							    
							    
<?php if(isset($_GET['id'])){
	$statementt = $pdo->prepare("SELECT * FROM pickup_entry where id IN ($pickup_entry_ids)");
    $statementt->execute();
    $_datas = $statementt->fetchAll(PDO::FETCH_ASSOC);
    $totalDatas = $statementt->rowCount();
    $conditions="1=1";
    
    
    if($totalDatas>0){
        
        $data='';
        $destin='';   
        $selected = "selected";
            foreach($_datas as $d){
                echo "<tr id='pic_".$d['id']."'>
    				    <td class='delete' style='text-align:center;' onclick='remove(".$d['id'].")' ><i class='fa fa-trash'></i></td>
    				    <td><i class='fa fa-barcode' style='font-size:12px;color:blue'></i><b style='font-weight:800;'> ".$d['cscs_awb']."</b></td>
    				    <td><input type='checkbox' name='assign_ids' value='".$d['id']."' class=' checkbox'></td>
    				    <td><i class='fa fa-dropbox'></i> <span class='no_of_box'>".$d['boxes']."</span></td>
				        <td><i class='fa fa-inr'></i> <span class='no_of_box'>".$d['order_type']."</span></td>
    				    <td>".date("d-m-Y", strtotime($d['created_at']))."</td>
    				    <td>".strtoupper($d['status'])."</td>
    				    <td><span class='a_wt'>".$d['a_weight']."</a></td>
    				    <td>".$d['client_id']."</td>
    				    <td>".$d['shipper_name']."</td>
    				    <td><b style='font-weight:800;'>".$d['consignee_name']."</b></td>
    				    <td>
    				        <input type='hidden' name='pickup_entry_id[]' value='".$d['id']."'>
    				        <select name='pickup_status[]' class='form-control'>
    				            <option value='5'" . ($d['status_code'] == '5' ? " selected" : "") . ">DELIVERED</option>
                                <option value='6'" . ($d['status_code'] == '6' ? " selected" : "") . ">UN-DELIVERED</option>
                                <option value='7'" . ($d['status_code'] == '7' ? " selected" : "") . ">RETURN TO ORIGIN</option>
    				            
    				        </select>
    				    </td>
    				    <td>
    				        <textarea  name='remarks[]' class='form-control'>".$d['remarks']."</textarea>
    				    </td>
    				</tr>";
    					$destin=$d['consignee_city'];
            }
	    }
} ?>   	
	
	
	
							    
							   
							</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>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 class="col-md-1">
			    <label>Actions</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> 
</form>
    
</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 Status Updated</h4></center>
            </div>
            <div class="modal-body" id="modal-body">
                <center>
                    <a href="delivery-runsheet-add.php" class="btn btn-warning btn-sm"><i class="fa fa-plus"></i> Create New Run Sheet</a>
                    <a class="btn btn-success btn-sm" target="_blank" id="btn_print"><i class="fa fa-print"></i> Print Run Sheet Copy </a>
                    <a href="delivery-run-sheet.php" class="btn btn-primary btn-sm"><i class="fa fa-arrow-left"></i> Back to Run Sheet 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>

</script>
<script>

    
$('#form_details').on('submit', function(e){
                e.preventDefault();
            $('#loading-div').show();
            $('#content-div').hide();
                console.log('Register Form Submit Option Start...');
                var formData = new FormData(this);
                formData.append('type', 'drs_update_status');
                        $.ajax({
                            url: 'apis/pickup-entry-update-apis.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                
                                if(response['status']==200){
                                         //put success coode  
                                         alert(response['message']);
                                         $('#loading-div').hide();
                                         $('#success-div').show();
                                    
                                }else{
                                    //put falied code
                                    alert(response['message']);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                                
                               
                            }
                        });
                
    }); 
    
            
            
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>

<script>

$(document).ready(function() {            
              $('#checkAll').click(function () { 
                    var checkbox_section=$('#checkbox_section').text();
                    if(checkbox_section==''){
                        $('#checkbox_section').text('Checked');
                        $('.checkbox').prop('checked',true);
                    }else{
                        $('#checkbox_section').text('');
                        console.log('Un Checked');
                        $('.checkbox').prop('checked',false);
                    }
             });

                
                
                $('#btn_assign').on('click',function(){
                var checkValues = $('input[name=assign_ids]:checked').map(function(){
                            return $(this).val();
                        }).get();
                        
                        var shift_details = $('#shift_details').val();
                if(checkValues != '' && shift_details!='0'){
                    var r = confirm("Confirm If You Want to Assign Those Shift for Employees?");
            	    console.log(checkValues);
                    if (r == true) {
                        var ids=[];
                        var shift_details = $('#shift_details').val();
                        ids = checkValues;
                        $.ajax({
                            url: 'api/bulk-updates.php',
                            type: 'post',
                            data: {type:'shift_assign',assign_id:ids,shift_details:shift_details},
                            success: function(response){
                            console.log(response);
                            emp_datas();
                            shift_details_function();
                            $('#d').text('');
                      }
                    });
                }
                 
                }else{
                    alert("Please Select Minimum One Employee for Shift Assign...");
                }
                });   
                
                
                /*Update Locations Here*/
                $('#update_locations').on('click',function(){
                var checkValues = $('input[name=assign_ids]:checked').map(function(){
                            return $(this).val();
                        }).get();
                        
                        var location_id = $('#location_id').val();
                if(checkValues != '' && location_id!=''){
                    var r = confirm("Confirm If You Want to Assign Those Locations for Employees?");
            	    console.log(checkValues);
                    if (r == true) {
                        var ids=[];
                        var location_id = $('#location_id').val();
                        ids = checkValues;
                        $.ajax({
                            url: 'api/bulk-updates.php',
                            type: 'post',
                            data: {type:'update_locations',assign_id:ids, location_id:location_id},
                            success: function(response){
                            console.log(response);
                            emp_datas();
                            shift_details_function();
                            $('#d').text('');
                      }
                    });
                }
                 
                }else{
                    alert("Please Select Minimum One Employee for Locations...");
                }
                });   
                
                
                
                
                
                
                
            });  
            
            
</script>