<?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.*
    FROM leave_policy t1
) 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' => 'duration', 'dt' => 'duration'),
    array(
        'db' => 'leave_details', 
        'dt' => 'leave_details',
        'formatter' => function($cell, $row){
            $returnValue = '';
            if(!empty($cell)){
                $returnValue = '<div><ul>';
                foreach (json_decode($cell, true) as $key => $value) {
                    $returnValue .= '<li>';
                    foreach($value as $k => $v){
                        $returnValue .= '<p class="mb-0"><strong>'.ucwords(str_replace("_"," ",$k)).': </strong>'.$v.'</p>';
                    }
                    $returnValue .= '</li>';
                }
                $returnValue .= '</ul></div>';
            }
            return $returnValue;
        }
    ),
    array('db' => 'max_leave_take', 'dt' => 'max_leave_take'),
    array('db' => 'carry_forward', 'dt' => 'carry_forward'),
    array('db' => 'max_carry_forward', 'dt' => 'max_carry_forward'),
    array('db' => 'applicable_from', 'dt' => 'applicable_from'),
    array(
        'db' => 'applicable_to', 
        'dt' => 'applicable_to',
        'formatter' => function($cell, $row){
            $returnValue = $row['applicable_to'];
            if(empty($returnValue)){
                $returnValue = 'Current';
            }
            return $returnValue;
        }
    ),
    array(
        'db' => 'id', 
        'dt' => 'id',
        'formatter' => function($cell, $row){
            $returnValue = '';
             if(empty($row['applicable_to'])){
                $returnValue .= '<a href="leave_policy?edit='.$row['id'].'" class="mr-1 text-success" title="Edit"><i class="uil uil-edit"></i></a>';
                $returnValue .= '<a href="javascript:;" onclick="_delete_('.$row['id'].');" class="text-danger" title="Delete"><i class="uil uil-trash-alt"></i></a>';
            }
            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)
);
