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

$type = $_GET['type'] ?? '';

if($type == 'restaurant_orders'){
    $restaurant_orders = $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.status IN ('IN_KITCHEN','READY')
        ORDER BY ro.id ASC
    ");

    if($restaurant_orders->num_rows > 0){
        echo '<div class="row">';
        while($order = $restaurant_orders->fetch_assoc()){
            echo '<div class="col-md-4">
                    <div class="kitchen-card">
                        <div class="kitchen-title">Order #'.$order['id'].'</div>
                        <div class="kitchen-subtitle">Waiter: '.$order['waiter_name'].'</div>
                        <div>Customer: '.($order['customer_name'] ?? 'Walk-in').'</div>
                        <div>Subtotal: MK '.number_format($order['subtotal'],2).'</div>
                        <div>Discount: MK '.number_format($order['discount'],2).'</div>
                        <div>Grand Total: MK '.number_format($order['grand_total'],2).'</div>
                        <div>Status: <strong>'.$order['status'].'</strong></div>
                        <div class="kitchen-actions">';
            if($order['status'] == 'IN_KITCHEN'){
                echo '<a href="restaurant_order_update_status.php?order_id='.$order['id'].'&status=READY" 
                        class="btn btn-success btn-sm w-100 mb-1"
                        onclick="return confirm(\'Mark this order as READY?\');">✅ Ready</a>';
            } elseif($order['status'] == 'READY'){
                echo '<a href="restaurant_order_update_status.php?order_id='.$order['id'].'&status=SERVED" 
                        class="btn btn-primary btn-sm w-100 mb-1"
                        onclick="return confirm(\'Mark this order as SERVED?\');">🍽️ Served</a>';
            }
            echo '<a href="restaurant_order_view.php?order_id='.$order['id'].'" class="btn btn-info btn-sm w-100">View Items</a>';
            echo '</div></div></div>';
        }
        echo '</div>';
    } else {
        echo '<p>No restaurant orders in kitchen.</p>';
    }

} elseif($type == 'breakfast_checkins') {
    $breakfast_checkins = $conn->query("
        SELECT rci.*, g.name as guest_name, rci.room_id
        FROM stays rci
        LEFT JOIN guests g ON rci.guest_id = g.id
        WHERE rci.breakfast_prepared = 0
        ORDER BY rci.id ASC
    ");

    if($breakfast_checkins->num_rows > 0){
        echo '<div class="row">';
        while($checkin = $breakfast_checkins->fetch_assoc()){
            echo '<div class="col-md-4">
                    <div class="kitchen-card">
                        <div class="kitchen-title">Guest: '.$checkin['guest_name'].'</div>
                        <div class="kitchen-subtitle">Room #: '.$checkin['room_id'].'</div>
                        <div>Check-in ID: '.$checkin['id'].'</div>
                        <div class="kitchen-actions">
                            <a href="mark_breakfast_prepared.php?checkin_id='.$checkin['id'].'" 
                               class="btn btn-success btn-sm w-100"
                               onclick="return confirm(\'Mark breakfast as prepared?\');">🥐 Mark as Prepared</a>
                        </div>
                    </div>
                  </div>';
        }
        echo '</div>';
    } else {
        echo '<p>No room check-ins waiting for breakfast.</p>';
    }
}
?>
