<?php
require_once('../config.php');
$user_id = $_SESSION['user_id'];
$emp_id = $_SESSION['emp_id'];
$user_role = $_SESSION['user_role'];


// SQL server connection information
// $sql_details = $search_db;
$sql_details = $search_db;

if(!isset($_GET['isset'])){
    exit();
}


$table = <<<EOT
(
    SELECT 
        id,
        issue_date,
        issue_note,
        created_at,
        issue_status,
        request_for,
        remark
    FROM issue_with_date WHERE employee_id='$emp_id'
) join_table
EOT;


$whereResult = null;
$whereAll = null;
if (isset($_GET['where_condition']) && !empty($_GET['where_condition'])) {
    $whereAll = $_GET['where_condition'];
}

// Table's primary key
$primaryKey = 'id';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array('db' => 'id', 'dt' => 'id'),
    array('db' => 'issue_date', 'dt' => 'issue_date'),
    array(
        'db' => 'issue_note', 
        'dt' => 'issue_note',
        'formatter' => function($cell, $row){
            $returnValue = '<div style="min-width:300px; white-space: pre-wrap;">'.$row['issue_note'].'</div>';
            return $returnValue;
        }
    ),
    array('db' => 'request_for', 'dt' => 'request_for'),
    array(
        'db' => 'created_at', 
        'dt' => 'created_at',
        'formatter' => function($cell, $row){
            $returnValue = date("h:i A - d M Y", strtotime($row['created_at']));
            return $returnValue;
        }
    ),
    array(
        'db' => 'issue_status', 
        'dt' => 'issue_status',
        'formatter' => function($cell, $row){
            if ($row['issue_status'] == 0) {
                $returnValue = '<span class="text-warning">Pending</span>';
            } else if ($row['issue_status'] == 1) {
                $returnValue = '<span class="text-success">Approved</span>';
            } else if ($row['issue_status'] == 2) {
                $returnValue = '<span class="text-danger">Rejected</span>';
            }
            return $returnValue;
        }
    ),
    array(
        'db' => 'remark', 
        'dt' => 'remark',
        'formatter' => function($cell, $row){
            $returnValue = '<div style="min-width:300px; white-space: pre-wrap;">'.$row['remark'].'</div>';
            return $returnValue;
        }
    ),
    
);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */
require('ssp.class.php');

echo json_encode(
    // SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns)
    SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, $whereResult, $whereAll)
);
