<?php
require_once "../config/db.php";
require_once "../inc/auth_admin.php";

/* ==========================
   FILTER STATUS
========================== */
$status = $_GET['status'] ?? '';

if ($status) {
    $stmt = $conn->prepare("
        SELECT laporan.*, kamar.nomor_kamar, penghuni.nama
        FROM laporan
        JOIN kamar ON laporan.kamar_id = kamar.id
        JOIN penghuni ON laporan.penghuni_id = penghuni.id
        WHERE laporan.status = ?
        ORDER BY laporan.tanggal_lapor DESC
    ");
    $stmt->bind_param("s", $status);
    $stmt->execute();
    $result = $stmt->get_result();
} else {
    $result = $conn->query("
        SELECT laporan.*, kamar.nomor_kamar, penghuni.nama
        FROM laporan
        JOIN kamar ON laporan.kamar_id = kamar.id
        JOIN penghuni ON laporan.penghuni_id = penghuni.id
        ORDER BY laporan.tanggal_lapor DESC
    ");
}
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../assets/style.css">
<title>Data Laporan - LuminaCare</title>

<style>
.page-title{
padding:30px 30px 10px 30px;
}

.page-title h1{
margin:0;
font-size:32px;
font-weight:700;
background:linear-gradient(90deg,#1e3a8a,#2563eb);
-webkit-background-clip:text;
color:transparent;
}

.page-title p{
margin-top:5px;
color:#6b7280;
font-size:14px;
}

/* garis dekorasi */
.page-title::after{
content:"";
display:block;
width:60px;
height:4px;
background:#2563eb;
border-radius:2px;
margin-top:10px;
}
body {
    margin:0;
    font-family: Arial, sans-serif;
    background:#f4f6f9;
}

.header {
    background:#1e3a8a;
    color:white;
    padding:20px;
    display:flex;
    justify-content:space-between;
    align-items:center;
}

.header-title {
    font-size:20px;
    font-weight:bold;
}

.nav a {
    color:white;
    text-decoration:none;
    margin-left:20px;
    font-size:14px;
}

.container {
    padding:30px;
}

.filter a {
    margin-right:15px;
    text-decoration:none;
    font-weight:bold;
    color:#6b7280;
}

.filter a.active {
    color:#1e3a8a;
}

table {
    width:100%;
    border-collapse:collapse;
    background:white;
    box-shadow:0 4px 10px rgba(0,0,0,0.05);
}

th, td {
    padding:12px;
    border-bottom:1px solid #ddd;
    text-align:left;
}

th {
    background:#f1f5f9;
}

tr:hover {
    background:#f9fafb;
}

.status-baru { color:#dc2626; font-weight:bold; }
.status-diproses { color:#f59e0b; font-weight:bold; }
.status-selesai { color:#16a34a; font-weight:bold; }

.btn-detail {
    display:inline-block;
    padding:6px 12px;
    background:#2563eb;
    color:white;
    text-decoration:none;
    border-radius:4px;
    font-size:13px;
}

/* Mobile Fix */
@media (max-width:768px){

    .header {
        flex-direction:column;
        align-items:flex-start;
    }

    .nav {
        margin-top:10px;
        width:100%;
    }

    .nav a {
        display:inline-block;
        margin:5px 10px 0 0;
    }

    .container {
        padding:15px;
    }

    th, td {
        padding:8px;
        font-size:12px;
    }

    .btn-detail {
        display:block;
        width:100%;
        text-align:center;
    }
}
</style>
</head>

<body>

<?php include "../inc/navbar_admin.php"; ?>
<div class="page-title">
    <h1>Data Laporan</h1>
    <p>Monitoring status laporan kost LuminaCare</p>
</div>
    <div class="nav">
  
</div>
</div>

<div class="container">

    <div class="filter">
        <a href="laporan.php" class="<?= $status==''?'active':'' ?>">Semua</a>
        <a href="laporan.php?status=baru" class="<?= $status=='baru'?'active':'' ?>">Baru</a>
        <a href="laporan.php?status=diproses" class="<?= $status=='diproses'?'active':'' ?>">Diproses</a>
        <a href="laporan.php?status=selesai" class="<?= $status=='selesai'?'active':'' ?>">Selesai</a>
    </div>

    <br>

    <div class="table-wrapper">
    <table>
        <tr>
            <th>Kamar</th>
            <th>Penghuni</th>
            <th>Kategori</th>
            <th>Status</th>
            <th>Tanggal</th>
            <th>Aksi</th>
        </tr>

        <?php if($result->num_rows > 0): ?>
            <?php while($row = $result->fetch_assoc()): ?>
            <tr>
                <td><?= $row['nomor_kamar'] ?></td>
                <td><?= $row['nama'] ?></td>
                <td><?= $row['kategori'] ?></td>
                <td class="status-<?= $row['status'] ?>">
                    <?= ucfirst($row['status']) ?>
                </td>
                <td><?= date('d M Y', strtotime($row['tanggal_lapor'])) ?></td>
                <td>
                    <a href="detail_laporan.php?id=<?= $row['id'] ?>" class="btn-detail">
                        Detail
                    </a>
                </td>
            </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr>
                <td colspan="6" style="text-align:center; padding:20px;">
                    Tidak ada data laporan
                </td>
            </tr>
        <?php endif; ?>
    </table>
    </div>

</div>
<?php include "../inc/footer.php"; ?>

</body>
</html>