<?php  
include('includes/config/config.php');
include('includes/auth/auth.php');
include('includes/functions.php');include('access.php');
// error_reporting(E_ERROR | E_PARSE);
include('log_entry.php');
?>
<?php  

// Connect to mysqli database
$page = 1; // The current page
$sortname = 'frequency_name'; // Sort column
$sortorder = 'asc'; // Sort order
$qtype = ''; // Search column
$query = ''; // Search string
// Get posted data
if (isset($_POST['page'])) {
        $page = @mysqli_real_escape_string($conn,$_POST['page']);
}
if (isset($_POST['sortname'])) {
        $sortname = @mysqli_real_escape_string($conn,$_POST['sortname']);
}
if (isset($_POST['sortorder'])) {
        $sortorder = @mysqli_real_escape_string($conn,$_POST['sortorder']);
}
if (isset($_POST['qtype'])) {
        $qtype = @mysqli_real_escape_string($conn,$_POST['qtype']);
}
if (isset($_POST['query'])) {
        $query = @mysqli_real_escape_string($conn,$_POST['query']);
}
if (isset($_POST['rp'])) {
        $rp = @mysqli_real_escape_string($conn,$_POST['rp']);
}
// Setup sort and search SQL using posted data
$hasReadAccess = isAccessible($_SESSION['RoleId'],$menu_key,'R');
$hasWriteAccess = isAccessible($_SESSION['RoleId'],$menu_key,'W');
$hasExecuteAccess = isAccessible($_SESSION['RoleId'],$menu_key,'E');

if($qtype=='frequency_name'){
    $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%" . trim ( $query ) . "%')" : '';
    error_log("patient name.......".$searchSql);
}else if($qtype=='frequency_code'){
    $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%" . trim ( $query ) . "%')" : '';
    error_log("patient name.......".$searchSql);
}else{
    $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%" . trim ( $query ) . "%')" : '';
    
}

$sortSql = "order by $sortname $sortorder";
//$searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%$query%')" : '';
// Get total count of records
$sql = "select count(*)from shift_status_details where current_status='R' and ohc_location_id='".$_SESSION['current_ohcttype']."' $searchSql";
$result = @mysqli_query($conn,$sql);
$row = @mysqli_fetch_array($result);
$total = $row[0];
// Setup paging
if(!isSet($rp)){
	$rp=10;
}
$pageStart = ($page-1)*$rp;
$limitSql = "limit $pageStart, $rp";
// Return JSON data
$data = array();
$data['page'] = $page;
$data['total'] = $total;
$data['rows'] = array();
$sql_shift = "select * from shift_status_details where current_status='R' and ohc_location_id='".$_SESSION['current_ohcttype']."'";
error_log($sql_shift);
$results_shift = @mysqli_query($conn,$sql_shift);
$count=($page-1)*$rp+1;

//echo $sql_frequency;
//echo $access_level;

while ($row = @mysqli_fetch_assoc($results_shift)) {

$shift=getFieldFromTable('status_name', 'shift_status', 'shift_status_id', $row['shift_id']) ;
error_log("shift".$row['shift_id']);
//echo $frequency_id;
if($row['current_status']=='R'){
    $status="RUNNING";
}

$data['rows'][] = array(
'id' => $row['id'],
'cell' => array($count++, $shift, $row['start_date_time'],$status )
);
}
// error_log(print_r($data, true));
echo json_encode($data);
?>