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

$status_filter = $_GET['status'] ?? '';

// ================= BUILD QUERY =================
$query = "
    SELECT 
        s.id AS id,
        s.checkin_date,
        s.checkout_date,
        s.days,
        s.breakfast_prepared,

        g.id AS guest_id,
        g.name AS guest_name,
        g.phone,
        g.email,

        r.room_number,
        r.room_type,
        IFNULL(r.price,0) AS room_price

    FROM stays s
    JOIN guests g ON s.guest_id = g.id
    JOIN rooms r ON s.room_id = r.id

    WHERE 
        s.checkout_date IS NULL
        AND g.name <> 'Walk-in'
";

// ================= STATUS FILTER =================
if ($status_filter === 'pending') {
    $query .= " AND s.breakfast_prepared = 0";
} elseif ($status_filter === 'prepared') {
    $query .= " AND s.breakfast_prepared = 1";
}

// ================= ORDER =================
$query .= " ORDER BY s.id DESC";

$orders = $conn->query($query);
?>

<div class="container mt-4">

    <div class="welcome-msg">
        <i class="fa fa-user-circle"></i>
        Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>

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

    <h3 class="text-center">Room Breakfast Orders</h3>
    <hr>

    <!-- ====== FILTER ====== -->
    <div class="mb-3">
        <form method="GET" class="row g-2">
            <div class="col-md-3">
                <select name="status" class="form-control" onchange="this.form.submit()">
                    <option value="">-- All --</option>
                    <option value="pending" <?= $status_filter=='pending'?'selected':'' ?>>Pending</option>
                    <option value="prepared" <?= $status_filter=='prepared'?'selected':'' ?>>Prepared</option>
                </select>
            </div>
        </form>
    </div>

    <!-- ====== TABLE ====== -->
    <table class="table table-bordered table-striped">
        <thead class="bg-primary text-white">
            <tr>
                <th>#</th>
                <th>Room</th>
                <th>Guest Name</th>
                <th>Phone</th>
                <th>Email</th>
                <th>Check-In</th>
                <th>Check-Out</th>
                <th>Duration</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>

        <tbody>
        <?php
        $i = 1;
        while ($o = $orders->fetch_assoc()):

            $checkin = new DateTime($o['checkin_date']);
            $checkout = clone $checkin;
            $checkout->modify('+' . intval($o['days']) . ' days');
        ?>

            <tr>
                <td><?= $i++ ?></td>
                <td><?= htmlspecialchars($o['room_number']) ?></td>
                <td><?= htmlspecialchars($o['guest_name']) ?></td>
                <td><?= htmlspecialchars($o['phone'] ?? '-') ?></td>
                <td><?= htmlspecialchars($o['email'] ?? '-') ?></td>
                <td><?= htmlspecialchars($o['checkin_date']) ?></td>
                <td><?= htmlspecialchars($checkout->format('Y-m-d')) ?></td>
                <td><?= intval($o['days']) ?> day(s)</td>

                <td>
                    <?php if ($o['breakfast_prepared']): ?>
                        <span class="badge bg-success">Prepared</span>
                    <?php else: ?>
                        <span class="badge bg-warning">Pending</span>
                    <?php endif; ?>
                </td>

                <td>
                    <?php if (!$o['breakfast_prepared']): ?>
                        <a href="mark_breakfast_prepared.php?checkin_id=<?= $o['id'] ?>"
                           class="btn btn-warning btn-sm"
                           onclick="return confirm('Mark breakfast as prepared?')">
                            🥞 Prepare
                        </a>
                    <?php else: ?>
                        <span class="text-success">✅ Done</span>
                    <?php endif; ?>
                </td>
            </tr>

        <?php endwhile; ?>
        </tbody>
    </table>

</div>

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