<?php 

require_once(__DIR__."/../config.php"); 
//require_once(__DIR__."/../vendor/autoload.php");
//require_once(__DIR__."/../vendor/sql.class.php");


$cs = new cs;  
$db = new Sql(); 

$request = $_POST;   
$return  = null;

if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' && isset($request['id']) && !empty($request['id'])) {       

    $login_us = $cs->filterKey($request['login_us']);

    $return = $db->query("CALL sp_manager_delete (:id, :acao, :detalhes, :ip)", 
        [ 
            ':id'       => (int) $request['id'],
            ':acao'     => 'apagoug',
            ':detalhes' => "Apagou o gerenciador {$login_us}", 
            ':ip'       => $_SERVER['REMOTE_ADDR']   
        ]
    );

    getPai($request['id']);
        
    echo json_encode($return);         
}  

/* FUNCOES PARA DELETAR TODOS OS GERENCIADORES E USUARIOS DE SUBS */

function getManagers($id) {
    
    $cs = new cs;
    
    $query = "SELECT login, id, gerenciador_responsavel FROM gerenciadores WHERE gerenciador_responsavel = '{$id}' GROUP BY id";
    
    $query = $cs->executeQuery($query);
    
    if ($cs->num_rows($query)  > 0) { 
        return $cs->fetchQuery($query);
    }
}

function getUsers($id, $users = []) {
    
    $cs = new cs;
    
    $query = "SELECT id, iptv_id_user, login FROM usuarios WHERE id_gerenciador = '{$id}'";
    
    $query = $cs->executeQuery($query);
    
    if ($cs->num_rows($query)  > 0) { 

        $data = $cs->fetchQuery($query);
    
        foreach ($data as $value) {
            if ($value) {
                $users['users'][]    = $value['id'];
                $users['iptv_ids'][] = $value['login'];
            }
        }
        
        $users['users']  = implode(',', $users['users']);
        //$users['xtream'] = implode(',', $users['xtream']);
         
        return $users; 
    }
}

function deleteManager($id) 
{
    $cs = new cs;
    
    if ($id) {
        
        $query = "DELETE FROM gerenciadores WHERE id = '{$id}'";
        $cs->executeQuery($query);

        $query = "DELETE FROM gerenciadores WHERE gerenciador_responsavel = '{$id}'";
        $cs->executeQuery($query);
    }
}

function deleteXtreamUsers($ids) 
{
    $sql = new Sql();
    
    if ($ids) {
        foreach ($ids as $id) {
        
            $query = "DELETE FROM xtream_iptvpro.users WHERE username = :id";
            
            $sql->query($query, [
                ':id' => $id
            ]);
        }
    }
}


function deleteUsers($id) 
{
    $cs = new cs;
    
    if ($id) {
        $query = "DELETE FROM usuarios WHERE id_gerenciador = '{$id}'";
        
        $cs->executeQuery($query);
    }

}

function getPai($id) {
    
    $users = getUsers($id);
    $data  = getManagers($id);   
    
    // Deleta os gerenciadores dele  
    deleteManager($id);

    // Deleta os usuarios do atual gerenciador
    deleteUsers($id); 

    // Deleta os usuarios do atual gerenciador na xtream
    deleteXtreamUsers($users['iptv_ids']);          
      
    if (!is_null($data) AND count($data) > 0) {
        foreach ($data as $key => $value) {
            
            if (isset($value['id'])) {
                
                getPai($value['id']);                                  
            }           
        }       
    }    
} 