<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

$order_id = intval($_GET['order_id'] ?? 0);

if(!$order_id){
    echo "<script>alert('Order not specified'); window.location='restaurant_orders_list.php';</script>";
    exit;
}

// ==================== 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){
    echo "<script>alert('Order not found'); window.location='restaurant_orders_list.php';</script>";
    exit;
}

// ==================== 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
");
?>

<div class="container mt-4">
     <!-- ================= WELCOME MESSAGE ================= -->
    
     <button class="btn btn-secondary btn-custom" onclick="history.back()">
    <i class="fa fa-arrow-left"></i> Back
</button>
    <h3 class="text-center">Order Details - #<?= $order['id'] ?></h3>
    <hr>

    <div class="mb-3">
        <strong>Waiter:</strong> <?= $order['waiter_name'] ?><br>
        <strong>Customer:</strong> <?= $order['customer_name'] ?? 'Walk-in' ?><br>
        <strong>Status:</strong> 
       <?php
$status = $order['status'];

$badges = [
    'IN_KITCHEN' => 'warning',
    'READY'      => 'info',
    'SERVED'     => 'primary',
    'PAID'       => 'success'
];

$badge = $badges[$status] ?? 'secondary';

echo "<span class='badge bg-$badge'>$status</span>";
?>
        <br>
        <strong>Subtotal:</strong> MK <?= number_format($order['subtotal'],2) ?><br>
        <strong>Discount:</strong> MK <?= number_format($order['discount'],2) ?><br>
        <strong>Grand Total:</strong> MK <?= number_format($order['grand_total'],2) ?><br>
        <strong>Order Date:</strong> <?= $order['created_at'] ?? $order['order_date'] ?? '' ?>
    </div>

    <!-- Order Items Table -->
    <table class="table table-bordered table-striped">
        <thead class="bg-dark text-white">
            <tr>
                <th>#</th>
                <th>Menu Item</th>
                <th>Linked Product</th>
                <th>Price (MK)</th>
                <th>Qty</th>
                <th>Total (MK)</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $i = 1;
            $total = 0;
            while($item = $items->fetch_assoc()):
                $item_total = $item['price'] * $item['qty'];
                $total += $item_total;
            ?>
                <tr>
                    <td><?= $i++ ?></td>
                    <td><?= $item['menu_name'] ?></td>
                    <td><?= $item['product_name'] ?? '-' ?></td>
                    <td><?= number_format($item['price'],2) ?></td>
                    <td><?= $item['qty'] ?></td>
                    <td><?= number_format($item_total,2) ?></td>
                </tr>
            <?php endwhile; ?>
        </tbody>
        <tfoot>
            <tr>
                <th colspan="5" class="text-end">Subtotal:</th>
                <th>MK <?= number_format($total,2) ?></th>
            </tr>
            <tr>
                <th colspan="5" class="text-end">Discount:</th>
                <th>MK <?= number_format($order['discount'],2) ?></th>
            </tr>
            <tr>
                <th colspan="5" class="text-end">Grand Total:</th>
                <th>MK <?= number_format($order['grand_total'],2) ?></th>
            </tr>
        </tfoot>
    </table>

    <div class="mt-3">

    <button class="btn btn-secondary btn-custom" onclick="history.back()">
        <i class="fa fa-arrow-left"></i> Back
    </button>

    <!-- ================= STATUS ACTIONS ================= -->

    <?php if($order['status']=='IN_KITCHEN'): ?>

        <a href="restaurant_order_update_status.php?order_id=<?= $order['id'] ?>&status=READY"
           class="btn btn-success"
           onclick="return confirm('Mark this order as READY?');">

           ✅ Mark Ready
        </a>

    <?php endif; ?>


    <?php if($order['status']=='READY'): ?>

        <a href="restaurant_order_update_status.php?order_id=<?= $order['id'] ?>&status=SERVED"
           class="btn btn-primary"
           onclick="return confirm('Mark this order as SERVED?');">

           🍽️ Mark Served
        </a>

    <?php endif; ?>


    <?php if($order['status']=='SERVED'): ?>

        <a href="restaurant_order_payment.php?order_id=<?= $order['id'] ?>"
           class="btn btn-success"
           onclick="return confirm('Proceed to collect payment?');">

           💳 Collect Payment
        </a>

    <?php endif; ?>


    <?php if($order['status']=='PAID'): ?>

        <span class="badge bg-dark p-2" style="font-size:14px;">
            <i class="fa fa-check-circle"></i> PAYMENT COMPLETED
        </span>

    <?php endif; ?>


    <!-- ================= PRINT BUTTONS ================= -->

    <?php if($order['status']=='SERVED' || $order['status']=='PAID'): ?>

        <a href="print_receipt.php?order_id=<?= $order['id'] ?>"
           target="_blank"
           class="btn btn-dark">

            🧾 Print Receipt
        </a>

    <?php endif; ?>


    <a href="print_restaurant_order.php?order_id=<?= $order['id'] ?>"
       target="_blank"
       class="btn btn-success">

       🖨 Print Order
    </a>

</div>
</div>

<?php include 'includes/footer.php'; ?>
