<?php
include 'db.php';

// Helper function
function fmt($num) { return number_format($num ?? 0, 2); }

// Get order ID
$order_id = isset($_GET['order_id']) && is_numeric($_GET['order_id']) ? intval($_GET['order_id']) : 0;
if (!$order_id) die("Error: Order ID not specified.");

// Fetch order
$order = $conn->query("
    SELECT ro.*, u.username AS waiter_name, c.name AS customer_name
    FROM restaurant_orders ro
    LEFT JOIN users u ON ro.waiter_id = u.id
    LEFT JOIN customers c ON ro.customer_id = c.id
    WHERE ro.id=$order_id
")->fetch_assoc();
if (!$order) die("Error: Order not found.");

// Fetch order items
$items = $conn->query("
    SELECT roi.*, rm.name AS menu_name, p.name AS product_name
    FROM restaurant_order_items roi
    LEFT JOIN restaurant_menu rm ON roi.menu_id = rm.id
    LEFT JOIN products p ON roi.product_id = p.id
    WHERE roi.order_id=$order_id
");
// ================= TAX (INCLUSIVE) =================
$subtotal_incl = floatval($order['grand_total']); // already inclusive

$total_tax_rate = 0.175 + 0.01; // 18.5%

$taxable_base = $subtotal_incl / (1 + $total_tax_rate);

$vat = $taxable_base * 0.175;
$tourism = $taxable_base * 0.01;
// Logo
$logo = 'assets/logo.png';
?>
<!DOCTYPE html>
<html>
<head>
    <title>Order #<?= $order['id'] ?> - Receipt</title>
    <style>
        body { font-family: monospace; font-size:12px; width:72mm; margin:0; }
        .center { text-align:center; }
        hr { border:none; border-top:1px dashed #000; margin:3px 0; }
        table { width:100%; border-collapse:collapse; }
        td { padding:2px 0; }
        td.right { text-align:right; }
        .total { font-weight:bold; font-size:14px; }
        .logo { width:60px; display:block; margin:0 auto; }
        button.print-btn { width:100%; padding:6px; background:#28a745; color:#fff; border:none; cursor:pointer; margin:5px 0; }
        @media print { button { display:none; } }
    </style>
</head>
<body>
    <img src="<?= $logo ?>" class="logo">
    <h3 class="center">🍽 RAM'S EXECUTIVE LODGE</h3>
    <p class="center">
       <b> Order #: <?= $order['id'] ?></b><br>
        Waiter: <?= $order['waiter_name'] ?><br>
        Customer: <?= $order['customer_name'] ?? 'Walk-in' ?><br>
        Status: <?= $order['status'] ?><br>
        Date: <?= $order['created_at'] ?>
    </p>
    <hr>
    <table>
        <?php while($item = $items->fetch_assoc()): 
            $item_total = $item['price'] * $item['qty'];
        ?>
        <tr>
            <td><?= $item['qty'] ?> x <?= $item['menu_name'] ?? 'Item '.$item['menu_id'] ?></td>
            <td class="right"><?= fmt($item_total) ?></td>
        </tr>
        <?php endwhile; ?>
    </table>
    <hr>
   <p>
    Subtotal (Incl): <?= fmt($subtotal_incl) ?><br>
    VAT (17.5%): <?= fmt($vat) ?><br>
    Tourism Levy (1%): <?= fmt($tourism) ?><br>
    Discount: <?= fmt($order['discount']) ?><br>
    <span class="total">Grand Total: <?= fmt($subtotal_incl) ?></span>
</p><p class="center">* Prices are Tax Inclusive *</p>
    <p class="center">Thank you for dining with us!</p>

    <!-- Print button -->
    <button class="print-btn" onclick="window.print()">🖨 Print Order</button>
</body>
</html>
