<?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 * FROM letters_of_intent
) 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' => 'loi_reference_id', 
        'dt' => 'loi_reference_id',
        'formatter' => function($cell, $row) {
            $returnValue = '<a href="loi_template/loi_preview?loi_id=' . base64_encode($row['loi_reference_id']) . '" class="text-primary" title="Preview LOI (Letter Of Intent)" target="_blank">' . $row['loi_reference_id'] . '</a>';
            return $returnValue;
        }
    ),
    array(
        'db' => 'applicant_name', 
        'dt' => 'applicant_name',
        'formatter' => function($cell, $row){
            $returnValue = '<div>';
            $returnValue .= '<p class="mb-0"><strong>Applicant Name: </strong>' . $row['applicant_name'] . '</p>';
            $returnValue .= '<p class="mb-0"><strong>Applicant Email: </strong>' . $row['applicant_email'] . '</p>';
            $returnValue .= '<p class="mb-0"><strong>Applicant Mobile: </strong>' . $row['applicant_mobile'] . '</p>';
            $returnValue .= '</div>';

            return $returnValue;
        }
    ),
    array('db' => 'loi_subject', 'dt' => 'loi_subject'),
    array(
        'db' => 'mail_log', 
        'dt' => 'mail_log',
        'formatter' => function($cell, $row){
            $returnValue = '<div>';
            $mail_log = !empty($row['mail_log'])? json_decode($row['mail_log'], true): [];
            foreach ($mail_log as $key => $value) {
                $returnValue .= '<p class="mb-0"><strong>' . ucwords(str_replace('_', ' ', $key)) . ': </strong>' . $value . '</p>';
            }
            if($mail_log['status']!='Not Sent'){
                $returnValue .= '<a href="loi_template/loi_preview?loi_id='.base64_encode($row['loi_reference_id']).'&mail_status=sent" class="text-primary" title="Preview Sent LOI (Letter Of Intent)"  target="_blank"><i class="uil uil-eye"></i></a>';
            }
            $returnValue .= '</div>';
            return $returnValue;
        }
    ),
    array(
        'db' => 'applicant_email', 
        'dt' => 'applicant_email',
        'formatter' => function($cell, $row) {
            $returnValue = '';
            $mail_log = !empty($row['mail_log'])? json_decode($row['mail_log'], true): [];
            if(!isset($mail_log['status']) || $mail_log['status'] === 'Not Sent') {
                $returnValue .= '<a href="javascript:;" onclick="generateLOI(\''.base64_encode($row['id']).'\', \''.$row['employment_type'].'\');" class="mr-1 text-secondary" title="Generate LOI (Letter of Intent)"><i class="uil uil-envelope-share"></i></a>';
            }
            $returnValue .= '<a href="add_loi?edit='.base64_encode($row['id']).'" class="mr-1 text-success" title="Edit"><i class="uil uil-edit"></i></a>';
            // $returnValue .= '<a href="javascript:;" onclick="_delete_(\''.base64_encode($row['id']).'\');" class="text-danger" title="Delete"><i class="uil uil-trash-alt"></i></a>';
            return $returnValue;
        }
    ),
    array('db' => 'applicant_mobile', 'dt' => 'applicant_mobile'),
    array('db' => 'employment_type', 'dt' => 'employment_type'),
    

);


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