<?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'];
$searchBySection = $_POST['searchBySection'];
## Search 

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

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


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

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



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

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

## Fetch records
$empQuery = "select sections.dept_id,sections.sect_name,sections.sect_id from sections WHERE sect_name!='NO SECTION' 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['sect_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(
    		"sect_id"=>$x,
			"Section"=>$row['sect_name'],
            "view"=>$button,
    		"edit"=>$button1,
			"delete"=>$button2
    	);
		$x++;
		
		
}

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

echo json_encode($response);
