<?php
include_once (ROOT . FOLDER_CORE .'/auth.php'); 
if ($auth == 1) {
    define('ALIAS_COMERCIALES', 'COMERCIALES');
    $base = consultaComerciales($bd_conn, ALIAS_COMERCIALES);
    $data = array();
    $array_count = 0;
    while ($row = $base->fetch_object()){
        $modelo_trim = str_replace(" ", "_", $row->DESC_MODELO); 
        //CARGO EL ARRAY DE CONEXIONES
        $data[$array_count] = array ( 
            "codigo_comercial" => $row->CONTACTO,
            "nombre_comercial" => mb_strtoupper($row->MOSTRAR, 'UTF-8')
        );
        $array_count ++;
    }
    mysqli_close($bd_conn);
    header("Content-type: application/json; charset=utf-8");
    //DEVUELVO EL JSON
    echo json_encode($data);
}elseif ($auth == 0) {
    $data = array();
    $data[] = array ( 
        "status" => "unauthorized_client",
        "message" => "Invalid authorization token"
    );
    header("Content-type: application/json; charset=utf-8");
    echo json_encode($data);
}
//FUNCIONES=============================================
function consultaComerciales($bd, $alias_comerciales)
{
    $sql =  "   SELECT R.CONTACTO, C.MOSTRAR
                FROM ETIQUETAS_GRUPOS G
                INNER JOIN CONTACTOS_RELACIONES R ON R.GRUPO = G.CODIGOCON
                INNER JOIN CONTACTOS C ON C.CODIGO = R.CONTACTO
                WHERE G.ALIAS = ?";
    $base = $bd->prepare($sql);
    $alias_comerciales = mysqli_real_escape_string($bd, $alias_comerciales);
    $base->bind_param('s', $alias_comerciales);
    $base->execute();
    $result = $base->get_result();
    $base->close();
    return $result;
}  
?>