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

$conditions = "";
$status = $_POST['status'];

if($status!=""){
    $conditions.=" and u.status=".$status;
}

/*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'
        );
        
/*Fetch All Data Start*/   
$sql_query = "SELECT u.*,
ut.name as user_type_name,
r.role_name, cu.name as created_user
FROM `tbl_users` u 
LEFT JOIN tbl_user_type ut ON ut.id=u.user_type_id
LEFT JOIN tbl_role r ON r.id=u.role_id
LEFT JOIN tbl_users cu ON cu.id=u.created_by";

    	$statement = $pdo->prepare("$sql_query
    	WHERE 1=1 $conditions");   
		$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 1=1 $conditions";
        if( !empty($requestData['search']['value'])){   
        	$sql.=" AND ( u.name LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR u.username LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR u.email 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']."   ";
        
        error_log("SQL: ".$sql);
        $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);



$data = array();
$i = $requestData['start']+1;
foreach( $query as $row ) { 
	$nestedData=array(); 
	
	$action="<div class='text-center'>";


    if($session_id==1 || $allPermissions['userManagement']['update']==1){
        $action.="<a href='auth-users.php?id=".$row['id']."' class='btn btn-primary btn-xs'><i class='fa fa-edit'></i></a>&nbsp;";
    }	
    if($row['id']!=1){
    if($session_id==1 || $allPermissions['userManagement']['delete']==1){   
    if($row["status"]==1){
	    $action.="<button class='btn btn-danger btn-xs' onClick='delete_Details(".$row["id"].")'><i class='fa fa-trash'></i></button>";
	}else if($row["status"]==0){
	    $action.="<button class='btn btn-danger btn-xs' onclick='HardDelete(".$row["id"].")'><i class='fa fa-trash'></i></button>";
	}
 }	
}

	$action.="</div>";
	
	
if($row["status"]==1){
	$status = "<span class='btn btn-success btn-xs' style='font-size: 12px; font-weight: 700; font-family: system-ui;'><i class='fa fa-eye'></i> Active</span>";
}else{
    $status = "<span class='btn btn-danger btn-xs' style='font-size: 12px; font-weight: 700; font-family: system-ui;'><i class='fa fa-eye-slash'></i> In-Active</span>";
}






    $nestedData[] = $action;
	$nestedData[] = $i;
	$nestedData[] = $row["name"];
	$nestedData[] = $row["email"];
	$nestedData[] = $row["password"];
	$nestedData[] = $row["user_type_name"];
	$nestedData[] = $row["role_name"];
	$nestedData[] = $status;
	$nestedData[] = $row["created_user"];
	$nestedData[] = date('d-m-Y h:i A', strtotime($row["updataed_at"]));
	

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

echo json_encode($json_data);  

?>