<?php


$db1 = new SQLite3('../a/.eggziedb.db');
if(isset($_GET['ref_id'])) {
    
    $id_ref = $_GET['ref_id'];
    $result = $db1->query("SELECT COUNT(id) AS total FROM revenda WHERE id_ref = '$id_ref'");
    $rownew = $result->fetchArray();
    $count1 = $rownew['total'];
    
    $result = $db1->query("SELECT COUNT(id) AS total FROM sub_revenda WHERE id_ref = '$id_ref'");
    $rownew = $result->fetchArray();
    $count2 = $rownew['total'];
    
    $count = $count1 + $count2;
    
    if($count > 0) {
        
        if($count1 > 0) {
             $res1 = $db1->query("SELECT * FROM revenda WHERE id_ref = '$id_ref'");
            while ($row1 = $res1->fetchArray()) {
                                           
               $usuario = $row1['usuario'];
               
            }
        } 
        if($count2 > 0) {
            $res1 = $db1->query("SELECT * FROM sub_revenda WHERE id_ref = '$id_ref'");
            while ($row1 = $res1->fetchArray()) {
                                           
               $usuario = $row1['usuario'];
               
            }
        }
       
          $ack_data = array(
                        "revenda" => "$usuario"
                    );
        
        
    } else {
        if($id_ref == 1) {
                $ack_data = array(
                        "revenda" => "ADM"
                    );
        } else {
               $ack_data = array(
                        "revenda" => "undefined"
                    );
        }
    }
    

} else {
     $ack_data = array(
                        "revenda" => "undefined"
                    );
}

echo json_encode($ack_data);

?>
