<?php
ob_start();
session_start();
include "../inc/config.php";
include "../inc/session.php";
include "../inc/functions.php";
$allPermissions = $_SESSION['permission'];
$requestData= $_REQUEST;
$conditions="";
$status = $_REQUEST['status'];
$branch_id = $_REQUEST['branch_id'];
if($status!=""){
    $conditions.=" and pp.status=".$status;
}

if($branch_id!=""){
    $conditions.=" and pp.branch_id=".$branch_id;
}


/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'id', 
            2 =>'id',
            3 =>'id'
        );
/*Fetch All Data Start*/        
  if($session['id']!=1 && $session_branch_ids!='0'){
        $conditions.= " and pp.branch_id IN ($session_branch_ids)";
    }      
    	$statement = $pdo->prepare("SELECT pp.* FROM tbl_delivery_boy pp WHERE 1=1 $conditions ORDER BY pp.id DESC");
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
         $sql="SELECT pp.*
         FROM tbl_delivery_boy pp 
         where 1=1 $conditions";
         
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( pp.name LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR pp.mobile_no LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR pp.address LIKE '%".$requestData['search']['value']."%' ) ";
        }
        	 
        $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
        $totalFiltered = $statement->rowCount();
        
/*Fetch Filter Data End*/       
        


        $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
       //error_log($sql);
        $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);

$data = array();
$i=$requestData['start']+1;
foreach( $query as $row ) {  // preparing an array
	$nestedData=array(); 
 	$nestedData[] = $i;
 	
 	$_details='';
        
        	
        if($row["status"]==0){	
            $s = "<a class='btn btn-danger btn-xs'><i class='fa fa-edit'></i> In-Active</a>";
        }else{
            $s = "<a class='btn btn-success btn-xs'><i class='fa fa-eye'></i> Active</a>";
        }
        
        
       
        // $_details.="<span class='label_title'>Name : </span><span style='font-weight:bold;'>".$row["name"]."</span>";
        // $_details.="<br><span class='label_title'>Mobile : </span><span style='font-weight:bold;'>".$row["mobile_no"]."</span>";
        // $_details.="<br><span class='label_title'>Address : </span><span style='font-weight:bold;'>".$row["address"]."</span>";
        // $_details.="<br><span class='label_title'>City : </span><span style='font-weight:bold;'>".$row["city"]."</span>";
        // $_details.="<br><span class='label_title'>State : </span><span style='font-weight:bold;'>".$row["state"]."</span>";
        // $_details.="<br><span class='label_title'>Pincode : </span><span style='font-weight:bold;'>".$row["pincode"]."</span>";
        // $_details.="<br><span class='label_title'>Country : </span><span style='font-weight:bold;'>".$row["country"]."</span>";

 	
 	$action='';
 	if($session_id==1 || $allPermissions['delivery_boy']['update']=="1"){ 
 	$action.="<a href='delivery_man_details.php?key=".id_encrypt($row['id'])."' class='btn btn-warning btn-xs mr-2'><i class='fa fa-backward'></i> Update Info</a>";
 	}
 	
 	if(!empty($row['delivery_man_proof'])){
 	    $proof = '<img src="img/delivery-man-proof/'.$row['delivery_man_proof'].'"  style="width:100px;height:50px;object-fit:contain">';
 	}else{
 	   $proof = '';  
 	}
 	
 	$nestedData[] = $action;
 	$nestedData[] = "<span class='label_title'>".$row["name"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["age"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["email"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["experience"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["salary"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["education"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["mother"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["father"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["mobile_no"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["address"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["city"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["pincode"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["country"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["state"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$proof."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["created_at"]."</span>";
	$nestedData[] = $s;
	$data[] = $nestedData;
	$i++;
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ), 
			"recordsFiltered" => intval( $totalFiltered ), 
			"data"            => $data  
			);

echo json_encode($json_data);  

?>