<?php
require_once '../../Server/connection.php';

## Read value
$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = $_POST['search']['value']; // Search value

## Custom Field value
$searchByPermName = $_POST['searchByPermName'];
$searchByroleName = $_POST['searchByroleName'];
$searchByuserName = $_POST['searchByuserName'];
$searchByStaffCurrent = 1;
## Search 

$searchQuery = " ";
if($searchByPermName != ''){
    $searchQuery .= " and (permissions.permission_name like '%".$searchByPermName."%') ";
}


if($searchByroleName != ''){
    $searchQuery .= " and (assigned_permissions.role_id='".$searchByroleName."') ";
}

if($searchByuserName != ''){
    $searchQuery .= " and (assigned_permissions.user_id='".$searchByuserName."') ";
}



if($searchValue != ''){ 
    $searchQuery .= " and (permissions.permission_name like '%".$searchValue."%')";
}



## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from permissions,assigned_permissions where permissions.permission_id=assigned_permissions.permission_id");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];



## Total number of records with filtering
$sel = mysqli_query($conn,"select count(*) as allcount from permissions,assigned_permissions WHERE permissions.permission_id=assigned_permissions.permission_id and 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
$empQuery = "select permissions.permission_name,permissions.action,assigned_permissions.assign_id,assigned_permissions.user_id,assigned_permissions.role_id from permissions,assigned_permissions WHERE permissions.permission_id=assigned_permissions.permission_id and 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
$empRecords = mysqli_query($conn, $empQuery);
$num = mysqli_num_rows($empRecords);
$data = array();
$x = 1;
while ($row = mysqli_fetch_assoc($empRecords)){
	
	$productId = $row['assign_id'];
	$roleId = $row['role_id'];
	$userId = $row['user_id'];
	$permName = $row['permission_name'];
	$permAction = $row['action'];
	
		 $empQuery13 = "select * from users WHERE user_id='$userId'";
    $empRecords13 = mysqli_query($conn, $empQuery13);
    while ($row13 = mysqli_fetch_assoc($empRecords13)){
		$staff_id = $row13['staff_id'];
	         }	
			 
	$empQuery1321 = "select * from staff WHERE staff_id='$staff_id'";
    $empRecords1321 = mysqli_query($conn, $empQuery1321);
    while ($row1321 = mysqli_fetch_assoc($empRecords1321)){
		$staffName = $row1321['firstname'].'&nbsp&nbsp'.$row1321['lastname'];
	         }	
			 
			
			  $empQuery132 = "select * from roles WHERE role_id='$roleId'";
    $empRecords132 = mysqli_query($conn, $empQuery132);
    while ($row132 = mysqli_fetch_assoc($empRecords132)){
		$role_name22 = $row132['role_name'];
	         }

	
	
	
	$button = '<!-- Single button -->
	<div class="btn-group">
	<a type="button" class="btn btn-primary" data-toggle="modal" data-target="#removeProductModal" id="removeProductModalBtn" onclick="removeProduct('.$productId.')"> <i class="glyphicon glyphicon-trash"></i></a>
	
	</div>';
	
	
	
	
	
    $data[] = array(
    		"perm_id"=>$x,
			"Permission Name"=>$permName,
			"Action"=>$permAction,
			"UserName"=>$staffName,
			"RoleName"=>$role_name22,
    		"delete"=>$button
    	);
		$x++;
		
		
}

## Response
$response = array(
    "draw" => intval($draw),
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
    "aaData" => $data
);

echo json_encode($response);
