38682-vm/admin/tables.php
2026-02-25 18:22:13 +00:00

222 lines
10 KiB
PHP

<?php
require_once __DIR__ . "/../includes/functions.php";
require_permission("tables_view");
require_once __DIR__ . '/../db/config.php';
$pdo = db();
$message = '';
// Handle Add/Edit Table
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$action = $_POST['action'];
$table_number = trim($_POST['table_number']);
$capacity = (int)$_POST['capacity'];
$area_id = (int)$_POST['area_id'];
$status = $_POST['status'];
$id = isset($_POST['id']) ? (int)$_POST['id'] : null;
if (empty($table_number)) {
$message = '<div class="alert alert-danger">Table number is required.</div>';
} else {
try {
if ($action === 'edit_table' && $id) {
if (!has_permission('tables_add')) {
$message = '<div class="alert alert-danger">Access Denied.</div>';
} else {
$stmt = $pdo->prepare("UPDATE tables SET table_number = ?, capacity = ?, area_id = ?, status = ? WHERE id = ?");
$stmt->execute([$table_number, $capacity, $area_id, $status, $id]);
$message = '<div class="alert alert-success">Table updated successfully!</div>';
}
} elseif ($action === 'add_table') {
if (!has_permission('tables_add')) {
$message = '<div class="alert alert-danger">Access Denied.</div>';
} else {
$stmt = $pdo->prepare("INSERT INTO tables (table_number, capacity, area_id, status) VALUES (?, ?, ?, ?)");
$stmt->execute([$table_number, $capacity, $area_id, $status]);
$message = '<div class="alert alert-success">Table created successfully!</div>';
}
}
} catch (PDOException $e) {
$message = '<div class="alert alert-danger">Database error: ' . $e->getMessage() . '</div>';
}
}
}
// Handle Delete (Soft Delete)
if (isset($_GET['delete'])) {
if (!has_permission('tables_del')) {
$message = '<div class="alert alert-danger">Access Denied: You do not have permission to delete tables.</div>';
} else {
try {
$id = (int)$_GET['delete'];
// Soft delete to avoid breaking historical order integrity
$pdo->prepare("UPDATE tables SET is_deleted = 1 WHERE id = ?")->execute([$id]);
header("Location: tables.php?deleted=1");
exit;
} catch (PDOException $e) {
$message = '<div class="alert alert-danger">Error removing table: ' . $e->getMessage() . '</div>';
}
}
}
if (isset($_GET['deleted'])) {
$message = '<div class="alert alert-success">Table removed successfully!</div>';
}
$areas = $pdo->query("SELECT * FROM areas WHERE is_deleted = 0 ORDER BY name ASC")->fetchAll();
$query = "SELECT t.*, a.name as area_name
FROM tables t
LEFT JOIN areas a ON t.area_id = a.id
WHERE t.is_deleted = 0
ORDER BY a.name ASC, t.table_number ASC";
$tables_pagination = paginate_query($pdo, $query);
$tables = $tables_pagination['data'];
include 'includes/header.php';
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="fw-bold mb-0">Tables</h2>
<?php if (has_permission('tables_add')): ?>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#tableModal" onclick="prepareAddForm()">
<i class="bi bi-plus-lg"></i> Add Table
</button>
<?php endif; ?>
</div>
<?= $message ?>
<div class="card border-0 shadow-sm">
<div class="card-body p-0">
<!-- Pagination Controls -->
<div class="p-3 border-bottom bg-light">
<?php render_pagination_controls($tables_pagination); ?>
</div>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="bg-light">
<tr>
<th class="ps-4">ID</th>
<th>Table Number</th>
<th>Area</th>
<th>Capacity</th>
<th>Status</th>
<th class="text-end pe-4">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($tables as $table): ?>
<tr>
<td class="ps-4 fw-medium">#<?= $table['id'] ?></td>
<td class="fw-bold"><?= htmlspecialchars($table['table_number']) ?></td>
<td><span class="badge bg-light text-dark border"><?= htmlspecialchars($table['area_name'] ?: 'None') ?></span></td>
<td><?= $table['capacity'] ?> Persons</td>
<td>
<?php if ($table['status'] === 'available'): ?>
<span class="badge bg-success-subtle text-success px-3">Available</span>
<?php elseif ($table['status'] === 'occupied'): ?>
<span class="badge bg-danger-subtle text-danger px-3">Occupied</span>
<?php else: ?>
<span class="badge bg-secondary-subtle text-secondary px-3"><?= ucfirst($table['status']) ?></span>
<?php endif; ?>
</td>
<td class="text-end pe-4">
<?php if (has_permission('tables_add')): ?>
<button type="button" class="btn btn-sm btn-outline-primary me-1"
data-bs-toggle="modal" data-bs-target="#tableModal"
onclick='prepareEditForm(<?= htmlspecialchars(json_encode($table), ENT_QUOTES, "UTF-8") ?>)' title="Edit"><i class="bi bi-pencil"></i></button>
<?php endif; ?>
<?php if (has_permission('tables_del')): ?>
<a href="?delete=<?= $table['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('<?= t('are_you_sure') ?>')"><i class="bi bi-trash"></i></a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($tables)): ?>
<tr>
<td colspan="6" class="text-center py-4 text-muted">No tables found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<!-- Bottom Pagination -->
<div class="p-3 border-top bg-light">
<?php render_pagination_controls($tables_pagination); ?>
</div>
</div>
</div>
<!-- Table Modal -->
<?php if (has_permission('tables_add')): ?>
<div class="modal fade" id="tableModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="tableModalTitle">Add New Table</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" id="tableForm">
<div class="modal-body">
<input type="hidden" name="action" id="tableAction" value="add_table">
<input type="hidden" name="id" id="tableId">
<div class="mb-3">
<label class="form-label">Table Number <span class="text-danger">*</span></label>
<input type="text" name="table_number" id="tableNumber" class="form-control" required placeholder="e.g. T1">
</div>
<div class="mb-3">
<label class="form-label">Area <span class="text-danger">*</span></label>
<select name="area_id" id="tableAreaId" class="form-select" required>
<option value="">Select Area</option>
<?php foreach ($areas as $area): ?>
<option value="<?= $area['id'] ?>"><?= htmlspecialchars($area['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Capacity (Persons)</label>
<input type="number" name="capacity" id="tableCapacity" class="form-control" value="2" min="1">
</div>
<div class="mb-3">
<label class="form-label">Status</label>
<select name="status" id="tableStatus" class="form-select">
<option value="available">Available</option>
<option value="occupied">Occupied</option>
<option value="reserved">Reserved</option>
<option value="inactive">Inactive</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save Table</button>
</div>
</form>
</div>
</div>
</div>
<script>
function prepareAddForm() {
document.getElementById('tableModalTitle').innerText = 'Add New Table';
document.getElementById('tableAction').value = 'add_table';
document.getElementById('tableForm').reset();
document.getElementById('tableId').value = '';
}
function prepareEditForm(table) {
if (!table) return;
document.getElementById('tableModalTitle').innerText = 'Edit Table';
document.getElementById('tableAction').value = 'edit_table';
document.getElementById('tableId').value = table.id;
document.getElementById('tableNumber').value = table.table_number || '';
document.getElementById('tableAreaId').value = table.area_id || '';
document.getElementById('tableCapacity').value = table.capacity || 2;
document.getElementById('tableStatus').value = table.status || 'available';
}
</script>
<?php endif; ?>
<?php include 'includes/footer.php'; ?>