<?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 
        ROW_NUMBER() OVER(ORDER BY t1.id DESC) row_num,
        t1.*,
        t2.name as request_by, 
        t3.name as slip_name
    FROM request_slip t1
    INNER JOIN users t2 ON t2.employee_id=t1.request_id
    INNER JOIN users t3 ON t3.employee_id=t1.slip_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' => 'row_num', 'dt' => 'row_num'),
    array('db' => 'request_by', 'dt' => 'request_by'),
    array('db' => 'slip_name', 'dt' => 'slip_name'),
    array('db' => 'month', 'dt' => 'month'),
    array('db' => 'request_time', 'dt' => 'request_time'),
    array(
        'db' => 'status', 
        'dt' => 'status',
        'formatter' => function($cell, $row){
            $returnValue = '<span id="requestStatus'.$row['id'].'">';
            if ($row['status'] == 0) {
                $returnValue .= '<span class="text-warning">Pending</span>';
            } else if ($row['status'] == 1) {
                $returnValue .= '<span class="text-success">Approved</span>';
            } else if ($row['status'] == 2) {
                $returnValue .= '<span class="text-danger">Rejected</span>';
            }
            $returnValue .= '</span>';
            return $returnValue;
        }
    ),
    array(
        'db' => 'id', 
        'dt' => 'id',
        'formatter' => function($cell, $row){
            if ($row['status'] == 0) {
                $returnValue = '
                <div id="requestButton'.$row['id'].'">
                    <button onclick="changeStatus(\''.$row['id'].'\',1);" class="btn btn-sm btn-soft-success" title="Approve"><i class="uil uil-check-circle mb-0" style="font-size:20px;"></i></button>
                    <button onclick="changeStatus(\''.$row['id'].'\',2);" class="btn btn-sm btn-soft-danger" title="Reject"><i class="uil uil-times-circle mb-0" style="font-size:20px;"></i></button>
                </div>';

            } else if ($row['status'] == 1) {
                $returnValue = '<button type="button" class="btn btn-sm btn-soft-success" title="Approved"><i class="uil uil-check-circle mb-0" style="font-size:20px;"></i></button>';
            } else if ($row['status'] == 2) {
                $returnValue = '<button type="button" class="btn btn-sm btn-soft-danger" title="Rejected"><i class="uil uil-times-circle mb-0" style="font-size:20px;"></i></button>';
            }
            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)
);
