265 lines
13 KiB
PHP
265 lines
13 KiB
PHP
<?php
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', '1');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . "/../includes/functions.php";
|
|
require_permission("tables_view");
|
|
$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();
|
|
|
|
// Use a very standard query without backticks on aliases to maximize compatibility
|
|
$query = "SELECT t.id, t.table_number, t.capacity, t.status, t.area_id, 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';
|
|
|
|
// Base URL for QR codes
|
|
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https') ? "https://" : "http://";
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
// Calculate project root
|
|
$current_dir = dirname($_SERVER['PHP_SELF']); // /admin
|
|
$project_root = dirname($current_dir); // /
|
|
if ($project_root === DIRECTORY_SEPARATOR) $project_root = '';
|
|
$baseUrl = $protocol . $host . $project_root;
|
|
?>
|
|
|
|
<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 rounded-3">
|
|
<div class="card-body p-0">
|
|
<?php render_pagination_controls($tables_pagination); ?>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead class="bg-light">
|
|
<tr>
|
|
<th class="ps-4">QR Code</th>
|
|
<th>Table #</th>
|
|
<th>Area</th>
|
|
<th>Capacity</th>
|
|
<th>Status</th>
|
|
<th class="text-end pe-4">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($tables)): ?>
|
|
<tr>
|
|
<td colspan="6" class="text-center py-5">
|
|
<div class="text-muted mb-3">
|
|
<i class="bi bi-info-circle fs-1"></i>
|
|
</div>
|
|
<h6>No tables found</h6>
|
|
<p class="small">Add your first table to get started.</p>
|
|
</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
<?php foreach ($tables as $table): ?>
|
|
<tr>
|
|
<td class="ps-4">
|
|
<?php
|
|
$qr_url = $baseUrl . "/qorder.php?table=" . $table['id'];
|
|
$qr_api = "https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=" . urlencode($qr_url);
|
|
?>
|
|
<div class="d-flex align-items-center gap-3">
|
|
<a href="<?= $qr_url ?>" target="_blank" class="d-inline-block border rounded p-1">
|
|
<img src="<?= $qr_api ?>" alt="QR" width="50" height="50">
|
|
</a>
|
|
<div class="small text-muted">
|
|
<a href="<?= $qr_api ?>&size=500x500" download="table_<?= $table['table_number'] ?>.png" class="text-decoration-none">
|
|
<i class="bi bi-download me-1"></i>Download
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
<td class="fw-bold"><?= htmlspecialchars($table['table_number']) ?></td>
|
|
<td>
|
|
<span class="badge bg-secondary bg-opacity-10 text-secondary border border-secondary border-opacity-25">
|
|
<?= htmlspecialchars($table['area_name'] ?? 'No Area') ?>
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<i class="bi bi-people me-1 text-muted"></i>
|
|
<?= htmlspecialchars((string)$table['capacity']) ?>
|
|
</td>
|
|
<td>
|
|
<?php
|
|
$status_class = 'bg-success';
|
|
if ($table['status'] === 'occupied') $status_class = 'bg-danger';
|
|
if ($table['status'] === 'reserved') $status_class = 'bg-warning';
|
|
if ($table['status'] === 'inactive') $status_class = 'bg-secondary';
|
|
?>
|
|
<span class="badge <?= $status_class ?> bg-opacity-10 text-<?= str_replace('bg-', '', $status_class) ?> border border-<?= str_replace('bg-', '', $status_class) ?> border-opacity-25">
|
|
<?= ucfirst($table['status']) ?>
|
|
</span>
|
|
</td>
|
|
<td class="text-end pe-4">
|
|
<div class="btn-group btn-group-sm">
|
|
<?php if (has_permission('tables_add')): ?>
|
|
<button class="btn btn-outline-primary" onclick='editTable(<?= json_encode($table) ?>)'>
|
|
<i class="bi bi-pencil"></i>
|
|
</button>
|
|
<?php endif; ?>
|
|
<?php if (has_permission('tables_del')): ?>
|
|
<a href="?delete=<?= $table['id'] ?>" class="btn btn-outline-danger" onclick="return confirm('Are you sure you want to remove this table?')">
|
|
<i class="bi bi-trash"></i>
|
|
</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php render_pagination_controls($tables_pagination); ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add/Edit Table Modal -->
|
|
<div class="modal fade" id="tableModal" tabindex="-1">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content border-0 shadow">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title fw-bold" id="modalTitle">Add New Table</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<form action="tables.php" method="POST">
|
|
<input type="hidden" name="action" id="formAction" value="add_table">
|
|
<input type="hidden" name="id" id="tableId">
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label fw-bold small text-uppercase">Table Number/Name</label>
|
|
<input type="text" name="table_number" id="tableNumber" class="form-control" required placeholder="e.g. Table 1, Booth A">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label fw-bold small text-uppercase">Area / Section</label>
|
|
<select name="area_id" id="areaId" 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="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label fw-bold small text-uppercase">Capacity</label>
|
|
<input type="number" name="capacity" id="tableCapacity" class="form-control" min="1" value="4" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label fw-bold small text-uppercase">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>
|
|
<div class="modal-footer bg-light border-0">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-primary px-4">Save Table</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function prepareAddForm() {
|
|
document.getElementById('modalTitle').innerText = 'Add New Table';
|
|
document.getElementById('formAction').value = 'add_table';
|
|
document.getElementById('tableId').value = '';
|
|
document.getElementById('tableNumber').value = '';
|
|
document.getElementById('areaId').value = '';
|
|
document.getElementById('tableCapacity').value = '4';
|
|
document.getElementById('tableStatus').value = 'available';
|
|
}
|
|
|
|
function editTable(table) {
|
|
document.getElementById('modalTitle').innerText = 'Edit Table';
|
|
document.getElementById('formAction').value = 'edit_table';
|
|
document.getElementById('tableId').value = table.id;
|
|
document.getElementById('tableNumber').value = table.table_number;
|
|
document.getElementById('areaId').value = table.area_id;
|
|
document.getElementById('tableCapacity').value = table.capacity;
|
|
document.getElementById('tableStatus').value = table.status;
|
|
|
|
new bootstrap.Modal(document.getElementById('tableModal')).show();
|
|
}
|
|
</script>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|