<?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
$searchByClientName = $_POST['searchByClientName'];
$searchByClientType = $_POST['searchByClientType'];
$searchByClientLocation = $_POST['searchByClientLocation'];
//$searchBystatusName = $_POST['searchBystatusName'];
//$searchByStaffCurrent = 1;
## Search 

$searchQuery = " ";
if($searchByClientName != ''){
    $searchQuery .= " and (client_fname like '%".$searchByClientName."%' or client_mname like '%".$searchByClientName."%' or client_lname like '%".$searchByClientName."%' or client_email like '%".$searchByClientName."%' or client_tin like '%".$searchByClientName."%') ";
}




if($searchByClientType != ''){
    $searchQuery .= " and (client_type='".$searchByClientType."') ";
}

if($searchByClientLocation != ''){
    $searchQuery .= " and (client_source='".$searchByClientLocation."') ";
}



if($searchValue != ''){ 
    $searchQuery .= " and (client_fname like '%".$searchValue."%' or client_mname like '%".$searchValue."%' or client_lname like '%".$searchValue."%' or client_email like '%".$searchValue."%' or client_tin like '%".$searchValue."%')";
}

//if($searchByStaffCurrent != ''){
    //$searchQuery .= " and (status_id='".$searchByStaffCurrent."') ";
//}



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

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

## Fetch records
$empQuery = "select * from clients WHERE 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['client_id'];
	$email = $row['client_email'];
	$phoneNum = $row['client_phone'];
	
	$phoneNumber = substr($phoneNum, 3);
	
	$pNumber = '0'.$phoneNumber;
	
		$button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" data-toggle="modal"  data-target="#MyModal" data-id='.$productId.' id="getUser11"> <i class="glyphicon glyphicon-eye-open"></i></a>
	</div>';
	
	
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" data-toggle="modal" id="editCategoriesModalBtn" data-target="#editCategoriesModal" onclick="editCategories('.$productId.')"> <i class="glyphicon glyphicon-edit"></i></a>
	</div>';
	
	$button2 = '<!-- 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(
    		"client_id"=>$x,
			"Client"=>$row['client_fname'].'&nbsp&nbsp'.$row['client_mname'].'&nbsp&nbsp'.$row['client_lname'],
			"PhoneNumber"=>$pNumber,
			"Email"=>$email,
    		"edit"=>$button1,
			"delete"=>$button2
    	);
		$x++;
		
		
}

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

echo json_encode($response);
