100 lines
4.6 KiB
PHP
100 lines
4.6 KiB
PHP
<?php
|
|
$adminSection = 'modulos';
|
|
$pageTitle = 'Módulos';
|
|
$pageHeading = 'Módulos / Áreas';
|
|
require_once __DIR__ . '/../includes/admin_bootstrap.php';
|
|
require_once __DIR__ . '/../config/database.php';
|
|
|
|
$db = getDB();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$action = $_POST['action'] ?? 'update';
|
|
|
|
if ($action === 'create') {
|
|
$nombre = trim($_POST['nombre'] ?? '');
|
|
if ($nombre !== '') {
|
|
$codigo = nextModuloCodigo($db);
|
|
$db->prepare('INSERT INTO modulos (codigo, nombre) VALUES (?, ?)')->execute([$codigo, $nombre]);
|
|
flash('success', "Módulo creado con código {$codigo}. Ya puede agregar ubicaciones.");
|
|
} else {
|
|
flash('danger', 'El nombre del módulo es obligatorio.');
|
|
}
|
|
} else {
|
|
$id = (int) ($_POST['id'] ?? 0);
|
|
$nombre = trim($_POST['nombre'] ?? '');
|
|
$activo = isset($_POST['activo']) ? 1 : 0;
|
|
if ($id > 0 && $nombre !== '') {
|
|
$db->prepare('UPDATE modulos SET nombre = ?, activo = ? WHERE id = ?')->execute([$nombre, $activo, $id]);
|
|
flash('success', 'Módulo actualizado. Los QR no requieren regenerarse.');
|
|
}
|
|
}
|
|
redirect(BASE_URL . '/admin/modulos.php');
|
|
}
|
|
|
|
$modulos = $db->query('
|
|
SELECT m.*, COUNT(u.id) AS total_ubicaciones
|
|
FROM modulos m LEFT JOIN ubicaciones u ON u.modulo_id = m.id
|
|
GROUP BY m.id ORDER BY m.codigo
|
|
')->fetchAll();
|
|
$flash = getFlash();
|
|
require_once __DIR__ . '/../includes/admin_header.php';
|
|
?>
|
|
|
|
<?php if ($flash): ?><div class="alert alert-<?= h($flash['type']) ?>"><?= h($flash['message']) ?></div><?php endif; ?>
|
|
|
|
<div class="row g-4">
|
|
<div class="col-lg-4">
|
|
<div class="card">
|
|
<div class="card-header">Nuevo módulo / área</div>
|
|
<div class="card-body">
|
|
<form method="post">
|
|
<input type="hidden" name="action" value="create">
|
|
<div class="mb-3">
|
|
<label class="form-label">Nombre <span class="text-danger">*</span></label>
|
|
<input type="text" name="nombre" class="form-control" required maxlength="150"
|
|
placeholder="Ej. PLP, COCINA, Trailas nuevas...">
|
|
</div>
|
|
<p class="text-muted small">Se asignará un código único automático (MOD-xxx) para uso interno y QR.</p>
|
|
<button type="submit" class="btn btn-primary w-100">
|
|
<span class="material-symbols-outlined align-middle" style="font-size:18px">add</span>
|
|
Agregar módulo
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-lg-8">
|
|
<p class="text-muted">El <strong>código</strong> es permanente. Puede editar el <strong>nombre</strong> sin regenerar QR.</p>
|
|
<div class="card">
|
|
<div class="card-header"><?= count($modulos) ?> módulo(s)</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover mb-0">
|
|
<thead class="table-light">
|
|
<tr><th>Código</th><th>Nombre</th><th>Ubicaciones</th><th>Activo</th><th></th></tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($modulos as $m): ?>
|
|
<tr>
|
|
<form method="post">
|
|
<input type="hidden" name="action" value="update">
|
|
<input type="hidden" name="id" value="<?= (int) $m['id'] ?>">
|
|
<td><code class="badge-codigo"><?= h($m['codigo']) ?></code></td>
|
|
<td><input type="text" name="nombre" class="form-control form-control-sm" value="<?= h($m['nombre']) ?>" required></td>
|
|
<td>
|
|
<?= (int) $m['total_ubicaciones'] ?>
|
|
<a href="ubicaciones.php?modulo_id=<?= (int) $m['id'] ?>" class="small ms-1">Ver</a>
|
|
</td>
|
|
<td><div class="form-check form-switch"><input class="form-check-input" type="checkbox" name="activo" <?= $m['activo'] ? 'checked' : '' ?>></div></td>
|
|
<td><button type="submit" class="btn btn-sm btn-primary">Guardar</button></td>
|
|
</form>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/../includes/admin_footer.php'; ?>
|