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

// Connect to mysqli database
$page = 1; // The current page
$sortname = 'a.last_modified'; // Sort column
$sortorder = 'desc'; // 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');
$sortSql = "order by $sortname $sortorder";

if ($qtype == 'dept_name') {
    $searchSql = " where dept_id  in (select dept_id from department where dept_name like '%$query%') ";
} else {
    $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%$query%')" : '';
}

$ohc_type = $_SESSION['current_ohctype'];
// Get total count of records
$sql = "SELECT count(distinct(a.response_id)) from questionaire_master_response a left join questionaire_master_response_details b on a.response_id = b.response_id left join patient_master c on a.patient_id = c.id WHERE a.form_type IN ('chs','khs') AND a.approval_status = 'Y' AND a.f_status = 'CLS' $searchSql";

//echo $sql;
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$total = $row[0];
// Setup paging
if (!isset($rp)) {
    $rp = 10;
}
//echo $_REQUEST[emp_id];
$pageStart = ($page - 1) * $rp;
$limitSql = "limit $pageStart, $rp";
// Return JSON data
$data = array();
$data['page'] = $page;
$data['total'] = $total;
$data['rows'] = array();
$sql_medical_examination = "SELECT a.* from questionaire_master_response a left join questionaire_master_response_details b on a.response_id = b.response_id WHERE a.form_type IN ('chs','khs') AND a.approval_status = 'Y' AND a.f_status = 'CLS'  group by a.response_id $searchSql $sortSql $limitSql";

error_log("chs grid " . $sql_medical_examination);
$results_medical_examination = mysqli_query($conn, $sql_medical_examination);
$count = ($page - 1) * $rp + 1;

//echo $sql_checkup;
//echo $access_level;

while ($row_medical_examination = mysqli_fetch_assoc($results_medical_examination)) {


    $chs_no = $row_medical_examination['medical_examination_no'];
    $form_type = $row_medical_examination['form_type'];
    error_log("chs grid--------".$chs_no);
    $response_id = $row_medical_examination['response_id'];
    error_log("responseID::::: ". $row_medical_examination['response_id']);
    $view_link = "";
    
    $edit_link = "";
    $delete_link = "";
    $pdf_link = "";
    $cirtificate="";
    $links = "";


    if ($hasWriteAccess) {
        if ($form_type == 'chs') {    
        $edit_link = "<a href=\"#\" class=\"blue\" onclick=\"open_chs_form('" . $response_id . "','" . $chs_no . "');\"><span  style=\"font-size:12px\" class=\"glyphicon glyphicon-edit\"></span></a>";

        $pdf_link = "<a href=\"#\" class=\"red\" onclick=\"open_form_chs_pdf('" . $response_id . "','" . $chs_no . "');\"><i class=\"fa fa-file-pdf-o\" style=\"font-size: 12px;\"></i></a>";
        }elseif ($form_type == 'khs') {
                
                $edit_link = "<a href=\"#\" class=\"blue\" onclick=\"open_khs_form('" . $response_id . "','" . $chs_no . "');\"><span  style=\"font-size:12px\" class=\"glyphicon glyphicon-edit\"></span></a>";

        $pdf_link = "<a href=\"#\" class=\"red\" onclick=\"open_form_khs_pdf('" . $response_id . "','" . $chs_no . "');\"><i class=\"fa fa-file-pdf-o\" style=\"font-size: 12px;\"></i></a>";
        }
        if($row_medical_examination['approval_status']=='Y'){
            $cirtificate = "<a href=\"#\" class=\"green\" onclick=\"open_form_chs_cirtificate('" . $response_id . "','" . $chs_no . "');\"><i class=\"fa fa-file-pdf-o\" style=\"font-size: 12px;\"></i></a>";
        }
    }
    if ($hasExecuteAccess) {
if ($form_type == 'khs') {
    $delete_link = "<a href=\"#\" class=\"blue\" onclick=\"delete_khs_form('" . $response_id . "','" . $chs_no . "');\"><span  style=\"font-size:20px\" class=\"ace-icon fa fa-trash bigger-130\"></span></a>";

}elseif($form_type == 'chs'){

        $delete_link = "<a href=\"#\" class=\"blue\" onclick=\"delete_chs_form('" . $response_id . "','" . $chs_no . "');\"><span  style=\"font-size:20px\" class=\"ace-icon fa fa-trash bigger-130\"></span></a>";
}
    }


if ($form_type == 'chs') {
    $type = "Canteen Hygiene";
}elseif ($form_type == 'khs') {
    $type = "Kitchen Hygiene";
}



    $space = "&nbsp;&nbsp;&nbsp;";
    $links = $assign_link . $space . $view_link . $space . $edit_link . $space . $delete_link . $space . $pdf_link.$space . $cirtificate;
   
    $data['rows'][] = array(
        'id' => $row_medical_examination['response_id'],

        'cell' => array($count++,  $row_medical_examination['canteen_location'], $type ,$row_medical_examination['screen_date'], $links)
    );
}
echo json_encode($data);
?>