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

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

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



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

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 (client_files.file_no 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");
$sel = mysqli_query($conn,"select count(*) as allcount from client_files,assignstatus where client_files.file_id=assignstatus.file_id");
$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);
$sel = mysqli_query($conn,"select count(*) as allcount from client_files,assignstatus WHERE client_files.file_id=assignstatus.file_id and 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;
$empQuery = "select client_files.file_id,client_files.file_no,client_files.fileStatus,client_files.client_id,assignstatus.remarks,assignstatus.assigned_by from client_files,assignstatus WHERE client_files.file_id=assignstatus.file_id and assignstatus.status=0 and 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'];
	$clientId = $row['client_id'];
	$fileStatus = $row['fileStatus'];
	$remarks = $row['remarks'];
	$assigned_by = $row['assigned_by'];
	$fileNo = $row['file_no'];
	$space = " ";
	
	
	$sql812 = "SELECT * FROM clients where client_id='$clientId'";
			$result812 = $conn->query($sql812);
				while($data812 = mysqli_fetch_array($result812))
							{
							$clientfName = $data812['client_fname'].$space.$data812['client_lname'];
                            $clientTin = $data812['client_tin']; 							
							}
							
							
	$sql81215 = "SELECT * FROM staff,users,designation where users.user_id='$assigned_by' and staff.staff_id=users.staff_id and staff.des_id=designation.des_id";
			$result81215 = $conn->query($sql81215);
				while($data81215 = mysqli_fetch_array($result81215))
							{
							$role = $data81215['firstname']." ".$data81215['lastname']." ".$data81215['des_short_name']; 
							}
	
	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="updateCustFile.php?i='.$categoriesId.'&assignId='.$searchByAssignId.'" 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="uploadedFls.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_file.php?i='.$categoriesId.'" id="editOrderModalBtn"> <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"><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>';
	
	//$button5 = '<!-- Single button -->
	//<div class="btn-group">
	    //<a type="button" class="btn btn-primary" href="emailClient.php?i='.$categoriesId.'" id="editOrderModalBtn"><center>Email Client </center></a>
	//</div>';
	
	
    $data[] = array(
    		"file_id"=>$x,
			"FileNo"=>$fileNo,
			"clientName"=>$clientfName,
			"FileStatus"=>$st,
			"FowardedBy"=>$role,
			"Remarks"=>$remarks,
			"edit"=>$button,
			"docs"=>$button1,
    		//"pdfFile"=>$button2,
			//"delete"=>$button4
    	);
		$x++;
		
		
}

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

echo json_encode($response);
