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


if($searchByClientSource==1){
	$searchByClient_Source = 1;
}else{
	$searchByClient_Source = 0;
}

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




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

if($searchByClientSource != ''){
    $searchQuery .= " and (client_source_id='".$searchByClient_Source."') ";
}




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

//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)){
	
	$categoriesId = $row['client_id'];
	$clientcate_id  = $row['clientcate_id'];
	$clientTin = $row['client_tin'];
	$space = " ";
	
	if($clientcate_id==1){
	$clientName = $row['client_fname'].$space.$row['client_lname'];	
	}else{
	$clientName = $row['client_fname'];	
	}
	
	
	$button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="editCustomer.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-edit"></i></center></a>
	</div>';
	
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="uploadedCustDocs.php?i='.$categoriesId.'" id="editOrderModalBtn"><center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	$button2 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="pdf_Customerfile.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	
	
	$button4 = '<!-- Single button -->
	<div class="btn-group">
	<a type="button" class="btn btn-primary" data-toggle="modal" data-target="#removeCategoriesModal" id="removeCategoriesModalBtn" onclick="removeCategories('.$categoriesId.')"> <i class="glyphicon glyphicon-trash"></i></a>
	
	</div>';
	
	
    $data[] = array(
    		"client_id"=>$x,
			"clientName"=>$clientName,
			"clientTin"=>$clientTin,
			"edit"=>$button,
			"docs"=>$button1,
    		"pdfFile"=>$button2,
			//"ClientFile"=>$button3,
			"delete"=>$button4
    	);
		$x++;
		
		
}

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

echo json_encode($response);
