<?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'];
## 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 assigned_permissions.assign_id,assigned_permissions.role_id,assigned_permissions.user_id,permissions.action,permissions.permission_name 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);
$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_id3 = $row13['staff_id'];
	         }	
			 
	$empQuery134 = "select * from staff WHERE staff_id='$staff_id3'";
    $empRecords134 = mysqli_query($conn, $empQuery134);
    while ($row134 = mysqli_fetch_assoc($empRecords134)){
		$userName = $row134['firstname'].'&nbsp&nbsp'.$row134['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>';
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="editPermForm.php?i='.$productId.'" id="editOrderModalBtn">  <i class="glyphicon glyphicon-edit"></i> </a>
	</div>';
	
	
	
    $data[] = array(
    		"assign_id"=>$x,
			"PermName"=>$permName,
			"Action"=>$permAction,
			"UserName"=>$userName,
			"RoleName"=>$role_name22,
    		"delete"=>$button
			//"edit"=>$button1
    	);
		$x++;
		
		
}

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

echo json_encode($response);
