<?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 
        t1.*, 
        str_to_date(t1.which_month, '%M %d %Y') as which_date,
        str_to_date(t1.punch_in, '%h:%i %p') as punch_in_time,
        str_to_date(t1.punch_out, '%h:%i %p') as punch_out_time,
        TIME_FORMAT(TIMEDIFF(str_to_date(t1.punch_out, '%h:%i %p'), str_to_date(t1.punch_in, '%h:%i %p')), '%H:%i') as hours_worked,
        t2.name, 
        t2.department, 
        t3.department_name
    FROM attendance t1
    INNER JOIN users t2 ON t1.employee_id=t2.employee_id
    INNER JOIN departments t3 ON t2.department=t3.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',
        'formatter' => function ($cell, $row) {
            $returnValue = '<input name="r_checked[]" class="checkboxes" value="'.$row['id'].'" type="checkbox" />';
            return $returnValue;
        }
    ),
    array('db' => 'employee_id', 'dt' => 'employee_id'),
    array('db' => 'name', 'dt' => 'name'),
    array('db' => 'department_name', 'dt' => 'department_name'),
    array('db' => 'attendance_status', 'dt' => 'attendance_status'),
    array('db' => 'punch_in', 'dt' => 'punch_in'),
    array('db' => 'punch_out', 'dt' => 'punch_out'),
    array('db' => 'hours_worked', 'dt' => 'hours_worked'),
    array('db' => 'which_month', 'dt' => 'which_month'),
    array('db' => 'attendance_change', 'dt' => 'attendance_change'),
    array('db' => 'attendance_change_reason', 'dt' => 'attendance_change_reason'),
    array('db' => 'attendance_comment', 'dt' => 'attendance_comment'),
    array('db' => 'department', 'dt' => 'department'),

);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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)
);
