723 lines
25 KiB
PHP
723 lines
25 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$pdo = db();
|
|
$settings = get_company_settings();
|
|
|
|
$table_id = isset($_GET['table_id']) ? (int)$_GET['table_id'] : (isset($_GET['table']) ? (int)$_GET['table'] : 0);
|
|
$table_info = null;
|
|
|
|
if ($table_id === 0 && isset($_GET['table_number'])) {
|
|
$stmt = $pdo->prepare('SELECT id FROM `tables` WHERE table_number = ? AND is_deleted = 0 LIMIT 1');
|
|
$stmt->execute([$_GET['table_number']]);
|
|
$table_id = (int)$stmt->fetchColumn();
|
|
}
|
|
if ($table_id > 0) {
|
|
try {
|
|
$stmt = $pdo->prepare("
|
|
SELECT t.id, t.table_number AS table_name, a.outlet_id, o.name AS outlet_name
|
|
FROM `tables` t
|
|
JOIN areas a ON t.area_id = a.id
|
|
JOIN outlets o ON a.outlet_id = o.id
|
|
WHERE t.id = ?
|
|
");
|
|
$stmt->execute([$table_id]);
|
|
$table_info = $stmt->fetch();
|
|
|
|
if (!$table_info) {
|
|
die("Table with ID $table_id not found. Please contact staff.");
|
|
}
|
|
} catch (PDOException $e) {
|
|
die("Database error: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
$outlet_id = (int)($table_info['outlet_id'] ?? 0);
|
|
$categories = $pdo->query("SELECT * FROM categories WHERE is_deleted = 0 ORDER BY sort_order")->fetchAll();
|
|
$all_products = $pdo->query("SELECT p.*, c.name as category_name, c.name_ar as category_name_ar FROM products p JOIN categories c ON p.category_id = c.id WHERE p.is_deleted = 0 AND p.show_in_qorder = 1 AND c.is_deleted = 0")->fetchAll();
|
|
|
|
// Fetch variants
|
|
$variants_raw = $pdo->query("SELECT * FROM product_variants WHERE is_deleted = 0 ORDER BY price_adjustment ASC")->fetchAll();
|
|
$variants_by_product = [];
|
|
foreach ($variants_raw as $v) {
|
|
$variants_by_product[$v['product_id']][] = $v;
|
|
}
|
|
|
|
?>
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
|
|
<title><?= htmlspecialchars($settings['company_name'] ?? 'Order Online') ?></title>
|
|
<?php if (!empty($settings['favicon_url'])): ?>
|
|
<link rel="icon" href="<?= get_base_url() . htmlspecialchars($settings['favicon_url']) ?>?v=<?= time() ?>">
|
|
<?php endif; ?>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
|
|
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&family=Noto+Sans+Arabic:wght@400;600;700&display=swap" rel="stylesheet">
|
|
<style>
|
|
:root {
|
|
--primary-color: #2563eb;
|
|
--secondary-color: #64748b;
|
|
--bg-light: #f8fafc;
|
|
--card-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
|
|
--primary-font: 'Plus Jakarta Sans', sans-serif;
|
|
--arabic-font: 'Noto Sans Arabic', sans-serif;
|
|
}
|
|
body {
|
|
background-color: var(--bg-light);
|
|
font-family: var(--primary-font);
|
|
color: #1e293b;
|
|
padding-bottom: 80px; /* Space for floating cart */
|
|
}
|
|
.arabic-text {
|
|
font-family: var(--arabic-font);
|
|
direction: rtl;
|
|
}
|
|
|
|
/* Header Styling */
|
|
.hero-section {
|
|
background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
|
|
color: white;
|
|
padding: 3rem 1rem 5rem;
|
|
border-bottom-left-radius: 2.5rem;
|
|
border-bottom-right-radius: 2.5rem;
|
|
margin-bottom: -3rem;
|
|
position: relative;
|
|
z-index: 1;
|
|
}
|
|
.company-logo {
|
|
width: 80px;
|
|
height: 80px;
|
|
background: white;
|
|
padding: 10px;
|
|
border-radius: 20px;
|
|
box-shadow: var(--card-shadow);
|
|
margin-bottom: 1rem;
|
|
object-fit: contain;
|
|
}
|
|
.table-badge {
|
|
background: rgba(255, 255, 255, 0.2);
|
|
backdrop-filter: blur(10px);
|
|
padding: 0.5rem 1rem;
|
|
border-radius: 50px;
|
|
font-weight: 600;
|
|
font-size: 0.9rem;
|
|
display: inline-block;
|
|
}
|
|
|
|
/* Category Nav */
|
|
.category-nav-container {
|
|
position: sticky;
|
|
top: 0;
|
|
z-index: 100;
|
|
background: rgba(248, 250, 252, 0.8);
|
|
backdrop-filter: blur(10px);
|
|
padding: 0.75rem 0;
|
|
border-bottom: 1px solid #e2e8f0;
|
|
margin-bottom: 1.5rem;
|
|
}
|
|
.category-wrapper {
|
|
display: flex;
|
|
overflow-x: auto;
|
|
gap: 0.75rem;
|
|
padding: 0.25rem 1rem;
|
|
scrollbar-width: none;
|
|
-ms-overflow-style: none;
|
|
-webkit-overflow-scrolling: touch;
|
|
}
|
|
.category-wrapper::-webkit-scrollbar { display: none; }
|
|
|
|
.category-item {
|
|
display: inline-flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
text-decoration: none;
|
|
color: var(--secondary-color);
|
|
font-weight: 500;
|
|
font-size: 0.85rem;
|
|
min-width: 70px;
|
|
text-align: center;
|
|
transition: all 0.2s;
|
|
padding: 0.5rem;
|
|
border-radius: 12px;
|
|
}
|
|
.category-item.active {
|
|
color: var(--primary-color);
|
|
background: white;
|
|
box-shadow: 0 4px 6px -1px rgba(37, 99, 235, 0.1);
|
|
}
|
|
.category-icon {
|
|
width: 44px;
|
|
height: 44px;
|
|
border-radius: 12px;
|
|
background: #f1f5f9;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
margin-bottom: 0.4rem;
|
|
font-size: 1.25rem;
|
|
transition: all 0.2s;
|
|
overflow: hidden;
|
|
}
|
|
.category-icon img {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
}
|
|
.category-item.active .category-icon {
|
|
background: #dbeafe;
|
|
color: var(--primary-color);
|
|
}
|
|
|
|
/* Product Grid */
|
|
.products-grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(2, 1fr);
|
|
gap: 1rem;
|
|
padding: 0 1rem;
|
|
}
|
|
@media (min-width: 768px) {
|
|
.products-grid { grid-template-columns: repeat(3, 1fr); }
|
|
}
|
|
@media (min-width: 992px) {
|
|
.products-grid { grid-template-columns: repeat(4, 1fr); }
|
|
}
|
|
@media (min-width: 1200px) {
|
|
.products-grid { grid-template-columns: repeat(5, 1fr); }
|
|
}
|
|
|
|
.product-card {
|
|
background: white;
|
|
border-radius: 1.25rem;
|
|
overflow: hidden;
|
|
box-shadow: var(--card-shadow);
|
|
transition: transform 0.2s;
|
|
border: 1px solid #f1f5f9;
|
|
display: flex;
|
|
flex-direction: column;
|
|
height: 100%;
|
|
}
|
|
.product-card:active { transform: scale(0.98); }
|
|
|
|
.product-img-wrapper {
|
|
position: relative;
|
|
width: 100%;
|
|
padding-top: 100%; /* 1:1 Aspect Ratio */
|
|
background: #f8fafc;
|
|
}
|
|
.product-img {
|
|
position: absolute;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
}
|
|
.no-image-placeholder {
|
|
position: absolute;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background: #f1f5f9;
|
|
color: #94a3b8;
|
|
font-size: 2rem;
|
|
}
|
|
.product-info {
|
|
padding: 0.85rem;
|
|
display: flex;
|
|
flex-direction: column;
|
|
flex-grow: 1;
|
|
}
|
|
.product-name {
|
|
font-size: 0.95rem;
|
|
font-weight: 600;
|
|
margin-bottom: 0.25rem;
|
|
line-height: 1.3;
|
|
display: -webkit-box;
|
|
-webkit-line-clamp: 2;
|
|
-webkit-box-orient: vertical;
|
|
overflow: hidden;
|
|
}
|
|
.product-price {
|
|
font-weight: 700;
|
|
color: var(--primary-color);
|
|
font-size: 1rem;
|
|
margin-top: auto;
|
|
}
|
|
.add-btn {
|
|
width: 32px;
|
|
height: 32px;
|
|
border-radius: 10px;
|
|
background: var(--primary-color);
|
|
color: white;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
border: none;
|
|
box-shadow: 0 4px 6px -1px rgba(37, 99, 235, 0.3);
|
|
}
|
|
|
|
/* Floating Cart Bar */
|
|
.cart-bar {
|
|
position: fixed;
|
|
bottom: 1.5rem;
|
|
left: 1rem;
|
|
right: 1rem;
|
|
background: #1e293b;
|
|
color: white;
|
|
padding: 0.85rem 1.25rem;
|
|
border-radius: 1.25rem;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
z-index: 1000;
|
|
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
|
|
transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
|
|
transform: translateY(150%);
|
|
}
|
|
.cart-bar.visible { transform: translateY(0); }
|
|
.cart-count {
|
|
background: var(--primary-color);
|
|
color: white;
|
|
width: 24px;
|
|
height: 24px;
|
|
border-radius: 50%;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-size: 0.75rem;
|
|
font-weight: 700;
|
|
margin-right: 0.5rem;
|
|
}
|
|
.view-cart-btn {
|
|
color: white;
|
|
text-decoration: none;
|
|
font-weight: 600;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
/* Footer */
|
|
footer {
|
|
background: #f1f5f9;
|
|
padding: 3rem 1rem;
|
|
margin-top: 3rem;
|
|
text-align: center;
|
|
border-top: 1px solid #e2e8f0;
|
|
}
|
|
.footer-logo { height: 40px; margin-bottom: 1rem; filter: grayscale(1); opacity: 0.6; }
|
|
.footer-links {
|
|
display: flex;
|
|
justify-content: center;
|
|
gap: 1.5rem;
|
|
margin-bottom: 1.5rem;
|
|
}
|
|
.footer-links a { color: var(--secondary-color); font-size: 1.25rem; }
|
|
|
|
/* Modals */
|
|
.modal-content { border-radius: 1.5rem; border: none; }
|
|
.modal-header { border-bottom: 1px solid #f1f5f9; padding: 1.25rem; }
|
|
.modal-body { padding: 1.25rem; }
|
|
.variant-item {
|
|
border: 1px solid #e2e8f0;
|
|
border-radius: 12px;
|
|
padding: 0.75rem;
|
|
margin-bottom: 0.5rem;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
cursor: pointer;
|
|
}
|
|
.variant-item.selected {
|
|
border-color: var(--primary-color);
|
|
background: #eff6ff;
|
|
}
|
|
.cart-item {
|
|
display: flex;
|
|
gap: 1rem;
|
|
padding-bottom: 1rem;
|
|
margin-bottom: 1rem;
|
|
border-bottom: 1px solid #f1f5f9;
|
|
}
|
|
.qty-control {
|
|
display: flex;
|
|
align-items: center;
|
|
background: #f1f5f9;
|
|
border-radius: 10px;
|
|
padding: 2px;
|
|
}
|
|
.qty-btn {
|
|
width: 30px;
|
|
height: 30px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
border: none;
|
|
background: white;
|
|
border-radius: 8px;
|
|
font-weight: 700;
|
|
}
|
|
.qty-val { width: 30px; text-align: center; font-weight: 600; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<!-- Hero Header -->
|
|
<header class="hero-section text-center">
|
|
<div class="container">
|
|
<?php if (!empty($settings['logo_url'])): ?>
|
|
<img src="<?= get_base_url() . htmlspecialchars($settings['logo_url']) ?>?v=<?= time() ?>" class="company-logo" alt="Logo">
|
|
<?php endif; ?>
|
|
<h1 class="h3 fw-bold mb-1"><?= htmlspecialchars($settings['company_name'] ?? 'Restaurant') ?></h1>
|
|
<p class="opacity-75 mb-3 small"><?= htmlspecialchars($table_info['outlet_name'] ?? '') ?></p>
|
|
<div class="table-badge">
|
|
<i class="bi bi-geo-alt-fill me-1"></i> Table <?= htmlspecialchars($table_info['table_name'] ?? 'N/A') ?>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="container py-4">
|
|
<!-- Category Navigation -->
|
|
<div class="category-nav-container">
|
|
<div class="category-wrapper">
|
|
<a href="#" class="category-item active" data-category="all">
|
|
<div class="category-icon"><i class="bi bi-grid-fill"></i></div>
|
|
<span>All</span>
|
|
</a>
|
|
<?php foreach ($categories as $cat): ?>
|
|
<a href="#" class="category-item" data-category="<?= $cat['id'] ?>">
|
|
<div class="category-icon">
|
|
<?php if (!empty($cat['image_url'])): ?>
|
|
<img src="<?= (strpos($cat['image_url'], 'http') === 0) ? htmlspecialchars($cat['image_url']) : get_base_url() . htmlspecialchars($cat['image_url']) ?>" alt="<?= htmlspecialchars($cat['name']) ?>">
|
|
<?php else: ?>
|
|
<i class="bi bi-egg-fried"></i>
|
|
<?php endif; ?>
|
|
</div>
|
|
<span><?= htmlspecialchars($cat['name']) ?></span>
|
|
</a>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Product Grid -->
|
|
<div class="products-grid">
|
|
<?php foreach ($all_products as $p): ?>
|
|
<div class="product-card-container" data-category-id="<?= $p['category_id'] ?>">
|
|
<div class="product-card" onclick="openProduct(<?= htmlspecialchars(json_encode($p)) ?>)">
|
|
<div class="product-img-wrapper">
|
|
<?php if (!empty($p['image_url'])): ?>
|
|
<img src="<?= (strpos($p['image_url'], 'http') === 0) ? htmlspecialchars($p['image_url']) : get_base_url() . htmlspecialchars($p['image_url']) ?>" class="product-img" alt="<?= htmlspecialchars($p['name']) ?>">
|
|
<?php else: ?>
|
|
<div class="no-image-placeholder"><i class="bi bi-image"></i></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="product-info">
|
|
<h3 class="product-name"><?= htmlspecialchars($p['name']) ?></h3>
|
|
<div class="d-flex justify-content-between align-items-center">
|
|
<span class="product-price"><?= number_format($p['price'], 2) ?></span>
|
|
<button class="add-btn"><i class="bi bi-plus-lg"></i></button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Footer -->
|
|
<footer>
|
|
<div class="container">
|
|
<?php if (!empty($settings['logo_url'])): ?>
|
|
<img src="<?= get_base_url() . htmlspecialchars($settings['logo_url']) ?>?v=<?= time() ?>" class="footer-logo" alt="Logo">
|
|
<?php endif; ?>
|
|
<p class="text-muted small mb-3">
|
|
<?= htmlspecialchars($settings['address'] ?? '') ?><br>
|
|
<?= htmlspecialchars($settings['phone'] ?? '') ?>
|
|
</p>
|
|
<div class="footer-links">
|
|
<a href="#"><i class="bi bi-instagram"></i></a>
|
|
<a href="#"><i class="bi bi-facebook"></i></a>
|
|
<a href="#"><i class="bi bi-whatsapp"></i></a>
|
|
</div>
|
|
<p class="text-muted small mb-0">© <?= date('Y') ?> <?= htmlspecialchars($settings['company_name'] ?? 'Restaurant') ?>. All rights reserved.</p>
|
|
</div>
|
|
</footer>
|
|
|
|
<!-- Floating Cart -->
|
|
<div class="cart-bar" id="cart-bar">
|
|
<div class="d-flex align-items-center">
|
|
<span class="cart-count" id="cart-count">0</span>
|
|
<span class="fw-bold" id="cart-total">0.00</span>
|
|
</div>
|
|
<a href="#" class="view-cart-btn" onclick="showCartModal()">
|
|
View Cart <i class="bi bi-arrow-right ms-2"></i>
|
|
</a>
|
|
</div>
|
|
|
|
<!-- Product Modal -->
|
|
<div class="modal fade" id="productModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<div class="modal-header border-0">
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body text-center pt-0">
|
|
<div id="modal-img-container" class="mb-3 rounded-4 overflow-hidden" style="height: 200px; background: #f8fafc;"></div>
|
|
<h4 class="fw-bold mb-1" id="modal-product-name"></h4>
|
|
<p class="text-muted small mb-3" id="modal-product-desc"></p>
|
|
|
|
<div id="variants-section" class="text-start mb-4 d-none">
|
|
<p class="fw-bold mb-2">Options</p>
|
|
<div id="variants-list"></div>
|
|
</div>
|
|
|
|
<div class="d-flex align-items-center justify-content-between mb-4 px-2">
|
|
<span class="h4 fw-bold text-primary mb-0" id="modal-price"></span>
|
|
<div class="qty-control">
|
|
<button class="qty-btn" onclick="updateModalQty(-1)">-</button>
|
|
<span class="qty-val" id="modal-qty">1</span>
|
|
<button class="qty-btn" onclick="updateModalQty(1)">+</button>
|
|
</div>
|
|
</div>
|
|
<button class="btn btn-primary btn-lg w-100 rounded-pill py-3 fw-bold" onclick="addToCartFromModal()">Add to Cart</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Cart Modal -->
|
|
<div class="modal fade" id="cartModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-fullscreen-sm-down modal-dialog-scrollable">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title fw-bold">Review Order</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div id="cart-items-list"></div>
|
|
<div class="mt-4 p-3 bg-light rounded-4">
|
|
<div class="d-flex justify-content-between mb-2">
|
|
<span class="text-muted">Subtotal</span>
|
|
<span class="fw-bold" id="checkout-subtotal">0.00</span>
|
|
</div>
|
|
<div class="d-flex justify-content-between h4 mt-3 pt-3 border-top">
|
|
<span class="fw-bold">Total</span>
|
|
<span class="fw-bold text-primary" id="checkout-total">0.00</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer border-0 p-3">
|
|
<button class="btn btn-primary btn-lg w-100 rounded-pill py-3 fw-bold" onclick="placeOrder()">Place Order</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
let cart = [];
|
|
const variantsByProduct = <?= json_encode($variants_by_product) ?>;
|
|
let currentProduct = null;
|
|
let currentModalQty = 1;
|
|
let selectedVariant = null;
|
|
|
|
// Category Filtering
|
|
document.querySelectorAll('.category-item').forEach(item => {
|
|
item.addEventListener('click', (e) => {
|
|
e.preventDefault();
|
|
const catId = item.getAttribute('data-category');
|
|
|
|
document.querySelectorAll('.category-item').forEach(i => i.classList.remove('active'));
|
|
item.classList.add('active');
|
|
|
|
document.querySelectorAll('.product-card-container').forEach(card => {
|
|
if (catId === 'all' || card.getAttribute('data-category-id') === catId) {
|
|
card.style.display = 'block';
|
|
} else {
|
|
card.style.display = 'none';
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
function openProduct(p) {
|
|
currentProduct = p;
|
|
currentModalQty = 1;
|
|
selectedVariant = null;
|
|
|
|
document.getElementById('modal-product-name').textContent = p.name;
|
|
document.getElementById('modal-price').textContent = parseFloat(p.price).toFixed(2);
|
|
document.getElementById('modal-qty').textContent = '1';
|
|
|
|
const imgContainer = document.getElementById('modal-img-container');
|
|
if (p.image_url) {
|
|
const fullUrl = p.image_url.startsWith('http') ? p.image_url : '<?= get_base_url() ?>' + p.image_url;
|
|
imgContainer.innerHTML = `<img src="${fullUrl}" style="width:100%; height:100%; object-fit:cover;">`;
|
|
} else {
|
|
imgContainer.innerHTML = `<div class="d-flex align-items-center justify-content-center h-100 text-muted fs-1"><i class="bi bi-image"></i></div>`;
|
|
}
|
|
|
|
const variants = variantsByProduct[p.id] || [];
|
|
const vSection = document.getElementById('variants-section');
|
|
const vList = document.getElementById('variants-list');
|
|
|
|
if (variants.length > 0) {
|
|
vSection.classList.remove('d-none');
|
|
vList.innerHTML = variants.map(v => `
|
|
<div class="variant-item" data-variant-id="${v.id}" onclick="selectVariant(this, ${v.price_adjustment})">
|
|
<span>${v.variant_name}</span>
|
|
<span class="fw-bold">+${parseFloat(v.price_adjustment).toFixed(2)}</span>
|
|
</div>
|
|
`).join('');
|
|
} else {
|
|
vSection.classList.add('d-none');
|
|
}
|
|
|
|
new bootstrap.Modal(document.getElementById('productModal')).show();
|
|
}
|
|
|
|
function selectVariant(el, adj) {
|
|
document.querySelectorAll('.variant-item').forEach(v => v.classList.remove('selected'));
|
|
el.classList.add('selected');
|
|
selectedVariant = {
|
|
id: el.getAttribute('data-variant-id'),
|
|
name: el.querySelector('span').textContent,
|
|
adjustment: adj
|
|
};
|
|
updateModalPrice();
|
|
}
|
|
|
|
function updateModalPrice() {
|
|
let price = parseFloat(currentProduct.price);
|
|
if (selectedVariant) price += parseFloat(selectedVariant.adjustment);
|
|
document.getElementById('modal-price').textContent = (price * currentModalQty).toFixed(2);
|
|
}
|
|
|
|
function updateModalQty(delta) {
|
|
currentModalQty = Math.max(1, currentModalQty + delta);
|
|
document.getElementById('modal-qty').textContent = currentModalQty;
|
|
updateModalPrice();
|
|
}
|
|
|
|
function addToCartFromModal() {
|
|
const variants = variantsByProduct[currentProduct.id] || [];
|
|
if (variants.length > 0 && !selectedVariant) {
|
|
alert('Please select an option');
|
|
return;
|
|
}
|
|
|
|
const item = {
|
|
id: currentProduct.id,
|
|
name: currentProduct.name,
|
|
price: parseFloat(currentProduct.price),
|
|
qty: currentModalQty,
|
|
variant_id: selectedVariant ? selectedVariant.id : null,
|
|
variant_name: selectedVariant ? selectedVariant.name : '',
|
|
variant_adj: selectedVariant ? selectedVariant.adjustment : 0
|
|
};
|
|
|
|
const existing = cart.find(i => i.id === item.id && i.variant_id === item.variant_id);
|
|
if (existing) {
|
|
existing.qty += item.qty;
|
|
} else {
|
|
cart.push(item);
|
|
}
|
|
|
|
updateCartUI();
|
|
bootstrap.Modal.getInstance(document.getElementById('productModal')).hide();
|
|
}
|
|
|
|
function updateCartUI() {
|
|
const count = cart.reduce((sum, item) => sum + item.qty, 0);
|
|
const total = cart.reduce((sum, item) => sum + ((item.price + parseFloat(item.variant_adj)) * item.qty), 0);
|
|
|
|
document.getElementById('cart-count').textContent = count;
|
|
document.getElementById('cart-total').textContent = total.toFixed(2);
|
|
|
|
const cartBar = document.getElementById('cart-bar');
|
|
if (count > 0) {
|
|
cartBar.classList.add('visible');
|
|
} else {
|
|
cartBar.classList.remove('visible');
|
|
}
|
|
}
|
|
|
|
function showCartModal() {
|
|
const list = document.getElementById('cart-items-list');
|
|
const total = cart.reduce((sum, item) => sum + ((item.price + parseFloat(item.variant_adj)) * item.qty), 0);
|
|
|
|
list.innerHTML = cart.map((item, index) => `
|
|
<div class="cart-item">
|
|
<div class="flex-grow-1">
|
|
<h6 class="fw-bold mb-0">${item.name}</h6>
|
|
${item.variant_name ? `<small class="text-muted">${item.variant_name}</small>` : ''}
|
|
<div class="fw-bold text-primary mt-1">${((item.price + parseFloat(item.variant_adj)) * item.qty).toFixed(2)}</div>
|
|
</div>
|
|
<div class="qty-control align-self-center">
|
|
<button class="qty-btn" onclick="updateCartQty(${index}, -1)">-</button>
|
|
<span class="qty-val">${item.qty}</span>
|
|
<button class="qty-btn" onclick="updateCartQty(${index}, 1)">+</button>
|
|
</div>
|
|
</div>
|
|
`).join('');
|
|
|
|
document.getElementById('checkout-subtotal').textContent = total.toFixed(2);
|
|
document.getElementById('checkout-total').textContent = total.toFixed(2);
|
|
|
|
new bootstrap.Modal(document.getElementById('cartModal')).show();
|
|
}
|
|
|
|
function updateCartQty(index, delta) {
|
|
cart[index].qty += delta;
|
|
if (cart[index].qty <= 0) cart.splice(index, 1);
|
|
updateCartUI();
|
|
if (cart.length === 0) {
|
|
bootstrap.Modal.getInstance(document.getElementById('cartModal')).hide();
|
|
} else {
|
|
showCartModal();
|
|
}
|
|
}
|
|
|
|
async function placeOrder() {
|
|
if (cart.length === 0) return;
|
|
|
|
const orderData = {
|
|
table_id: <?= $table_id ?>,
|
|
outlet_id: <?= $outlet_id ?>,
|
|
items: cart.map(item => ({
|
|
product_id: item.id,
|
|
variant_id: item.variant_id,
|
|
quantity: item.qty,
|
|
price: item.price + parseFloat(item.variant_adj)
|
|
})),
|
|
order_type: 'dine-in'
|
|
};
|
|
|
|
try {
|
|
const resp = await fetch('api/order.php', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(orderData)
|
|
});
|
|
const res = await resp.json();
|
|
|
|
if (res.success) {
|
|
alert('Order placed successfully!');
|
|
cart = [];
|
|
updateCartUI();
|
|
bootstrap.Modal.getInstance(document.getElementById('cartModal')).hide();
|
|
} else {
|
|
alert('Error: ' + res.error);
|
|
}
|
|
} catch (e) {
|
|
alert('Failed to place order. Please try again.');
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|