<?php
include 'db.php';
$order_id = intval($_GET['order_id']);

// Fetch order
$order = $conn->query("SELECT * FROM restaurant_orders WHERE id=$order_id")->fetch_assoc();
$items = $conn->query("SELECT * FROM restaurant_order_items WHERE order_id=$order_id");

?>

<div style="font-family:monospace; width:300px;">
    <h3 style="text-align:center;">🍽 My Restaurant</h3>
    <p>Order #: <?= $order['id'] ?><br>
       Table: <?= $order['table_number'] ?? 'N/A' ?><br>
       Waiter: <?= $order['waiter_id'] ?><br>
       Time: <?= $order['created_at'] ?></p>
    <hr>
    <table style="width:100%;">
    <?php while($i = $items->fetch_assoc()): ?>
        <tr>
            <td><?= $i['qty'] ?> x <?= $i['menu_id'] ?></td>
            <td style="text-align:right"><?= number_format($i['price']*$i['qty'],2) ?></td>
        </tr>
    <?php endwhile; ?>
    </table>
    <hr>
    <p>Subtotal: <?= number_format($order['subtotal'],2) ?><br>
       Discount: <?= number_format($order['discount'],2) ?><br>
       Grand Total: <?= number_format($order['grand_total'],2) ?></p>
</div>

<script>
window.print(); // auto print to kitchen thermal printer
</script>
