<?php
require('includes/config.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
$primary_key = 'user_id';
$call_back   =  'users';
$type = !empty ( $_REQUEST['type'] ) ? $_REQUEST['type'] : 4;


## Search 
$searchQuery = " ";
if($searchValue != ''){
	$searchQuery = " and ( ud.name like '%".$searchValue."%' or 
        u.user_name like '%".$searchValue."%' or 
        r.role_name like '%".$searchValue."%' or 
        ud.phone like '%".$searchValue."%') ";
}
## Total number of records without filtering
$sel = mysqli_query($con,"SELECT count(*) as allcount FROM users u JOIN user_details ud ON u.user_id = ud.user_id JOIN user_roles ur ON ud.user_id = ur.user_id JOIN roles r ON r.role_id = ur.role_id");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
$sel = mysqli_query($con,"SELECT count(*) as allcount FROM users u JOIN user_details ud ON u.user_id = ud.user_id JOIN user_roles ur ON ud.user_id = ur.user_id JOIN roles r ON r.role_id = ur.role_id WHERE 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];

## Fetch records
if( $type == 4 ) {
  $childQuery = "SELECT u.user_id, ud.name, u.access_token AS token, r.role_name AS type, ud.phone, u.active FROM users u JOIN user_details ud ON u.user_id = ud.user_id JOIN user_roles ur ON ud.user_id = ur.user_id JOIN roles r ON r.role_id = ur.role_id WHERE ur.role_id IN( 1,2,3,4 ) AND 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
} else {
  $childQuery = "SELECT u.user_id, ud.name, u.access_token AS token, r.role_name AS type, ud.phone, u.active FROM users u JOIN user_details ud ON u.user_id = ud.user_id JOIN user_roles ur ON ud.user_id = ur.user_id JOIN roles r ON r.role_id = ur.role_id WHERE ur.role_id = ".$type." AND 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
}

$childRecords = mysqli_query($con, $childQuery);
$data = array();
while ($row = mysqli_fetch_assoc($childRecords)) {
    ( $row['active'] == 1 ) ? $active = 'Yes': $active = 'No';
    $data[] = array(
      "user_id"   => $row['user_id'],
   		"name"      => $row['name'],            
      "type"      => $row['type'],            
   		"token"     => $row['token'],
      "phone"     => $row['phone'],
  		"active"    => $active,  		
      "edit"      => '<a href="users-details.php?edit='.$row['user_id'].'" data-extension = "'.$extension.'" data-identifier = "'.$primary_key.'" data-id = '.$row['user_id'].' data-table = "users" data-call_back = "'.$call_back.'" class = "edit"><i class="fa fa-pencil"></i></a>',
      "delete"    => '<a href="javascript:void(0);" data-extension = "'.$extension.'" data-identifier = "'.$primary_key.'" data-id = '.$row['user_id'].' data-table = "users" data-call_back = "'.$call_back.'" class = "delete"><i class="fa fa-trash"></i></a>',
   	);
}


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