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

/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'pin_code',
            2 =>'city',
            3 =>'state',
            4 =>'service',
            5 =>'air',
            6 =>'road',
            7 =>'b2b',
            8 =>'b2c',
            9 =>'oda'
        );
        
/*Fetch All Data Start*/    
    	$statement = $pdo->prepare("SELECT * FROM pincode WHERE delete_status!=1");   
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
        $sql="SELECT * FROM pincode WHERE delete_status!=1";
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( pin_code LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR city LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR state LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR service 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']."   ";
//  $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */	
$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;
 	if($edit=='1')
 	{ 	
     	$action="<a href='pincode-edit.php?id=".$row['id']."' class='btn btn-danger btn-xs'>Edit</a>&nbsp;";
 	} 
 	if($edit=='1'&& $del=='1')
 	{ 	
     	$action .=" <a href='#' class='btn btn-success btn-xs' data-href='pincode-delete.php?id=".$row['id']."' data-toggle='modal' data-target='#confirm-delete'>Delete</a>";
     	$nestedData[]=$action;
     	$nestedData[] ="<input type='checkbox' class='checkbox' name='checkboxlist' value=".$row['id']." />"; 
 	}
 	elseif($del=='1'){
 	    $action =" <a href='#' class='btn btn-success btn-xs' data-href='pincode-delete.php?id=".$row['id']."' data-toggle='modal' data-target='#confirm-delete'>Delete</a>";
 	    $nestedData[]=$action;
 	    $nestedData[] ="<input type='checkbox' class='checkbox' name='checkboxlist' value=".$row['id']." />"; 
 	}elseif($edit=='1'){
 	    $nestedData[]=$action;
 	}
 	
 	$nestedData[] = $row["pin_code"];
	$nestedData[] = $row["city"];
	$nestedData[] = $row["state"];
	$nestedData[] = $row["location"];
	if($row["service"]=="Yes"){$nestedData[] ="<i class='fa fa-check-circle ' aria-hidden='true' style='font-size:24px;color:green;'></i>";}else{$nestedData[] ="<i class='fa fa-times-circle ' aria-hidden='true' style='font-size:24px;color:red;'></i>";}
	if($row["air"]=="Yes"){$nestedData[] ="<i class='fa fa-check-circle ' aria-hidden='true' style='font-size:24px;color:green;'></i>";}else{$nestedData[] ="<i class='fa fa-times-circle ' aria-hidden='true' style='font-size:24px;color:red;'></i>";}
	if($row["road"]=="Yes"){$nestedData[] ="<i class='fa fa-check-circle ' aria-hidden='true' style='font-size:24px;color:green;'></i>";}else{$nestedData[] ="<i class='fa fa-times-circle ' aria-hidden='true' style='font-size:24px;color:red;'></i>";}
	if($row["b2b"]=="Yes"){$nestedData[] ="<i class='fa fa-check-circle ' aria-hidden='true' style='font-size:24px;color:green;'></i>";}else{$nestedData[] ="<i class='fa fa-times-circle ' aria-hidden='true' style='font-size:24px;color:red;'></i>";}
	if($row["b2c"]=="Yes"){$nestedData[] ="<i class='fa fa-check-circle ' aria-hidden='true' style='font-size:24px;color:green;'></i>";}else{$nestedData[] ="<i class='fa fa-times-circle ' aria-hidden='true' style='font-size:24px;color:red;'></i>";}
	if($row["oda"]=="Yes"){$nestedData[] ="<i class='fa fa-check-circle ' aria-hidden='true' style='font-size:24px;color:green;'></i>";}else{$nestedData[] ="<i class='fa fa-times-circle ' aria-hidden='true' style='font-size:24px;color:red;'></i>";}
	$data[] = $nestedData;
	$i++;
}
        $json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $data   // total data array
			);

echo json_encode($json_data);  // send data as json format

?>