<?php
ob_start();
session_start();
include "../inc/config.php";
include "../inc/session.php";
include "../inc/functions.php";
$allPermissions = $_SESSION['permission'];

$requestData= $_REQUEST;
$conditions='';
$client_id= $_POST['client_id'] ?? '';
$zone_id= $_POST['zone_id'] ?? '';
$from_city= $_POST['from_city'] ?? '';
$destination_state= $_POST['destination_state'] ?? '';

if($client_id!=''){
    $conditions.=" and tbl_generate_zone.client_id=".$client_id;
}if($zone_id!=''){
    $conditions.=" and tbl_generate_zone.zone_id=".$zone_id;
}
if($from_city!=''){
    $conditions.=" and tbl_generate_zone.from_city='".$from_city."'";
}
if($destination_state!=''){
    $conditions.=" and tbl_generate_zone.destination_state='".$destination_state."'";
}

/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'client_id', 
            2 =>'from_city', 
            3 =>'zone_id',
            4 =>'destination_state',
            5 =>'destination_cities'
        );
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT * from tbl_generate_zone where 1=1 $conditions order by 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 tbl_generate_zone.*,tbl_client_new.name as client_name,tbl_zone.zone_name from tbl_generate_zone left join tbl_client_new on tbl_generate_zone.client_id=tbl_client_new.id left join tbl_zone on tbl_generate_zone.zone_id=tbl_zone.id where 1=1 $conditions";
         
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( tbl_generate_zone.destination_cities 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']."   ";
        $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>";
        }
       
 	
 	$action='';
 	 if($session_id==1 || $allPermissions['generate_zone']['update']=="1"){
 	$action.="<a href='generate-zones.php?key=".id_encrypt($row['id'])."' class='btn btn-warning btn-xs mr-2'><i class='fa fa-backward'></i> Update Info</a>";
 	 }
 	if (strlen($row["destination_cities"]) > 45)
        $row["destination_cities"] = substr($row["destination_cities"], 0, 42) . '...';
 	
 	$nestedData[] = $action;
 	$nestedData[] = "<span class='label_title'>".$row["client_name"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["from_city"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["zone_name"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["destination_state"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["destination_cities"]."</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);  

?>