Files
whatsapp/api/lab/get_formularios.php
2026-06-16 12:06:26 -05:00

46 lines
1.8 KiB
PHP

<?php
/**
* GET /api/lab/get_formularios.php — Lista de plantillas
* GET /api/lab/get_formularios.php?id=X — Plantilla específica
* GET /api/lab/get_formularios.php?envios=1&formulario_id=X — Envíos
*/
require_once __DIR__ . '/_helpers.php';
requireMethod('GET');
try {
$form = new Formulario();
// Lista de envíos
if (!empty($_GET['envios'])) {
$filtros = [];
if (!empty($_GET['formulario_id'])) $filtros['formulario_id'] = (int)$_GET['formulario_id'];
if (!empty($_GET['paciente_id'])) $filtros['paciente_id'] = (int)$_GET['paciente_id'];
if (!empty($_GET['estado'])) $filtros['estado'] = $_GET['estado'];
if (!empty($_GET['fecha_desde'])) $filtros['fecha_desde'] = $_GET['fecha_desde'];
if (!empty($_GET['fecha_hasta'])) $filtros['fecha_hasta'] = $_GET['fecha_hasta'];
if (!empty($_GET['paciente'])) $filtros['paciente'] = trim($_GET['paciente']);
if (!empty($_GET['origen'])) $filtros['origen'] = $_GET['origen'];
// Enfermero solo ve los suyos
if (userRole() === 'enfermero') {
$filtros['enviado_por'] = adminId();
}
$pagina = max(1, (int)($_GET['page'] ?? 1));
$porPagina = !empty($_GET['export']) ? 9999 : max(1, min(100, (int)($_GET['limit'] ?? 20)));
jsonOk($form->listarEnvios($filtros, $pagina, $porPagina));
}
// Plantilla específica
if (!empty($_GET['id'])) {
$f = $form->obtener((int)$_GET['id']);
if (!$f) jsonError('Formulario no encontrado', 404);
jsonOk(['formulario' => $f]);
}
// Lista de plantillas
$todas = isset($_GET['todas']) && userRole() === 'admin';
jsonOk(['data' => $form->listar(!$todas)]);
} catch (Exception $e) {
jsonError($e->getMessage(), 500);
}