<?php 
ini_set('display_errors', 1);
ini_set('display_startup_erros', 1);
error_reporting(E_ALL);
include_once("../config.php"); 

$cs = new cs;

$id_unf = $_SESSION['info_unf']['id'];


if (isset($_REQUEST['token']) && $_REQUEST['token'] == $_SESSION['tkn_log']) { 

    if(isset($_REQUEST['usuario']) || isset($_REQUEST['id_unf']))
    {
        $SQL  = "";
        
        $login = $cs->filterKey($_REQUEST['usuario']);

        if(isset($_REQUEST['ordenar'])) {
            
            switch ($_REQUEST['ordenar']) {
                case 'vencasc':
                    $SQL = " ORDER by vencimento ASC";
                    break;
                  
                case 'logasc':
                    $SQL = " ORDER by p.login ASC";
                    break;
            
                case 'logasc':
                    $SQL = " ORDER by p.login DESC";
                    break;  
            }  
        }

        if(isset($_REQUEST['id_unf']) && is_numeric($_REQUEST['id_unf']))
        {
            $id_unf = (string) $_REQUEST['id_unf'];
        }

        $gerenciador = (in_array($id_unf, [1010101010, 1])) ? " and p.id NOT IN (1010101010, 1)" : " and p.gerenciador_responsavel = '{$id_unf}' ";
		$sql =  "SELECT p.*, s.login pai_login from gerenciadores p LEFT JOIN gerenciadores s ON p.gerenciador_responsavel = s.id WHERE (p.login like '%$login%' OR p.email like '%$login%') $gerenciador $SQL";
        
        
        if($query = ($cs->executeQuery($sql)))
        {
            $rows = $cs->fetchQuery($query);
            
            echo json_encode($rows);

        } else {
            echo 'Não foi possível fazer a pesquisa';
        }

    } 
}
    
if (isset($_GET['id']) && !empty($_GET['id'])) {
    
    $row = [];

    $id = $cs->filterKey($_GET['id']);
    
    $query = (in_array($id, [1010101010, 1])) ? "SELECT a.id, a.login FROM `gerenciadores` a, `gerenciadores` b WHERE a.id = b.gerenciador_responsavel AND a.id NOT IN(1,1010101010) GROUP BY a.id ORDER BY login ASC " : "SELECT * from gerenciadores WHERE (id = '{$id}' OR login = '{$id}')";
    
    $row = $cs->fetchQuery($cs->executeQuery($query));

    $query = $cs->executeQuery("select GROUP_CONCAT(DISTINCT  ips.ip SEPARATOR ' - ') as ips from has_ip as has LEFT JOIN ips ON has.ip_id = ips.id LEFT JOIN authenticator a ON has.authenticator_id = a.id WHERE a.user_id = '$id'");

    $query = $cs->fetchQuery($query);

    if (count($query) > 0) {
        
        array_push($row, $query);
    }    
     
    //var_dump($row); 
    echo json_encode($row);
}

// function getSubs($id) 
// {
//     $cs = new cs;

//     $query = "
//         SELECT 
//            id 
//         FROM 
//            gerenciadores 
//         WHERE 
//            gerenciador_responsavel = $id
//         ";

//     $query = $cs->executeQuery($query);
    
//     if ($cs->num_rows($query) > 0) {

//         return $cs->fetchQuery($query);
//     }   
// }

// function getManagerData($id) 
// {
    
//     //$row = [];

//     $query = "SELECT id, login, gerenciador_responsavel,
//     -- (SELECT sub.login FROM gerenciadores g INNER JOIN gerenciadores sub ON g.gerenciador_responsavel = sub.id WHERE g.id = $id) as Pai,  
        
//     (SELECT count(*) FROM usuarios WHERE id_gerenciador = $id and status = 'normal' and TIMESTAMP(vencimento) > CURRENT_TIMESTAMP()) as  total_usuarios, 
//     (SELECT count(*) FROM gerenciadores WHERE gerenciador_responsavel = $id) as total_gerenciadores
//         FROM 
//            gerenciadores 
//         WHERE 
//            id = $id";

//     $query = $cs->executeQuery($query);
    
//     if ($cs->num_rows($query) > 0) {
       
//         $row = $cs->fetchQuery($query);
        
//         $row[0]['lista'] = 0;
        
//         //$row[0]['subs'][] = getManagerData($row[0]['gerenciador_responsavel'], $cs);

//         return $row;

//     }    
// }

 

 
?>