AutoFiller_QR_Campamento/admin/empresas.php

106 lines
4.7 KiB
PHP

<?php
$adminSection = 'empresas';
$pageTitle = 'Empresas';
$pageHeading = 'Empresas';
require_once __DIR__ . '/../includes/admin_bootstrap.php';
require_once __DIR__ . '/../config/database.php';
$db = getDB();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
if ($action === 'create') {
$nombre = trim($_POST['nombre'] ?? '');
if ($nombre !== '') {
try {
$db->prepare('INSERT INTO empresas (nombre) VALUES (?)')->execute([$nombre]);
flash('success', 'Empresa agregada.');
} catch (PDOException $e) {
flash('danger', 'Ya existe esa 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 empresas SET nombre = ?, activo = ? WHERE id = ?')->execute([$nombre, $activo, $id]);
flash('success', 'Empresa actualizada.');
}
} elseif ($action === 'delete') {
$id = (int) ($_POST['id'] ?? 0);
if ($id > 0) {
$registros = countRegistrosForEmpresa($db, $id);
if ($registros > 0) {
flash('danger', "No se puede eliminar: tiene {$registros} registro(s) asociado(s). Desactívela en su lugar.");
} else {
$db->prepare('DELETE FROM empresas WHERE id = ?')->execute([$id]);
flash('success', 'Empresa eliminada (sus servicios también se eliminaron).');
}
}
}
redirect(BASE_URL . '/admin/empresas.php');
}
$empresas = $db->query('
SELECT e.*, COUNT(s.id) AS total_servicios
FROM empresas e LEFT JOIN servicios s ON s.empresa_id = e.id
GROUP BY e.id ORDER BY e.nombre
')->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-8">
<label class="form-label">Nueva empresa</label>
<input type="text" name="nombre" class="form-control" required maxlength="150">
</div>
<div class="col-md-4"><button type="submit" class="btn btn-primary w-100">Agregar</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>Nombre</th><th>Servicios</th><th>Activo</th><th></th></tr></thead>
<tbody>
<?php foreach ($empresas as $e): ?>
<tr>
<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) $e['id'] ?>">
<input type="text" name="nombre" class="form-control form-control-sm" value="<?= h($e['nombre']) ?>" required>
</td>
<td><?= (int) $e['total_servicios'] ?></td>
<td>
<div class="form-check form-switch mb-0">
<input class="form-check-input" type="checkbox" name="activo" <?= $e['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 la empresa «<?= h($e['nombre']) ?>» y todos sus servicios?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= (int) $e['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'; ?>