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

$requestData= $_REQUEST;


/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'name', 
            2 =>'email',
            3 =>'password',
            4 =>'phone',
            5 =>'address',
            6 =>'role',
            7 =>'role_id',
            8 =>'id',
            9 =>'id'
           
        );
        
$conditions="";

$status = $_POST['status'];
$scheme_type = $_POST['scheme_type'];

if($status!=""){
    $conditions.=" and tbl_schemes.status=".$status;
}
if($scheme_type!=""){
    $conditions.=" and tbl_schemes.type=".$scheme_type;
}



        
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT tbl_schemes.*
        FROM `tbl_schemes` 
    	WHERE tbl_schemes.id!=0 ".$conditions." ORDER BY tbl_schemes.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_schemes.*
                FROM `tbl_schemes` 
    	        WHERE tbl_schemes.id!=0 ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( tbl_schemes.name LIKE '%".$_REQUEST['search']."%' ) ";
        }
        
/*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=1;
foreach( $query as $row ) {  // preparing an array
	$nestedData=array(); 
	$action= "<div class='text-center'><div class='btn-group'>";
	 
		                
	 	               $action.="<a style='padding: 6px;margin-right: 3px;' href='modal/scheme-edit.php?id=".$row["id"]."' data-toggle='ajax-modal' title='Edit Scheme Details' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		         
		   if($row["status"]==1){	                
		                $action.=" <button style='padding: 6px;margin-right: 3px;' title='Delete Scheme' onClick='delete_Details(".$row["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
        	}           
		                

		                
	$action.="</div></div>";
	
	
	
	
	$nestedData[] = $action;	
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".$row["name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["month"]."</div>";
	
	if($row["type"]==1){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-primary btn-xs'> Payment Type</span></div>";
	}else if($row["type"]==2){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-info btn-xs'> Weight Type</span></div>";
	}
	
	
	

	
	if($row["status"]==1){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-success btn-xs'><i class='fa fa-eye'></i> Active</span></div>";
	}else if($row["status"]==0){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-danger btn-xs' onclick='DynamicActive(".$row["id"].")'><i class='fa fa-eye-slash'></i> In-Active</span></div>";
	}
        	
        	
	$data[] = $nestedData;
	$i++;
	

}

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

echo json_encode($json_data); 




?>