<?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
//$searchBy_financial_quarter = $_POST['searchBy_financial_quarter'];
$searchByDeptName = $_POST['searchByDeptName'];
$searchByDesignation = $_POST['searchByDesignation'];
$searchBySectName = $_POST['searchBySectName'];
## Search 

$searchQuery = " ";
if($searchByDesignation != ''){
    $searchQuery .= " and (des_name like '%".$searchByDesignation."%') ";
}

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


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


//if($searchBy_financial_quarter != ''){
    //$searchQuery .= " and (financial_quarter='".$searchBy_financial_quarter."') ";
//}

if($searchValue != ''){ 
    $searchQuery .= " and (des_name like '%".$searchValue."%' or dept_id like '%".$searchValue."%' or sect_id like '%".$searchValue."%')";
}



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

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

## Fetch records
$empQuery = "select * from designation 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['des_id'];
	
		
		$button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" data-toggle="modal"  data-target="#MyModal" data-id='.$categoriesId.' id="getUser11"> <i class="glyphicon glyphicon-eye-open"></i></a>
	</div>';
	
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" data-toggle="modal" id="editCategoriesModalBtn" data-target="#editCategoriesModal" onclick="editCategories('.$categoriesId.')"> <i class="glyphicon glyphicon-edit"></i></a>
	</div>';
	
	$button2 = '<!-- 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(
    		"des_id"=>$x,
			"Designation"=>$row['des_name'],
			"ShortName"=>$row['des_short_name'],
            "view"=>$button,
    		"edit"=>$button1,
			"delete"=>$button2
    	);
		$x++;
		
		
}

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

echo json_encode($response);
