<?php
session_start();
require_once '../../Server/connection.php';
include('../includes/session.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'];
$searchByClientFileStatus = $_POST['searchByClientFileStatus'];
$searchByEntryPointType = $_POST['searchByEntryPointType'];
$searchByEntryPointName = $_POST['searchByEntryPointName'];
//$searchByStaffCurrent = 1;
## Search 

$sql22 = "Select * from clients where client_id ='$staff_id'";
	            $result22 = $conn->query($sql22);
	            $row22 = $result22->fetch_array();
                $client_id = $row22['client_id'];  


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

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



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


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


if($searchByEntryPointType != ''){
    $searchQuery .= " and (entryId='".$searchByEntryPointType."') ";
}


if($searchByEntryPointName != ''){
    $searchQuery .= " and (entryName_id	='".$searchByEntryPointName."') ";
}



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


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



## 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'];
	$fileNo = $row['file_no'];
	
	if($fileStatus==0){
		$st = "<span class='badge badge-info'>File On Progress</span>";
	}else{
		$st = "<span class='badge badge-success'>File Closed</span>";
	 }
	
	
	
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="yourDocs.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="pdfCustfile.php?i='.$categoriesId.'" id="editOrderModalBtn"><center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	
	
    $data[] = array(
    		"file_id"=>$x,
			"FileNo"=>$fileNo,
			"FileStatus"=>$st,
			"docs"=>$button1,
    		"pdfFile"=>$button2
    	);
		$x++;
		
		
}

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

echo json_encode($response);
