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

$penghuni_id = $_SESSION['penghuni_id'];

/* ===============================
   RESET NOTIFIKASI SAAT DIBUKA
================================ */
$conn->query("
    UPDATE laporan 
    SET notifikasi=0 
    WHERE penghuni_id=$penghuni_id
");

/* ===============================
   AMBIL DATA LAPORAN
================================ */
$stmt = $conn->prepare("
    SELECT laporan.*, kamar.nomor_kamar
    FROM laporan
    JOIN kamar ON laporan.kamar_id = kamar.id
    WHERE laporan.penghuni_id = ?
    ORDER BY laporan.tanggal_lapor DESC
");

$stmt->bind_param("i", $penghuni_id);
$stmt->execute();
$result = $stmt->get_result();
?>

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

<style>
body{
    margin:0;
    font-family:Arial;
    background:#f4f6f9;
}

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

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

.container{
    padding:30px;
}

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{
    padding:6px 10px;
    background:#2563eb;
    color:white;
    text-decoration:none;
    border-radius:4px;
    font-size:12px;
}

.empty{
    background:white;
    padding:20px;
    text-align:center;
    box-shadow:0 4px 10px rgba(0,0,0,0.05);
}

/* MOBILE */
@media (max-width:768px){
    .header{
        flex-direction:column;
        align-items:flex-start;
    }

    .container{
        padding:15px;
    }

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

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

<?php include "../inc/navbar_penghuni.php"; ?>


<div class="container">

    <?php if($result->num_rows > 0): ?>
    <table>
        <tr>
            <th>Kamar</th>
            <th>Kategori</th>
            <th>Status</th>
            <th>Tanggal</th>
            <th>Aksi</th>
        </tr>

        <?php while($row = $result->fetch_assoc()): ?>
        <tr>
            <td><?= $row['nomor_kamar'] ?></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; ?>

    </table>

    <?php else: ?>
        <div class="empty">
            Belum ada laporan yang dibuat.
        </div>
    <?php endif; ?>

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

</body>
</html>