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



/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'zone_name', 
            2 =>'description', 
            3 =>'status'
        );
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT * from tbl_zone 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 * from tbl_zone where 1=1 ";
         
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( tbl_zone.zone_name 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['zone_master']['update']=="1"){ 
 	$action.="<a href='zone.php?key=".id_encrypt($row['id'])."' class='btn btn-warning btn-xs mr-2'><i class='fa fa-backward'></i> Update Info</a>";
    }
 	$nestedData[] = $action;
 	$nestedData[] = "<span class='label_title'>".$row["zone_name"]."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["description"]."</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);  

?>