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

// ================= FETCH ROOMS =================
$rooms = $conn->query("
    SELECT r.*, h.housekeeping_status, h.last_cleaned, h.housekeeper
    FROM rooms r
    LEFT JOIN housekeeping_status h ON r.id = h.room_id
    ORDER BY r.room_number
");

// ================= STATUS COUNTS =================
$stats = [
    'vacant' => 0,
    'occupied' => 0,
    'dirty' => 0,
    'clean' => 0
];

// Room occupancy stats
$statQuery = $conn->query("
    SELECT 
        SUM(CASE WHEN status='Vacant' THEN 1 ELSE 0 END) as vacant,
        SUM(CASE WHEN status='Occupied' THEN 1 ELSE 0 END) as occupied
    FROM rooms
");

if ($row = $statQuery->fetch_assoc()) {
    $stats['vacant']   = $row['vacant'] ?? 0;
    $stats['occupied'] = $row['occupied'] ?? 0;
}

// Housekeeping stats
$hkQuery = $conn->query("
    SELECT
        SUM(CASE WHEN housekeeping_status='Dirty' THEN 1 ELSE 0 END) as dirty,
        SUM(CASE WHEN housekeeping_status='Clean' THEN 1 ELSE 0 END) as clean
    FROM housekeeping_status
");

if ($row = $hkQuery->fetch_assoc()) {
    $stats['dirty'] = $row['dirty'] ?? 0;
    $stats['clean'] = $row['clean'] ?? 0;
}
?>

<style>
.badge-status {
    font-size: 12px;
    padding: 6px 10px;
    border-radius: 20px;
}

.search-box {
    max-width: 300px;
}

.table tbody tr:hover {
    background: #f5fdf7;
}

.stat-card {
    border-radius: 12px;
    color: #fff;
    padding: 15px;
    text-align: center;
    font-weight: 600;
}

.welcome-msg {
    font-size: 1.05rem;
    margin-bottom: 15px;
    color: #155724;
    background: #e6f4ea;
    padding: 10px 15px;
    border-radius: 8px;
}
</style>

<div class="container-fluid mt-4">

    <!-- Welcome -->
    <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="mb-3">
        <i class="fa fa-bed text-success"></i> Room Status
    </h3>

    <!-- ================= SUMMARY CARDS ================= -->
    <div class="row mb-3">
        <div class="col-md-3 mb-2">
            <div class="stat-card bg-success">
                Vacant Rooms<br><h4><?= $stats['vacant'] ?></h4>
            </div>
        </div>
        <div class="col-md-3 mb-2">
            <div class="stat-card bg-danger">
                Occupied Rooms<br><h4><?= $stats['occupied'] ?></h4>
            </div>
        </div>
        <div class="col-md-3 mb-2">
            <div class="stat-card bg-warning text-dark">
                Dirty Rooms<br><h4><?= $stats['dirty'] ?></h4>
            </div>
        </div>
        <div class="col-md-3 mb-2">
            <div class="stat-card bg-primary">
                Clean Rooms<br><h4><?= $stats['clean'] ?></h4>
            </div>
        </div>
    </div>

    <!-- ================= SEARCH ================= -->
    <div class="mb-3">
        <input type="text" id="roomSearch" class="form-control search-box"
               placeholder="🔍 Search room...">
    </div>

    <!-- ================= TABLE ================= -->
    <div class="table-responsive">
        <table class="table table-bordered table-striped">
            <thead class="bg-dark text-white">
                <tr>
                    <th>#</th>
                    <th>Room Number</th>
                    <th>Type</th>
                    <th>Status</th>
                    <th>Housekeeping</th>
                    <th>Last Cleaned</th>
                    <th>Housekeeper</th>
                    <th>Actions</th>
                </tr>
            </thead>

            <tbody id="roomTable">
            <?php $i=1; while($r=$rooms->fetch_assoc()): 

                // ===== ROOM STATUS BADGE =====
                $status = strtolower($r['status'] ?? '');
                $status_class = 'secondary';
                if ($status == 'vacant') $status_class = 'success';
                elseif ($status == 'occupied') $status_class = 'danger';

                // ===== HK STATUS BADGE =====
                $hk = strtolower($r['housekeeping_status'] ?? 'pending');
                $hk_class = 'secondary';
                if ($hk == 'clean') $hk_class = 'primary';
                elseif ($hk == 'dirty') $hk_class = 'warning';
            ?>
                <tr>
                    <td><?= $i ?></td>
                    <td><strong><?= htmlspecialchars($r['room_number']) ?></strong></td>
                    <td><?= htmlspecialchars($r['room_type']) ?></td>

                    <td>
                        <span class="badge badge-status bg-<?= $status_class ?>">
                            <?= htmlspecialchars($r['status']) ?>
                        </span>
                    </td>

                    <td>
                        <span class="badge badge-status bg-<?= $hk_class ?>">
                            <?= htmlspecialchars($r['housekeeping_status'] ?? 'Pending') ?>
                        </span>
                    </td>

                    <td><?= htmlspecialchars($r['last_cleaned'] ?? '-') ?></td>
                    <td><?= htmlspecialchars($r['housekeeper'] ?? '-') ?></td>

                    <td>
                        <a href="update_room_status.php?room_id=<?= $r['id'] ?>"
                           class="btn btn-sm btn-primary">
                           <i class="fa fa-edit"></i> Update
                        </a>
                    </td>
                </tr>
            <?php $i++; endwhile; ?>
            </tbody>
        </table>
    </div>

</div>

<script>
document.getElementById("roomSearch").addEventListener("keyup", function () {
    let value = this.value.toLowerCase();
    let rows = document.querySelectorAll("#roomTable tr");

    rows.forEach(row => {
        row.style.display =
            row.innerText.toLowerCase().includes(value) ? "" : "none";
    });
});
</script>

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