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

if($searchByClientFileStatus=='Pending Files'){
	$searchByClientFStatus = 0;
}else{
	$searchByClientFStatus = 1;
}

$searchQuery = " ";
if($searchByClientFile != ''){
    $searchQuery .= " and (file_no like '%".$searchByClientFile."%' or tra_RefNo like '%".$searchByClientFile."%') ";
}




if($searchByClientName != ''){
    $searchQuery .= " and (client_id='".$searchByClientName."') ";
}

if($searchByClientFileStatus != ''){
    $searchQuery .= " and (fileStatus='".$searchByClientFStatus."') ";
}



if($searchValue != ''){ 
    $searchQuery .= " and (file_no like '%".$searchValue."%' or tra_RefNo like '%".$searchValue."%')";
}

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



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

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

## Fetch records
$empQuery = "select * from client_files 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['file_id'];
	$fileStatus = $row['fileStatus'];
	
	
	if($fileStatus==0){
		$st = "<span class='badge badge-info'>File Pending</span>";
	}else{
		$st = "<span class='badge badge-success'>File Closed</span>";
	 }
	
	
    $button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="editClientFile.php?i='.$categoriesId.'" id="editOrderModalBtn">  Edit </br><center><i class="glyphicon glyphicon-eye-edit"></i></center></a>
	</div>';
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="uploadedDocs.php?i='.$categoriesId.'" id="editOrderModalBtn">  View </br><center><i class="glyphicon glyphicon-eye-edit"></i></center></a>
	</div>';
	
	
	$button2 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="pdf_file.php?i='.$categoriesId.'" id="editOrderModalBtn">  pdf Client File </br><center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	
	
	$button3 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="viewClientFiles.php?i='.$categoriesId.'" id="editOrderModalBtn">  View  </br><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(
    		"file_id"=>$x,
			"FileStatus"=>$st,
    		"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);
