<?php
include_once (ROOT . FOLDER_CORE .'/auth.php'); 
if ($auth == 1) {
    $cod_exp = ! empty($uri[4]) ? $uri[4] : '';
    if ($cod_exp !== '') {
        //CONSULTO EL PREFIJO DE LAS PLANTILLAS DEL EXPEDIENTE-----------------
        $datos_exp = consultaPrefijoPlantilla($bd_conn, $cod_exp);
        $prefijo_plantilla = $datos_exp['PLANTILLA'];
        if ( $prefijo_plantilla !== '') {
           //CUENTO LOS CARACTERES PARA DEFINIR LA CONSULTA--------------------
           $cant_caracteres = strlen($prefijo_plantilla);
           if ( $cant_caracteres == 2) {
            $prefijo_qry = $prefijo_plantilla . '%';
            $base = consultaPlantillasExpedienteNombre($bd_conn, $prefijo_qry);
           }else{
            $prefijo_qry = $prefijo_plantilla;
            $base = consultaPlantillasExpedienteGrupo($bd_conn, $prefijo_qry);
           }
            $data = array();
            $array_count = 0;
            while ($row = $base->fetch_object()){
                //CARGO EL ARRAY DE CONEXIONES
                $data[$array_count] = array ( 
                    "codigo_plantilla" => $row->NOMBRE,
                    "nombre_plantilla" => mb_strtoupper($row->DESCRIPCION, 'UTF-8')
                );
                $array_count ++;
            }
            mysqli_close($bd_conn);
            header("Content-type: application/json; charset=utf-8");
            //DEVUELVO EL JSON
            echo json_encode($data);
        }else {
            $data = array();
            $data[] = array ( 
                "status" => "error",
                "message" => "código de expediente inválido"
            );
            header("Content-type: application/json; charset=utf-8");
            echo json_encode($data);
        }
    }else {
        $data = array();
        $data[] = array ( 
            "status" => "error",
            "message" => "necesita enviar un código de expediente"
        );
        header("Content-type: application/json; charset=utf-8");
        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 consultaPrefijoPlantilla($bd, $cod_exp)
{
    $sql =  "   SELECT T.PLANTILLA
                FROM EXPEDIENTES_TIPOS T
                WHERE T.CODIGO = ? LIMIT 1";
    $base = $bd->prepare($sql);
    $cod_exp = mysqli_real_escape_string($bd, $cod_exp);
    $base->bind_param('i', $cod_exp);
    $base->execute();
    $result =mysqli_fetch_array($base->get_result());
    $base->close();
    return $result;
}
function consultaPlantillasExpedienteNombre($bd, $prefijo_qry)
{
    $sql =  "   SELECT T.NOMBRE, T.DESCRIPCION
                FROM OCRPDF_TIPOS T
                WHERE T.NOMBRE LIKE ? AND T.VISIBLE = 1 ORDER BY T.DESCRIPCION ASC";
    $base = $bd->prepare($sql);
    $prefijo_qry = mysqli_real_escape_string($bd, $prefijo_qry);
    $base->bind_param('s', $prefijo_qry);
    $base->execute();
    $result = $base->get_result();
    $base->close();
    return $result;
}
function consultaPlantillasExpedienteGrupo($bd, $prefijo_qry)
{
    $sql =  "   SELECT T.NOMBRE, T.DESCRIPCION
                FROM OCRPDF_TIPOS T
                WHERE T.GRUPO = ? AND T.VISIBLE = 1 ORDER BY T.DESCRIPCION ASC";
    $base = $bd->prepare($sql);
    $prefijo_qry = mysqli_real_escape_string($bd, $prefijo_qry);
    $base->bind_param('s', $prefijo_qry);
    $base->execute();
    $result = $base->get_result();
    $base->close();
    return $result;
}  
?>