<?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.*, 
        t2.name, 
        ROW_NUMBER() OVER (ORDER BY id DESC) AS row_num
    FROM incash_leave t1
    INNER JOIN users t2 ON t1.employee_id=t2.employee_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' => 'employee_id', 'dt' => 'employee_id'),
    array('db' => 'name', 'dt' => 'name'),
    array(
        'db' => 'casual_inCash', 
        'dt' => 'casual_inCash',
        'formatter' => function($cell, $row){
            $returnValue = '<div>
                <p class="mb-0"><strong>Casual: </strong>'.$row['casual_inCash'].'</p>
                <p class="mb-0"><strong>Medical: </strong>'.$row['medical_inCash'].'</p>
            </div>';

            return $returnValue;
        }
    ),
    array(
        'db' => 'casual_carryForward', 
        'dt' => 'casual_carryForward',
        'formatter' => function($cell, $row){
            $returnValue = '<div>
                <p class="mb-0"><strong>Casual: </strong>'.$row['casual_carryForward'].'</p>
                <p class="mb-0"><strong>Medical: </strong>'.$row['medical_carryForward'].'</p>
            </div>';

            return $returnValue;
        }
    ),
    array(
        'db' => 'financial_year_start', 
        'dt' => 'financial_year_start',
        'formatter' => function($cell, $row){
            $returnValue = '<div>
                <p class="mb-0"><strong>Start: </strong>'.$row['financial_year_start'].'</p>
                <p class="mb-0"><strong>End: </strong>'.$row['financial_year_end'].'</p>
            </div>';

            return $returnValue;
        }
    ),
    array(
        'db' => 'financial_year_end', 
        'dt' => 'financial_year_end',
        'formatter' => function($cell, $row){
            $returnValue = '
            <a href="leave_incash?edit='.$row['id'].'" class="mr-1 text-success" title="Edit"><i class="uil uil-edit"></i></a>
                <a href="javascript:;" onclick="_delete_('.$row['id'].');" class="text-danger" title="Delete"><i class="uil uil-trash-alt"></i></a>
            ';
            return $returnValue;
        }
    ),
    array('db' => 'medical_inCash', 'dt' => 'medical_inCash'),
    array('db' => 'medical_carryForward', 'dt' => 'medical_carryForward'),
);


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