<?php
include 'db.php';

// Helper function to format numbers safely
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) =================
$total_incl = floatval($order['grand_total']); // already inclusive
$tax_rate = 0.175 + 0.01; // VAT 17.5% + Tourism 1%
$taxable_base = $total_incl / (1 + $tax_rate);

$vat = $taxable_base * 0.175;
$tourism = $taxable_base * 0.01;

// Logo
$logo = 'assets/logo.png';
?>
<!DOCTYPE html>
<html>
<head>
    <title>Receipt #<?= $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">
        Web: www.ramslodgemw.com<br>
        Phone: +265 993 899 382<br>
        Phone: +265 883 899 382<br>
        Receipt #: <?= $order['id'] ?><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 
        $total_calc = 0;
        while($item = $items->fetch_assoc()):
            $item_total = $item['price'] * $item['qty']; // no double tax
            $total_calc += $item_total;
        ?>
        <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>
    <table>
        <tr>
            <td>Subtotal (Incl):</td>
            <td class="right"><?= fmt($total_incl) ?></td>
        </tr>
        <tr>
            <td>VAT (17.5%):</td>
            <td class="right"><?= fmt($vat) ?></td>
        </tr>
        <tr>
            <td>Tourism Levy (1%):</td>
            <td class="right"><?= fmt($tourism) ?></td>
        </tr>
        <tr>
            <td>Discount:</td>
            <td class="right"><?= fmt($order['discount']) ?></td>
        </tr>
        <tr>
            <td><strong>Grand Total:</strong></td>
            <td class="right"><strong><?= fmt($total_incl) ?></strong></td>
        </tr>
        <tr>
            <td>Payment Method:</td>
            <td><?= htmlspecialchars($order['payment_method']) ?></td>
        </tr>
    </table>

    <div class="center">
        <p>* Prices are Tax Inclusive *</p>
        <p>Thank you for dining with us!</p>
        <p><b>e-HOTELS</b>&nbsp;Powered by Giant+</p>
    </div>

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