<?php 
ob_start();
session_start();
include "../config/config.php";
include "../config/session.php";


$requestData = $_REQUEST;


if($requestData['length']==-1){
    $requestData['length'] = 100000;
}


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

$status = $_POST['status'];
$religion = $_POST['religion'];
$gender = $_POST['gender'];

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


$sql_query = "SELECT e.* FROM tbl_customers e";
        
/*Fetch All Data Start*/        
    	$statement = $pdo->prepare("$sql_query 
    	                            WHERE e.id!=0 ".$conditions." ORDER BY e.id DESC");
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
         $sql = "$sql_query
                 WHERE e.id!=0 ".$conditions;
         
        if( !empty($_REQUEST['search'])){   
        	$sql.=" AND ( e.customer_name LIKE '%".$_REQUEST['search']."%' ";    
        	$sql.=" OR e.unique_id LIKE '%".$_REQUEST['search']."%'  ";
        	$sql.=" OR e.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) { 
	$nestedData = array(); 
	
	$action= "<div class='text-center'><div class='btn-group'>";
	 
	 
	if($session_id==1 || $session_user_type==1 || $allPermissions['customers']['read']=="1"){
	                    $action.=" <a style='padding: 6px;margin-right: 3px;' href='modal/customers-view-modal.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>";
	}
	 
	 
	if($session_id==1 || $session_user_type==1 || $allPermissions['customers']['edit']=="1"){	                
	 	               $action.="<a style='padding: 6px;margin-right: 3px;' href='customers-edit.php?id=".$row["id"]."' title='Edit Customer' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a> ";
	}
	
	
	if($session_id==1 || $session_user_type==1 || $allPermissions['customers']['delete']=="1"){	
		      if($row["status"]==1){
		                $action.=" <button style='padding: 6px;margin-right: 3px;' title='Delete Customers' 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["customer_name"]."</div>";
	
	$nestedData[] = "<div class='text-center'>".$row["mobile_no"]."</div>";
	
	$nestedData[] = "<div class='text-center'>".$row["unique_id"]."</div>";
	
	$nestedData[] = "<div class='text-center'><div class='btn-group'>
	                    <a href='pos.php?cus_id=".$row["id"]."' class='btn btn-info btn-xs btn_smalls'><i class='fa fa-plus'></i> Add Sales&nbsp;&nbsp;</a>
	                    <a href='sales-list.php?cus_id=".$row["id"]."' class='btn btn-warning btn-xs btn_smalls'><i class='fa fa-shopping-cart'></i> View Sales&nbsp;&nbsp;</a>
	                 </div></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); 




?>