<?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
$searchByStaffName = $_POST['searchByStaffName'];
$searchBydeptName = $_POST['searchBydeptName'];
$searchBysectName = $_POST['searchBysectName'];
$searchBydesName = $_POST['searchBydesName'];
$searchBystaffCate = $_POST['searchBystaffCate'];
$searchByGender = $_POST['searchByGender'];
//$searchByStaffCurrent = 1;
## Search 

$searchQuery = " ";
if($searchByStaffName != ''){
    $searchQuery .= " and (staff.firstname like '%".$searchByStaffName."%' or staff.middlename like '%".$searchByStaffName."%' or staff.lastname like '%".$searchByStaffName."%' or staff.email like '%".$searchByStaffName."%') ";
}




if($searchBydeptName != ''){
    $searchQuery .= " and (staff.dept_id='".$searchBydeptName."') ";
}

if($searchBysectName != ''){
    $searchQuery .= " and (staff.sect_id='".$searchBysectName."') ";
}

if($searchBydesName != ''){
    $searchQuery .= " and (staff.des_id='".$searchBydesName."') ";
}


if($searchBystaffCate != ''){
    $searchQuery .= " and (staff.staffcate_id='".$searchBystaffCate."') ";
}



if($searchByGender != ''){
    $searchQuery .= " and (staff.gender_id='".$searchByGender."') ";
}

if($searchValue != ''){ 
    $searchQuery .= " and (staff.firstname like '%".$searchValue."%' or staff.middlename like '%".$searchValue."%' or staff.lastname like '%".$searchValue."%' or staff.email like '%".$searchValue."%' or staff.phone_number like '%".$searchValue."%')";
}

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



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

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

## Fetch records
$empQuery = "select staff.firstname,staff.lastname,staff.staff_id,staff_permission.perm_date,staff_permission.permend_date,staff_permission.duration,staff_permission.remarks from staff,staff_permission WHERE staff.staff_id=staff_permission.staff_id and 1 ".$searchQuery." order by staff.firstname";
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$x = 1;
while ($row = mysqli_fetch_assoc($empRecords)){
	
	$productId = $row['staff_id'];
	$permDate = $row['perm_date'];
	$permEndDate = $row['permend_date'];
	$duration = $row['duration'];
	$remarks = $row['remarks'];
	$firstname = $row['firstname'];
	$lastname = $row['lastname'];
	$permissionDate = date('d-m-Y', strtotime($permDate));
	$permissionEndDate = date('d-m-Y', strtotime($permEndDate));
	
	
	$button3 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="permstaffDocs.php?i='.$productId.'" 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_Stafffile.php?i='.$productId.'" 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="#removeProductModal" id="removeProductModalBtn" onclick="removeProduct('.$productId.')"> <i class="glyphicon glyphicon-trash"></i></a>
	
	</div>';
	
	
    $data[] = array(
    		"staff_id"=>$x,
			"Staff"=>$row['firstname'].'&nbsp&nbsp'.$row['lastname'],
			"permDate"=>$permissionDate,
			"permEndDate"=>$permissionEndDate,
			"Duration"=>$duration,
			"remarks"=>$remarks,
			"staffdocs"=>$button3,
			"staffFile"=>$button2
    	);
		$x++;
		
		
}

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

echo json_encode($response);
