38682-vm/admin/tables.php
2026-02-27 01:20:05 +00:00

314 lines
14 KiB
PHP

<?php
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();
$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';
// 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">
<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>QR Code</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>
<button type="button" class="btn btn-sm btn-outline-info"
onclick="showTableQR('<?= $table['id'] ?>', '<?= htmlspecialchars($table['table_number']) ?>')">
<i class="bi bi-qr-code"></i> View QR
</button>
</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="7" 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">Cancel</button>
<button type="submit" class="btn btn-primary">Save Table</button>
</div>
</form>
</div>
</div>
</div>
<?php endif; ?>
<!-- QR Modal -->
<div class="modal fade" id="qrModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Table QR Code</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-center p-5">
<h4 id="qrTableNumber" class="mb-4 fw-bold"></h4>
<div id="qrcode" class="mb-4 d-flex justify-content-center"></div>
<p class="text-muted small">Scan this QR to order from this table</p>
<div class="d-grid gap-2">
<button type="button" class="btn btn-outline-primary" onclick="downloadQR()">
<i class="bi bi-download"></i> Download QR
</button>
<button type="button" class="btn btn-outline-secondary" onclick="printQR()">
<i class="bi bi-printer"></i> Print
</button>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script>
let qrGenerator = null;
const baseUrl = '<?= $baseUrl ?>';
function prepareAddForm() {
document.getElementById('tableModalTitle').innerText = 'Add New Table';
document.getElementById('tableAction').value = 'add_table';
document.getElementById('tableId').value = '';
document.getElementById('tableNumber').value = '';
document.getElementById('tableAreaId').value = '';
document.getElementById('tableCapacity').value = '2';
document.getElementById('tableStatus').value = 'available';
}
function prepareEditForm(table) {
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';
}
function showTableQR(id, number) {
document.getElementById('qrTableNumber').innerText = 'Table ' + number;
const qrContainer = document.getElementById('qrcode');
qrContainer.innerHTML = '';
const url = baseUrl + '/qorder.php?table=' + id;
qrGenerator = new QRCode(qrContainer, {
text: url,
width: 256,
height: 256,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
new bootstrap.Modal(document.getElementById('qrModal')).show();
}
function downloadQR() {
const img = document.querySelector('#qrcode img');
if (img) {
const link = document.createElement('a');
link.download = 'table-qr.png';
link.href = img.src;
link.click();
}
}
function printQR() {
const img = document.querySelector('#qrcode img');
if (!img) return;
const win = window.open('', '_blank');
win.document.write('<html><body style="text-align:center;padding:50px;">');
win.document.write('<h1>' + document.getElementById('qrTableNumber').innerText + '</h1>');
win.document.write('<img src="' + img.src + '" style="width:300px;">');
win.document.write('<p style="font-family:sans-serif;margin-top:20px;">Scan to Order</p>');
win.document.write('</body></html>');
win.document.close();
win.print();
win.close();
}
</script>
<?php include 'includes/footer.php'; ?>