<?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.*,
        t1.id as check_id,
        t2.password_assign_id,
        t2.u_eid,
        t2.u_name,
        t3.leader as p_eid
    FROM (
        SELECT 
            p1.id,
            p1.passwords,
        	p1.added_by as l_eid,
            p2.name AS l_name
        FROM password_management p1
        INNER JOIN users p2 ON p1.added_by = p2.employee_id
    ) t1
    LEFT JOIN (
        SELECT 
            GROUP_CONCAT(q1.id SEPARATOR ',') AS password_assign_id,
        	GROUP_CONCAT(q2.employee_id SEPARATOR ',') as u_eid,
            GROUP_CONCAT(q2.name SEPARATOR ',') as u_name,
            q1.pass_id
        FROM password_assign q1
        INNER JOIN users q2 ON q1.assigned_to=q2.employee_id
        GROUP BY q1.pass_id
        ORDER BY password_assign_id ASC
    ) t2 ON t1.id=t2.pass_id
    LEFT JOIN users t3 ON t3.employee_id=t1.l_eid
) 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' => 'check_id',
        'dt' => 'check_id',
        'formatter' => function ($cell, $row) {
            $returnValue = '<input name="r_checked[]" class="checkboxes" value="'.$row['id'].'" type="checkbox" />';
            return $returnValue;
        }
    ),
    array('db' => 'row_num', 'dt' => 'row_num'),
    array('db' => 'l_name', 'dt' => 'l_name'),
    array('db' => 'l_eid', 'dt' => 'l_eid'),
    array(
        'db' => 'passwords', 
        'dt' => 'passwords',
        'formatter' => function($cell, $row){
            $returnValue = '<div>';
            foreach (json_decode($cell, true) as $key => $value) {
                $returnValue .= '<h6 class="m-0">Host: <strong>'.$key.'</strong></h6>';
                foreach ($value as $k => $v) {
                    $returnValue .= '
                    <input type="hidden" value="'.$v.'" id="input_'.str_replace(" ","_",$k).'_'.$row['id'].'">
                    <p class="m-0 ml-2">
                        <strong>'.$k.': </strong> <span id="span_'.str_replace(" ","_",$k).'_'.$row['id'].'" style="filter:blur(2px);">'.$v.'</span> 
                        <i class="uil uil-eye text-primary" title="Show" style="cursor:pointer;" id="eye_'.str_replace(" ","_",$k).'_'.$row['id'].'" onclick="eye_button(\''.str_replace(" ","_",$k).'_'.$row['id'].'\')";></i> 
                        <i class="uil uil-copy text-primary" title="Copy" style="cursor:pointer;" id="copy_'.str_replace(" ","_",$k).'_'.$row['id'].'" onclick="copy_button(\''.str_replace(" ","_",$k).'_'.$row['id'].'\')";></i>
                    </p>';
                }
            }
            $returnValue .= '</div>';
            return $returnValue;
        }
    ),
    array(
        'db' => 'u_name', 
        'dt' => 'u_name',
        'formatter' => function($cell, $row){
            $returnValue = '<div>';
            if(!empty($row['u_name'])){
                $emp = explode(",",$row['u_name']);
                $ass_id = explode(",",$row['password_assign_id']);
                for ($i=0; $i < sizeof($ass_id); $i++) { 
                    $returnValue .= '<p class="m-0" id="assign_emp'.$ass_id[$i].'">'.$emp[$i].' <i class="uil uil-times-circle text-danger" title="Remove Access" style="cursor:pointer;" onclick="removePassAssign('.$ass_id[$i].');"></i></p>';
                }
            }
            $returnValue .= '</div>';
            return $returnValue;
        }
    ),
    array(
        'db' => 'password_assign_id', 
        'dt' => 'password_assign_id',
        'formatter' => function($cell, $row){
            $returnValue = '
                <a href="add_password?edit='.$row['id'].'" class="text-success mr-1" title="Edit"><i class="uil uil-edit"></i></a>
                <a href="javascript:;" onclick="_delete_('.$row['id'].');" title="Delete" class="text-danger"><i class="uil uil-trash-alt"></i></a>';
            return $returnValue;
        }
    ),
    array('db' => 'id', 'dt' => 'id'),
    array('db' => 'p_eid', 'dt' => 'p_eid'),
);


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