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

// Connect to mysqli database
$page = 1; // The current page
$sortname = 'id'; // Sort column
$sortorder = 'asc'; // Sort order
$qtype = ''; // Search column
$query = ''; // Search string
// Get posted data
if (isset($_REQUEST['page'])) {
    $page = mysqli_real_escape_string($conn, $_REQUEST['page']);
}
if (isset($_REQUEST['sortname'])) {
    $sortname = mysqli_real_escape_string($conn, $_REQUEST['sortname']);
}
if (isset($_REQUEST['sortorder'])) {
    $sortorder = mysqli_real_escape_string($conn, $_REQUEST['sortorder']);
}
if (isset($_REQUEST['qtype'])) {
    $qtype = mysqli_real_escape_string($conn, $_REQUEST['qtype']);
}
if (isset($_REQUEST['query'])) {
    $query = mysqli_real_escape_string($conn, $_REQUEST['query']);
}
if (isset($_REQUEST['rp'])) {
    $rp = mysqli_real_escape_string($conn, $_REQUEST['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');

$sortSql = "order by $sortname $sortorder";
// if($qtype="item_name"){
//     $data=getTableFieldValue('tbl_items','item_id','item_name',$query);
//     $searchSql =  " and 'item_id' ='" .$data ."'";    
// };
$sql = "SELECT count(*) from item_cost where ohc_type_id='" . $_SESSION['current_ohcttype'] . "'  $searchSql ";
// echo $sql;
error_log("query2: " . $sql);
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$total = $row[0];

if (!isset($rp)) {
    $rp = 10;
}
// Setup paging SQL
// $rp=1;
$pageStart = ($page - 1) * $rp;
error_log("pageStart: " . $pageStart);
$limitSql = "limit $pageStart, $rp";

error_log("limitSql: " . $limitSql);
// Return JSON data
$data = array();
$data['page'] = $page;
$data['total'] = $total;
error_log("total: " . $total);
$data['rows'] = array();

$sql1 = "SELECT * from item_cost where ohc_type_id='" . $_SESSION['current_ohcttype'] . "'";
// echo $sql1;
error_log("query21: " . $sql1);
$sql_export = $sql1;
$sql1 .= " $searchSql $sortSql $limitSql ";

$sql_export .= " $searchSql $sortSql ";

error_log("query1: " . $sql1);
$results = mysqli_query($conn, $sql1);
$count = ($page - 1) * $rp + 1;
// echo $sql1;
// echo $access_level;

while ($row1 = mysqli_fetch_assoc($results)) {
    extract($row1);

    $item_id = $row1['item_id'];
    $item_name = getItemWithFormName($row1['item_id']);
    $latest_cost = $row1['item_rate_latest'];
    $old_cost = $row1['item_rate_old'];
    $applicable_date = $row1['applicable_date'];

    $edit_link1 = "";
    //$delete_link="";
    $view_link1 = "";
    if ($hasWriteAccess || $hasExecuteAccess) {
        // $edit_link1 = "<a href=\"#\" class=\"blue\" onclick=\"open_item('" . $item_id . "','E');\"><i class=\"ace-icon fa fa-pencil bigger-130\"></i></a>";
    }

    if ($hasReadAccess || $hasWriteAccess || $hasExecuteAccess) {
        // $view_link1 = "<a  href=\"#\"class=\"green\"  onclick=\"open_item('" . $item_id . "','V');\"><i class=\"ace-icon fa fa-search-plus bigger-130\"></i></a>";
    }

    $space = "&nbsp;&nbsp;";
    $links = $view_link1 . $space . $edit_link1;

    $data['rows'][] = array(
        'id' => $row1['item_id'],
        'cell' => array(
            $count++,
            $item_name,
            $latest_cost,
            $old_cost,
            $applicable_date,
            $links
        )
    );
}
$data['rows'][] = array(
    'id' => $row['filterkey'],
    'cell' => array(
        '',
        "<input type=hidden name='filterkey' id='filterkey' value=\"" . base64_encode($sql_export) . "\">",
        "<input type=hidden name=paramlist id=paramlist value=\"" . base64_encode($paramlist) . "\">",
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        '',
        ''
    )
);
echo json_encode($data);
?>