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

// ================= FETCH ROOMS =================
$rooms = $conn->query("SELECT id, room_number, room_type FROM rooms ORDER BY room_number ASC");

// ================= FETCH STAFF =================
$staff_result = $conn->query("SELECT id, fullname FROM staff ORDER BY fullname ASC");
$staff_list = [];
while ($s = $staff_result->fetch_assoc()) {
    $staff_list[] = $s;
}
?>

<div class="container mt-4">

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

    <div class="mb-3">
        <button class="btn btn-secondary" onclick="window.location.href='cleaning_schedule.php'">
            <i class="fa fa-arrow-left"></i> Back
        </button>
    </div>

    <h3><i class="fa fa-calendar-alt text-success"></i> Create New Cleaning Schedule</h3>

    <form action="set_schedule.php" method="post">
        <div class="table-responsive">
            <table class="table table-bordered table-striped mt-3">
                <thead class="bg-dark text-white">
                    <tr>
                        <th>Room</th>
                        <th>Type</th>
                        <th>Assign Housekeeper</th>
                        <th>Schedule Date</th>
                        <th>Assign?</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($r = $rooms->fetch_assoc()): ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($r['room_number']) ?></strong></td>
                        <td><?= htmlspecialchars($r['room_type']) ?></td>

                        <!-- Staff dropdown -->
                        <td>
                            <select name="housekeeper[<?= $r['id'] ?>]" class="form-control" required>
                                <option value="">-- Select Staff --</option>
                                <?php foreach ($staff_list as $s): ?>
                                    <option value="<?= htmlspecialchars($s['fullname']) ?>">
                                        <?= htmlspecialchars($s['fullname']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </td>

                        <!-- Schedule date -->
                        <td>
                            <input type="date" name="schedule_date[<?= $r['id'] ?>]" 
                                   value="<?= date('Y-m-d') ?>" class="form-control" required>
                        </td>

                        <!-- Assign Yes/No -->
                        <td class="text-center">
                            <input type="checkbox" name="assigned[<?= $r['id'] ?>]" value="Yes" checked>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>

        <div class="mt-3">
            <button type="submit" class="btn btn-success">
                <i class="fa fa-check"></i> Save Schedule
            </button>
        </div>
    </form>

</div>

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