<?php
session_start();
require_once '../../Server/connection.php';
$staff_id = $_SESSION['staff_id'];
## 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
//$searchByAssetName = $_POST['searchByAssetName'];
$searchByLeaveType = $_POST['searchByLeaveType'];
//$searchByStaffCurrent = 1;
## Search 




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

$searchQuery = " ";
//if($searchByAssetName != ''){
    //$searchQuery .= " and (assets.asset_name like '%".$searchByAssetName."%') ";
//}




if($searchByLeaveType != ''){
    $searchQuery .= " and (leave_requests.leaveType_id='".$searchByLeaveType."') ";
}




//if($searchValue != ''){ 
    //$searchQuery .= " and (assets.asset_name like '%".$searchValue."%')";
//}

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



## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from leave_requests,leavestatus where leave_requests.leave_requestCode=leavestatus.leave_requestCode and leave_requests.staff_id='$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 leave_requests,leavestatus WHERE leave_requests.leave_requestCode=leavestatus.leave_requestCode and leave_requests.staff_id='$staff_id' and 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
$empQuery = "select * from leave_requests,leavestatus WHERE leave_requests.leave_requestCode=leavestatus.leave_requestCode and leave_requests.staff_id='$staff_id' and 1 ".$searchQuery." order by leave_requests.leaveType_id  limit ".$row.",".$rowperpage;
//$empQuery = "select * from collected_payments,invoices WHERE collected_payments.inv_id=invoices.inv_id 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['leave_requestId'];
	$staffRemarks = $row['staff_remarks'];
	$daysRequested = $row['leave_days_requested'];
	$leaveType_id = $row['leaveType_id'];
	$hodStatus = $row['hod_status'];
	$hodRemarks = $row['hod_remarks'];
	$hrStatus = $row['hr_status'];
	$hrRemarks = $row['hr_remarks'];
	$m = $row['month'];
	$y = $row['year'];
	
	$month = $m.' '.$y;
	
	if($hodStatus==0){
	$hdStatus = 'Pending Approval Wait';	
	}else if($hodStatus==1){
	$hdStatus = 'Approved Fowarded To Hr';	
	}else{
	$hdStatus = 'Rejected';		
	}
	
	
	if($hrStatus==0){
	$hr_Status = 'Pending Approval Wait';	
	}else if($hrStatus==1){
	$hr_Status = 'Approved';	
	}else{
	$hr_Status = 'Rejected';		
	}
	
	$query154 = "select * from leave_type where leaveType_id='$leaveType_id'";
$result154 = $conn->query($query154);
$row154 = $result154->fetch_array();
$leaveType = $row154['type'];
	
	
	$button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="editLeaveRequest.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-edit"></i></center></a>
	</div>';
	
	
	$button2 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="pdf_receipt.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	
	//$button1 = '<!-- Single button -->
	//<div class="btn-group">
	    //<a type="button" class="btn btn-primary" href="uploaded_Docs.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(
    		"leave_requestId"=>$x,
			"leaveType"=>$leaveType,
			"daysReq"=>$daysRequested,
			"leaveReason"=>$staffRemarks,
			"hodApproval"=>$hdStatus,
			"hodRemarks"=>$hodRemarks,
			"hrApproval"=>$hr_Status,
			"hrRemarks"=>$hrRemarks,
			"month"=>$month,
			"edit"=>$button
			//"delete"=>$button4
    	);
		$x++;
		
	
		
		
		
}




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

echo json_encode($response);
