<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . "/../config/db.php";
require_once __DIR__ . "/../inc/auth_penghuni.php";

/* Ambil data penghuni */
$id = $_SESSION['penghuni_id'];

$stmt = $conn->prepare("
    SELECT penghuni.*, kamar.nomor_kamar 
    FROM penghuni
    JOIN kamar ON penghuni.kamar_id = kamar.id
    WHERE penghuni.id=?
");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();

/* Hitung jumlah laporan */
$laporan = $conn->prepare("
    SELECT COUNT(*) as total 
    FROM laporan 
    WHERE penghuni_id=?
");
$laporan->bind_param("i", $id);
$laporan->execute();
$total_laporan = $laporan->get_result()->fetch_assoc()['total'];

/* ===============================
   CEK NOTIFIKASI UPDATE STATUS
================================ */
$notif = $conn->prepare("
    SELECT COUNT(*) as total 
    FROM laporan 
    WHERE penghuni_id=? AND notifikasi=1
");
$notif->bind_param("i", $id);
$notif->execute();
$total_notif = $notif->get_result()->fetch_assoc()['total'];
?>

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

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

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

.container {
    padding:30px;
}

.card {
    background:white;
    padding:20px;
    border-radius:10px;
    box-shadow:0 4px 10px rgba(0,0,0,0.1);
    margin-bottom:20px;
}

/* NOTIFIKASI */
.notif-box {
    background:#f59e0b;
    color:white;
    padding:15px;
    border-radius:8px;
    margin-bottom:20px;
    font-weight:bold;
}

/* BUTTON */
.menu-buttons {
    display:flex;
    flex-wrap:wrap;
    gap:10px;
}

.btn {
    display:inline-block;
    padding:10px 15px;
    border-radius:5px;
    text-decoration:none;
    font-size:14px;
    color:white;
    text-align:center;
    position:relative;
}

.btn-primary { background:#2563eb; }
.btn-warning { background:#f59e0b; }
.btn-danger { background:#dc2626; }

.btn:hover { opacity:0.85; }

/* BADGE MERAH */
.badge {
    position:absolute;
    top:-6px;
    right:-6px;
    background:red;
    color:white;
    font-size:11px;
    padding:3px 6px;
    border-radius:50%;
}

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

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

    .container {
        padding:15px;
    }

    .menu-buttons {
        flex-direction:column;
    }

    .btn {
        width:100%;
    }

    .badge {
        right:10px;
        top:5px;
    }
}
</style>
</head>

<body>

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

<div class="container">

    <?php if($total_notif > 0): ?>
        <div class="notif-box">
            🔔 Ada <?= $total_notif ?> update status laporan terbaru.
            <a href="riwayat_laporan.php" 
               style="color:white;text-decoration:underline;margin-left:10px;">
                Lihat Sekarang
            </a>
        </div>
    <?php endif; ?>

    <div class="card">
        <h3>Informasi Penghuni</h3>
        <p><b>Nama:</b> <?= $user['nama'] ?></p>
        <p><b>No HP:</b> <?= $user['no_hp'] ?></p>
        <p><b>Kamar:</b> <?= $user['nomor_kamar'] ?></p>
        <p><b>Total Laporan:</b> <?= $total_laporan ?></p>
    </div>

    <div class="card">
        <h3>Menu</h3>

        <div class="menu-buttons">
            <a href="buat_laporan.php" class="btn btn-primary">
                Buat Laporan Kerusakan
            </a>

            <a href="riwayat_laporan.php" class="btn btn-primary">
                Riwayat Laporan
                <?php if($total_notif > 0): ?>
                    <span class="badge"><?= $total_notif ?></span>
                <?php endif; ?>
            </a>

            <a href="ganti_password.php" class="btn btn-warning">
                Ganti Password
            </a>

            <a href="../logout.php" class="btn btn-danger">
                Logout
            </a>
        </div>
    </div>

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

</body>
</html>