<?php require_once('header.php'); ?>
<?php if($session_id==1 || $allPermissions['drs']['read']=="1"){ ?>
<section class="content" style="background-color: white;">
    <div class="row">
		<div class="col-md-4" style="text-align: left;">
			<h4><i class="fa fa-map-marker"></i> Delivery Runsheet Details</h4>
		</div>
		<div class="col-md-8" style="text-align: right;"> 
		<?php if($session_id==1 || $allPermissions['drs']['create']=="1"){ ?>
		    <a href="delivery-runsheet-add.php" class="btn btn-success btn-sm"><i class="fa fa-plus"></i> Create New DRS</a>
		    <?php }?>
		</div>
	</div> 
	
	<div class="row" style="padding-left: 10px;padding-right: 10px;margin-top:10px;">
	    <div class='col-md-2 col-sm-6'>
                 <label for="from">From Date:</label>
                 <input type="date" id="from_date" class="form-control" value="<?php echo date("Y-m-d") ?>" name="from_date" onchange="date_changed()">
        </div>
        <div class='col-md-2 col-sm-6'>
             <label for="to">To Date:</label>
             <input type="date" id="to_date" class="form-control" value="<?php echo date("Y-m-d") ?>" name="to_date" onchange="date_changed()">
        </div>
        
        
        <div class="col-md-3">
                <label>Branch</label>
            		<select class="form-control select2" name="branch_id" id="branch_id" >
            		    <option value="">All Branch</option>
            		    <?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-3">
                <label>Select Delivery Boy</label>
            		<select class="form-control select2" name="boy_id" id="boy_id" onchange="getData()">
            		    <option value="">All</option>
            	        <?php
            			$statement = $pdo->prepare("SELECT * FROM `tbl_delivery_boy` where status='1'");
            			$statement->execute();
            			$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
            			foreach ($result as $row) { ?>
            				<option value="<?=$row['id']?>"><?=$row['name'];?> - <?=$row['mobile_no']?></option>
            			<?php } ?>
            	    </select>
			</div>
			
			<div class="col-md-3">
                <label>Select Driver</label>
            		<select class="form-control select2" name="driver_id" id="driver_id" onchange="getData()">
            		    <option value="">All</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['id']?>"><?=$row['name'];?> - <?=$row['mobile_no']?></option>
            			<?php } ?>
            	    </select>
			</div>
		
    </div>
    
	<div class="row">
		<div class="col-md-12">
			<div>
			<div class="box-body table-responsive">
					<table id="employee-grid"  class="table table-bordered table-striped">
						<thead>
							<tr>
								<th width="30">S.No</th>
								<th>Print</th>
								<th>DRS No</th>
								<th>Delivery Boy Name</th>
								<th>Mobile No</th>
								<th>Total Shipment</th>
								<th>Total Box</th>
								<th>Total Wt</th>
								<th>Date</th>
								<th>Vechicle No</th>
								<th>Status</th>
							</tr>
						</thead>
					</table>
				</div>
			</div>
		</div>
	</div>
</section>
<?php }?>
<?php require_once('footer.php'); ?>

<script type="text/javascript">
$(document).ready(function(){
    getData();
});

function date_changed(){
    var from_date=$('#from_date').val();
    var to_date=$('#to_date').val();
    if(from_date!='' && to_date!=''){
        getData();
    }
}
function getData(){
    var boy_id=$('#boy_id').val();
    var from_date=$('#from_date').val();
    var to_date=$('#to_date').val();
    var driver_id = $('#driver_id').val();
    
    var dataTable = $('#employee-grid').DataTable( {
		"processing": true,
		"serverSide": true,    
        "destroy":true,
        "ajax":{
			url : "data-tables/deliver-runsheet-data-table.php",
			type: "post",
			data:{'type':2, boy_id:boy_id, driver_id:driver_id, from_date:from_date,to_date:to_date},
			error: function(){ 
				$(".employee-grid-error").html("");
				$("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="10" style="text-align;center;">No data found in the server</th></tr></tbody>');
				$("#employee-grid_processing").css("display","none");
			}
		}
	} );
    }
</script>