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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" language="javascript" >
			$(document).ready(function() {
			    getData();
			} );
			function getData()
			{
			    var client_id   = $('#client_id').val();
			    var client_type = $('#client_type').val();
				var dataTable = $('#delivery-grid').DataTable( {
					"processing": true,
					"serverSide": true,
					"bDestroy": true,
					"ajax":{
						url :"data-tables/delivery-update-data-table.php", // json datasource
						data: {client_id:client_id,client_type:client_type},
						type: "post",  // method  , by default get
						error: function(){  // error handling
							$(".delivery-grid-error").html("");
							$("#delivery-grid").append('<tbody class="delivery-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
							$("#delivery-grid_processing").css("display","none");
							
						}
					}
				} );
			}
		</script>
<script>
$(document).ready(function(){
    $('input[type=datetime-local]').val(new Date().toJSON().slice(0,19));
});

</script>


<script>
    $(document).ready(function(){
       $('#status').on('change',function(){
        //   var status_v= $("#status option:selected").val();
        //   alert(status_v);
           var status= $("#status option:selected").text();
         
         if(status=='Delivered'){
              $("#delivered").show();
              $("#undelivered").hide();
         }
         else if(status=='Undelivery'){
               $("#delivered").hide();
              $("#undelivered").show();
         }
         else
         {
               $("#delivered").hide();
              $("#undelivered").hide();
         }
        });
    });
</script>

<script type="text/javascript">
$(document).ready(function(){
    $('#select_all').on('click',function(){
        if(this.checked){
            $('.checkbox').each(function(){
                this.checked = true;
            });
        }else{
             $('.checkbox').each(function(){
                this.checked = false;
            });
        }
      });
    
    $('.checkbox').on('click',function(){
        if($('.checkbox:checked').length == $('.checkbox').length){
            $('#select_all').prop('checked',true);
        }else{
            $('#select_all').prop('checked',false);
        }
    });
});

 function updatestatusTo(){
 var status_value=$("#status option:selected").val();
      
      if ($('#pod_file3').get(0).files.length != 0) {
    console.log("files 3 selected.");
}
 if ($('#pod_file2').get(0).files.length != 0) {
    console.log("files 2 selected.");
}
 if ($('#pod_file1').get(0).files.length != 0) {
    console.log("files 1 selected.");
}
        let myForm = document.getElementById('delivery_form');
        let formData = new FormData(myForm);
       formData.append("pickup_id", checkValues);
       var checkValues = $('input[name=checkboxlist]:checked').map(function()
            {
                return $(this).val();
            }).get();
            
            formData.append("pickup_id", checkValues);
            var check_count=checkValues.length;
   
    var v1=$('#pod_file1').get(0).files.length;
    var v2=$('#pod_file2').get(0).files.length;
    var v3=$('#pod_file3').get(0).files.length;
    var v4=$('#pod_file4').get(0).files.length;
    var v5=$('#pod_file5').get(0).files.length;         
if(status_value=='7' && (v1=='1' ||v2=='1' ||v3=='1' ||v4=='1' ||v5=='1') && check_count>1)
{
    $("#error_status_table").show();
    $("#res_status_table").hide();
    $("#error_msgs").text('Sorry !. Please select only one pickup entry for POD upload ');
}else{      
    if(checkValues!=""){
          $.ajax({
        url: 'delivery_status_update.php',
        type: 'post',
        data: formData,
        contentType: false,
        processData: false,
         beforeSend: function() {
                        // setting a timeout
                        $('#update_fa').removeClass('fa fa-paper-plane');
                        $('#update_txt').text('Please Wait');
                        $('#update_fa').addClass('fa fa-spinner fa-spin');
                    },
        success: function(response)
        {
            $("#error_status_table").hide();
            $('#update_txt').text('Update All');
            $('#update_fa').removeClass('fa fa-spinner fa-spin');
            $('#update_fa').addClass('fa fa-paper-plane');
            console.log(response);
            $("#res_status_table").show();
            $("#res_status_table_data").html(response);
            getData();
        }

    });
        }
            else{
                alert('No Value Selected');
            }
}
}
 
</script>
<section class="content-header">
	<div class="content-header-left">
		<h1>View Delivery</h1>
	</div>
<!--Bulk Upload Code Start-->
	<div class="row">
	    <div class="col-md-12">
	        <?php if($allPermissions['statusChange']['create']=="1"){ ?>
	        <div class="col-md-2">  
        	    <div class="content-header-left" >
	   	    	<a href="delivery_bulk_upload.php" class="btn btn-danger btn-sm">Bulk Upload</a>
            	</div>
    	    </div>
    	    <?php } ?>
    	</div>
	</div>
	<input type='hidden' name="client_id" id="client_id" value="<?php echo $clientId; ?>">
<input type="hidden" id="client_type" name="client_type" value="<?php echo $clientType; ?>">
	<!--Bulk Upload Code End-->	
</section><br>
<?php if($allPermissions['statusChange']['update']=="1"){ ?>
<form id='delivery_form' action="#" method="POST">
<div class="row">
	    <div class="col-md-12">
	        <div class="col-md-5">
	        	<div class="form-group">
					<label for="" class="col-sm-6 control-label text-right">Select Status <span>*</span></label>
							<div class="col-sm-6">
							    <select name="status" class="form-control" id='status'>
							        <option value="4">In Transit</option> 
							        <option value="5">Received at Destination</option>
							        <option value="6">Out for delivery</option>
							        <option value="7">Delivered</option>
							        <option value="8">Undelivery</option>
							        ?>
							        
							    </select>
							</div>	
						</div>		
				</div>
				<div class="col-md-5">
				        <div class="form-group">
				    	<label for="" class="col-sm-6 control-label text-right">Status Update Date<span>*</span></label>
							<div class="col-sm-6">
								<!--<input type="datetime-local" autocomplete="off" class="form-control" name="s_date" id="s_date" >-->
								<input type="date" autocomplete="off" class="form-control" name="s_date" id="s_date" value="<?php echo date('Y-m-d');?>">
							</div>
				</div>
				    </div>
				<div id='delivered' style="display:none;padding-top: 4%;" >
				    <div class="col-md-5">
				        <div class="form-group">
				    	<label for="" class="col-sm-6 control-label text-right">Receiver Name <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="r_name" id="r_name"  value="">
							</div>
				</div>
				    </div>
				    <div class="col-md-5">
				        <div class="form-group">
				    	<label for="" class="col-sm-6 control-label text-right">Receiver Mobile <span>*</span></label>
							<div class="col-sm-6">
								<input type="number" autocomplete="off" class="form-control" name="r_mobile" id="r_mobile" value="">
							</div>
				</div>
				    </div>
				    
	     <div style="padding-top: 4%;">
				    <div class="col-md-5">
				        <div class="form-group">
				    	<label for="" class="col-sm-6 control-label text-right">Relation <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="r_relation" id="r_relation" value="">
							</div>
				</div>
				    </div>
				    <div class="col-md-5">
				        <div class="form-group">
				    	<label for="" class="col-sm-6 control-label text-right">Delivery Date <span>*</span></label>
							<div class="col-sm-6">
								<input type="date" autocomplete="off" class="form-control" name="r_delivery_date" id="r_delivery_date" value="<?php echo date('Y-m-d');?>">
							</div>
			    </div>
				  </div>
				    <br>
				     <div class="col-md-12">
				     <div class="col-md-2">
				        <div class="form-group">
							<div class="col-sm-12" style="padding-top:5px">
								<input type="file" name="pod_file1" id="pod_file1"  accept="image/*">(POD - 1)
							</div>
						</div>
				    </div>
				    <div class="col-md-2">
				        <div class="form-group">
							<div class="col-sm-12" style="padding-top:5px">
								<input type="file" name="pod_file2" id="pod_file2" accept="image/*">(POD - 2)
							</div>
						</div>
				    </div>
				    <div class="col-md-2">
				        <div class="form-group">
							<div class="col-sm-12" style="padding-top:5px">
								<input type="file" name="pod_file3"  id="pod_file3" accept="image/*">(POD - 3)
							</div>
						</div>
				    </div>
				    <div class="col-md-2">
				        <div class="form-group">
							<div class="col-sm-12" style="padding-top:5px">
								<input type="file" name="pod_file4" id="pod_file4" accept="image/*">(POD - 4)
							</div>
						</div>
				    </div>
				    <div class="col-md-2">
				        <div class="form-group">
							<div class="col-sm-12" style="padding-top:5px">
								<input type="file" name="pod_file5" id="pod_file5" accept="image/*">(POD - 5)
							</div>
						</div>
				    </div>
			    </div>
			  </div>
				</div>
				<div id='undelivered' style="display:none;padding-top: 4%;">
				    <div class="col-md-5">
				        <div class="form-group">
				    	<label for="" class="col-sm-6 control-label text-right">Reason <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="reason" id="reason" value="">
							</div>
				</div>
				    </div>
				    <div class="col-md-5">
				        <div class="form-group">
				    	<label for="" class="col-sm-6 control-label text-right">Remark <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="remark" id="remark" value="">
							</div>
				</div>
				    </div>
				</div>
				<div class="col-md-12">
					<center><button type="button" id='update_all' class="btn btn-danger btn-sm" onclick="updatestatusTo();"><span id="update_txt">Update All</span> <i id="update_fa" class="fa fa-paper-plane"></i></button></center>
					</div>
				
		<br>	<br>
	        
	    </div>
	</div>
		</form>
		<?php } ?>
		<div id="error_status_table" class="alert alert-danger alert-dismissible"  style='display:none;margin-left: 2%;margin-right: 2%;' >
	    <a href="#" class="close" data-dismiss="alert" onclick='location.reload();' aria-label="close">Close &times;</a>
	    <span id="error_msgs"></span>
	</div>
	 <div id="res_status_table" class="alert alert-success alert-dismissible"  style='display:none;margin-left: 2%;margin-right: 2%;' >
	    <a href="#" class="close" data-dismiss="alert" onclick='location.reload();' aria-label="close">Close &times;</a>
    	<table id="res_status_table_data" class="table">
    	    
    	</table>
	</div>
<section class="content">
	<div class="row">
		<div class="col-md-12">
			<div class="box box-info">
				<div class="box-body table-responsive">
					<table id="delivery-grid"  class="table table-bordered table-striped">
						<thead>
							<tr>
								<th width="30">SL</th>
								<th data-sortable= false><i class="fa fa-check"></i><input type="checkbox" id="select_all" /></th>
								<?php  if($clientType=="Admin")	 { ?>	<th>Client</th><?php } ?>
								<th>CSCS AWB</th>
							<?php  if($clientType=="Admin")	 { ?>
							    <th>3PL AWB</th>
								<th>3PL Name</th>
								<?php } ?>
								<th>Ref No</th>
								<th>Order Type</th>
								<th>Status</th>
								<th>Shipper Name</th>
								<th>Shipper Address</th>
								<th>Shipper City</th>
								<th>Shipper State</th>
								<th>Shipper Phone</th>
								<th>Shipper Pincode</th>
								<th>Consignee Name</th>
								<th>Consignee Address</th>
								<th>Consignee City</th>
								<th>Consignee State</th>
								<th>Consignee Phone</th>
								<th>Consignee Pincode</th>
								<th>Volumetric Weight</th>
								<th>Volumetric Length</th>
								<th>Volumetric Breadth</th>
								<th>Volumetric Height</th>
								<th>Collectable Amount</th>
								<th>Product Description</th>
								<th>Quantity</th>
								<th>Invoice Value</th>
								<th>Taxable Value</th>
								<th>Hsn Code</th>
								<th>Discount</th>
								<th>Gst Amount</th>
								<th>MRP</th>
								<th>Ewaybill</th>
								<th>Actual Weight</th>
								<th>Charged Weight</th>
								<th>Gst Type</th>
								<th>Buyer Gst</th>
								<th>Seller Gst</th>
								<th>RTO Name</th>
								<th>RTO Address</th>
								<th>RTO City</th>
								<th>RTO State</th>
								<th>RTO Phone</th>
								<th>RTO Pincode</th>
								<th>Date Added</th>
							</tr>
						</thead>
					</table>
				</div>
			</div>
		</div>
	</div>


</section>



<div class="modal fade" id="confirm-delete" 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>
                <h4 class="modal-title" id="myModalLabel">Delete Confirmation</h4>
            </div>
            <div class="modal-body">
                <p>Are you sure want to delete this item?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a class="btn btn-danger btn-ok">Delete</a>
            </div>
        </div>
    </div>
</div>

<?php }else{ ?>
<center><h4 style="color:red;">You don't have permission to view status change page </h4></center>
<?php } ?>
<?php require_once('footer.php'); ?>