80 lines
3.3 KiB
PHP
80 lines
3.3 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.');
|
|
}
|
|
}
|
|
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>
|
|
<form method="post">
|
|
<input type="hidden" name="action" value="update">
|
|
<input type="hidden" name="id" value="<?= (int) $e['id'] ?>">
|
|
<td><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"><input class="form-check-input" type="checkbox" name="activo" <?= $e['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>
|
|
|
|
<?php require_once __DIR__ . '/../includes/admin_footer.php'; ?>
|