AutoFiller_QR_Campamento/admin/servicios.php

136 lines
6.3 KiB
PHP

<?php
$adminSection = 'servicios';
$pageTitle = 'Servicios';
$pageHeading = 'Servicios por empresa';
require_once __DIR__ . '/../includes/admin_bootstrap.php';
require_once __DIR__ . '/../config/database.php';
$db = getDB();
$empresaFiltro = (int) ($_GET['empresa_id'] ?? 0);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
if ($action === 'create') {
$empresaId = (int) ($_POST['empresa_id'] ?? 0);
$nombre = trim($_POST['nombre'] ?? '');
if ($empresaId > 0 && $nombre !== '') {
try {
$db->prepare('INSERT INTO servicios (empresa_id, nombre) VALUES (?, ?)')->execute([$empresaId, $nombre]);
flash('success', 'Servicio agregado.');
} catch (PDOException $e) {
flash('danger', 'Ese servicio ya existe para la empresa.');
}
}
} elseif ($action === 'update') {
$id = (int) ($_POST['id'] ?? 0);
$nombre = trim($_POST['nombre'] ?? '');
$activo = isset($_POST['activo']) ? 1 : 0;
if ($id > 0 && $nombre !== '') {
$db->prepare('UPDATE servicios SET nombre = ?, activo = ? WHERE id = ?')->execute([$nombre, $activo, $id]);
flash('success', 'Servicio actualizado.');
}
} elseif ($action === 'delete') {
$id = (int) ($_POST['id'] ?? 0);
if ($id > 0) {
$registros = countRegistrosForServicio($db, $id);
if ($registros > 0) {
flash('danger', "No se puede eliminar: tiene {$registros} registro(s) asociado(s). Desactívelo en su lugar.");
} else {
$db->prepare('DELETE FROM servicios WHERE id = ?')->execute([$id]);
flash('success', 'Servicio eliminado.');
}
}
}
redirect(BASE_URL . '/admin/servicios.php' . ($empresaFiltro ? '?empresa_id='.$empresaFiltro : ''));
}
$empresas = $db->query('SELECT id, nombre FROM empresas ORDER BY nombre')->fetchAll();
$sql = 'SELECT s.*, e.nombre AS empresa FROM servicios s JOIN empresas e ON e.id = s.empresa_id WHERE 1=1';
$params = [];
if ($empresaFiltro > 0) { $sql .= ' AND s.empresa_id = ?'; $params[] = $empresaFiltro; }
$sql .= ' ORDER BY e.nombre, s.nombre';
$stmt = $db->prepare($sql);
$stmt->execute($params);
$servicios = $stmt->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="card mb-4">
<div class="card-body">
<form method="post" class="row g-2 align-items-end">
<input type="hidden" name="action" value="create">
<div class="col-md-4">
<label class="form-label">Empresa</label>
<select name="empresa_id" class="form-select" required>
<option value="">— Seleccione —</option>
<?php foreach ($empresas as $e): ?>
<option value="<?= (int) $e['id'] ?>"><?= h($e['nombre']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-5">
<label class="form-label">Nuevo servicio</label>
<input type="text" name="nombre" class="form-control" required maxlength="150">
</div>
<div class="col-md-3"><button type="submit" class="btn btn-primary w-100">Agregar</button></div>
</form>
</div>
</div>
<div class="card mb-3">
<div class="card-body py-2">
<form method="get" class="row g-2 align-items-end">
<div class="col-md-4">
<select name="empresa_id" class="form-select">
<option value="">Todas las empresas</option>
<?php foreach ($empresas as $e): ?>
<option value="<?= (int) $e['id'] ?>" <?= $empresaFiltro === (int) $e['id'] ? 'selected' : '' ?>><?= h($e['nombre']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2"><button type="submit" class="btn btn-outline-primary">Filtrar</button></div>
</form>
</div>
</div>
<div class="card">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead class="table-light"><tr><th>Empresa</th><th>Servicio</th><th>Activo</th><th></th></tr></thead>
<tbody>
<?php foreach ($servicios as $s): ?>
<tr>
<td><?= h($s['empresa']) ?></td>
<td>
<form method="post" class="d-flex gap-2 align-items-center">
<input type="hidden" name="action" value="update">
<input type="hidden" name="id" value="<?= (int) $s['id'] ?>">
<input type="text" name="nombre" class="form-control form-control-sm" value="<?= h($s['nombre']) ?>" required>
</td>
<td>
<div class="form-check form-switch mb-0">
<input class="form-check-input" type="checkbox" name="activo" <?= $s['activo'] ? 'checked' : '' ?>>
</div>
</td>
<td class="text-nowrap">
<button type="submit" class="btn btn-sm btn-primary">Guardar</button>
</form>
<form method="post" class="d-inline" onsubmit="return confirm('¿Eliminar el servicio «<?= h($s['nombre']) ?>»?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= (int) $s['id'] ?>">
<button type="submit" class="btn btn-sm btn-outline-danger" title="Eliminar">
<span class="material-symbols-outlined align-middle" style="font-size:18px">delete</span>
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php require_once __DIR__ . '/../includes/admin_footer.php'; ?>