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

$username = $_SESSION['username'] ?? 'Guest';
$today = date('Y-m-d');

// ================= FILTERS =================
$room_number = $_GET['room_number'] ?? '';
$type        = $_GET['type'] ?? '';
$status      = $_GET['status'] ?? '';
$search      = $_GET['search'] ?? '';

// ================= GET ROOM NUMBERS FOR DROPDOWN =================
$rooms_dropdown = $conn->query("SELECT room_number FROM rooms ORDER BY room_number ASC");

// ================= BUILD WHERE =================
$where = [];

if($room_number){
    $where[] = "r.room_number = '".$conn->real_escape_string($room_number)."'";
}
if($type){
    $where[] = "r.type = '".$conn->real_escape_string($type)."'";
}
if($status){
    $where[] = "r.status = '".$conn->real_escape_string($status)."'";
}
if($search){
    $s = $conn->real_escape_string($search);
    $where[] = "(r.room_number LIKE '%$s%' OR r.description LIKE '%$s%')";
}

$where_sql = $where ? "WHERE ".implode(" AND ", $where) : "";

// ================= FETCH ROOMS =================
$query = "SELECT r.*, 
                 IF(r.status='Occupied', 1, 0) AS occupied_flag 
          FROM rooms r 
          $where_sql
          ORDER BY r.room_number ASC";
$result = $conn->query($query);

// ================= SUMMARY =================
$summary = $conn->query("
SELECT 
    COUNT(*) AS total_rooms,
    SUM(price) AS total_price,
    SUM(CASE WHEN status='Occupied' THEN 1 ELSE 0 END) AS occupied_count,
    SUM(CASE WHEN status='Available' THEN 1 ELSE 0 END) AS available_count
FROM rooms r
$where_sql
")->fetch_assoc();

$total_rooms     = $summary['total_rooms'] ?? 0;
$total_price     = $summary['total_price'] ?? 0;
$occupied_count  = $summary['occupied_count'] ?? 0;
$available_count = $summary['available_count'] ?? 0;
?>

<style>
.report-card{border-radius:12px;}
.table thead th{white-space:nowrap;}
.welcome-msg{background:#f8f9fa;padding:8px 12px;border-radius:8px;margin-bottom:10px;}
@media print{.no-print{display:none!important;}}
</style>

<div class="container-fluid mt-4">
    <button class="btn btn-secondary no-print" onclick="history.back()"><i class="fa fa-arrow-left"></i> Back</button>

    <div class="welcome-msg no-print">
        <i class="fa fa-user-circle"></i>
        Welcome, <strong><?= htmlspecialchars($username) ?></strong>
    </div>

    <h3 class="mb-2"><i class="fa fa-bed text-primary"></i> Rooms Report</h3>
    <hr>

    <!-- ================= FILTERS ================= -->
    <form method="GET" class="row g-2 mb-3 no-print">
        <div class="col-md-2">
            <select name="room_number" class="form-control">
                <option value="">All Rooms</option>
                <?php while($r = $rooms_dropdown->fetch_assoc()): ?>
                    <option value="<?= htmlspecialchars($r['room_number']) ?>" <?= $room_number==$r['room_number']?'selected':'' ?>>
                        <?= htmlspecialchars($r['room_number']) ?>
                    </option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="col-md-2">
            <select name="type" class="form-control">
                <option value="">All Types</option>
                <option <?= $type=='Single'?'selected':'' ?>>Single</option>
                <option <?= $type=='Double'?'selected':'' ?>>Double</option>
                <option <?= $type=='Suite'?'selected':'' ?>>Suite</option>
            </select>
        </div>
        <div class="col-md-2">
            <select name="status" class="form-control">
                <option value="">All Statuses</option>
                <option <?= $status=='Available'?'selected':'' ?>>Available</option>
                <option <?= $status=='Occupied'?'selected':'' ?>>Occupied</option>
                <option <?= $status=='Maintenance'?'selected':'' ?>>Maintenance</option>
            </select>
        </div>
        <div class="col-md-3"><input type="text" name="search" class="form-control" placeholder="Search description" value="<?= htmlspecialchars($search) ?>"></div>
        <div class="col-md-3 d-flex gap-1">
            <button class="btn btn-primary w-100"><i class="fa fa-filter"></i> Filter</button>
            <a href="rooms_report.php" class="btn btn-secondary w-100">Reset</a>
        </div>
    </form>

    <!-- ================= ACTION BUTTONS ================= -->
    <div class="mb-3 no-print d-flex gap-2">
        <a href="export_rooms_excel.php?room_number=<?= urlencode($room_number) ?>&type=<?= urlencode($type) ?>&status=<?= urlencode($status) ?>&search=<?= urlencode($search) ?>" class="btn btn-success">
            <i class="fa fa-file-excel"></i> Export Excel
        </a>
        <button onclick="window.print()" class="btn btn-info"><i class="fa fa-print"></i> Print / PDF</button>
    </div>

    <!-- ================= SUMMARY CARDS ================= -->
    <div class="row mb-3 text-center">
        <div class="col-md-3">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Total Rooms</h6>
                    <h4 class="text-primary"><?= $total_rooms ?></h4>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Total Price (MK)</h6>
                    <h4 class="text-dark"><?= number_format($total_price,2) ?></h4>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Occupied</h6>
                    <h4 class="text-success"><?= $occupied_count ?></h4>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Available</h6>
                    <h4 class="text-warning"><?= $available_count ?></h4>
                </div>
            </div>
        </div>
    </div>

    <!-- ================= TABLE ================= -->
    <div class="table-responsive">
        <table class="table table-bordered table-striped table-sm">
            <thead class="table-dark">
                <tr>
                    <th>Room Number</th>
                    <th>Type</th>
                    <th>Status</th>
                    <th>Price (MK)</th>
                    <th>Capacity</th>
                </tr>
            </thead>
            <tbody>
                <?php if($result && $result->num_rows): ?>
                    <?php while($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?= $row['room_number'] ?></td>
                            <td><?= $row['room_type'] ?></td>
                            <td><?= $row['status'] ?></td>
                            <td><?= number_format($row['price'],2) ?></td>
                            <td><?= $row['capacity'] ?></td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr><td colspan="5" class="text-center text-danger">No rooms found</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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