<?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'];
$religion = $_POST['religion'];
$gender = $_POST['gender'];

if($status!=""){
    $conditions.=" and tbl_customer_scheme.status=".$status;
}
if($religion!=""){
    $conditions.=" and c.religion='".$religion."'";
}
if($gender!=""){
    $conditions.=" and c.gender='".$gender."'";
}


        
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT tbl_customer_scheme.*,c.customer_name, c.mobile_no, tbl_schemes.name as scheme_name, 
cu.name as created_user_name, uu.name as updated_user_name,
(SELECT COUNT(id) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as payment_paid_month_counts
FROM `tbl_customer_scheme` 
LEFT JOIN tbl_customers c ON c.id=tbl_customer_scheme.customer_id
LEFT JOIN tbl_schemes ON tbl_schemes.id=tbl_customer_scheme.scheme_id
LEFT JOIN tbl_users cu ON cu.id=tbl_customer_scheme.created_by
LEFT JOIN tbl_users uu ON uu.id=tbl_customer_scheme.updated_by
    	WHERE tbl_customer_scheme.id!=0 ".$conditions." ORDER BY tbl_customer_scheme.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_customer_scheme.*,c.customer_name, c.mobile_no, tbl_schemes.name as scheme_name, 
cu.name as created_user_name, uu.name as updated_user_name,
(SELECT COUNT(id) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as payment_paid_month_counts
FROM `tbl_customer_scheme` 
LEFT JOIN tbl_customers c ON c.id=tbl_customer_scheme.customer_id
LEFT JOIN tbl_schemes ON tbl_schemes.id=tbl_customer_scheme.scheme_id
LEFT JOIN tbl_users cu ON cu.id=tbl_customer_scheme.created_by
LEFT JOIN tbl_users uu ON uu.id=tbl_customer_scheme.updated_by
    	WHERE tbl_customer_scheme.id!=0 ".$conditions;
         
        if( !empty($_REQUEST['search']) ) 
        {   
        	$sql.=" AND ( c.customer_name LIKE '%".$_REQUEST['search']."%' ";  
        	$sql.=" OR tbl_customer_scheme.scheme_no LIKE '%".$_REQUEST['search']."%' ";
        	$sql.=" OR c.mobile_no 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/users-view.php?id=".$row["id"]."' data-toggle='ajax-modal' title='View Details' class='tip btn btn-primary btn-xs'>
		                    <i class='fa fa-eye'></i>
		                </a>";
		                
	 	               $action.="<a style='padding: 6px;margin-right: 3px;' href='users-edit.php?id=".$row["id"]."' title='Edit Users' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
		                
		                $action.="<a style='padding: 6px;margin-right: 3px;' href='scheme-payment-add.php?id=".$row["id"]."' title='Payments' class='tip btn btn-success btn-xs'>
		                    <i class='fa fa-inr' style='width: 14px;font-size: 12px;'></i>
		                </a> ";
		   if($row['status']==1){
		       $action.=" <button style='padding: 6px;margin-right: 3px;' title='Disable Customers' onClick='delete_Details(".$row["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
		   }else if($row['status']==0){
		       $action.=" <button style='padding: 6px;margin-right: 3px;' title='Delete Customers' onClick='delete_Hard_Details(".$row["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
		   }            
		                

		                
	$action.="</div></div>";
	
	
	$payment_pending_months = $row["scheme_months"]-$row["payment_paid_month_counts"];
	$nestedData[] = $action;	
	$nestedData[] = "<div class='text-center'>".$i."</div>";
	$nestedData[] = "<div class='text-center'>".$row["customer_name"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row["mobile_no"]."</div>";
	$nestedData[] = "<div class='text-center'>".$row['scheme_no']."</div>";
	$nestedData[] = "<div class='text-center'><span class='btn btn-primary btn-xs'>".$row["scheme_name"]."</span></div>";
	$nestedData[] = "<div class='text-center'>".$row["scheme_months"]."</div>";
	
	$nestedData[] = "<div class='text-center'>".$row["payment_paid_month_counts"]."</div>";
	$nestedData[] = "<div class='text-center'>".$payment_pending_months."</div>";
	
	
	
	if($row["gender"]=="Male"){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-info btn-xs'>Male</span></div>";
	}else if($row["gender"]=="Female"){
	    $nestedData[] ="<div class='text-center'><span class='btn btn-warning btn-xs'>Female</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); 




?>