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

$id = $_GET['id'] ?? 0;
$penghuni_id = $_SESSION['penghuni_id'];

$stmt = $conn->prepare("
    SELECT laporan.*, kamar.nomor_kamar
    FROM laporan
    JOIN kamar ON laporan.kamar_id = kamar.id
    WHERE laporan.id=? AND laporan.penghuni_id=?
");
$stmt->bind_param("ii", $id, $penghuni_id);
$stmt->execute();
$data = $stmt->get_result()->fetch_assoc();

if(!$data){
    die("Data tidak ditemukan");
}

$fotos = [];
if(!empty($data['foto'])){
    $fotos = explode(",", $data['foto']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Detail Laporan</title>
<style>
body{
    margin:0;
    font-family:Arial;
    background:#f4f6f9;
}

.container{
    max-width:700px;
    margin:40px auto;
    background:white;
    padding:30px;
    border-radius:10px;
    box-shadow:0 5px 20px rgba(0,0,0,0.1);
}

h2{ margin-bottom:20px; }

.info{ margin-bottom:10px; }

.label{
    font-weight:bold;
    width:120px;
    display:inline-block;
}

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

/* GRID FOTO */
.photo-grid{
    display:flex;
    flex-wrap:wrap;
    gap:15px;
    margin-top:15px;
}

.thumbnail{
    width:150px;
    height:150px;
    object-fit:cover;
    border-radius:8px;
    cursor:pointer;
    transition:0.3s;
    box-shadow:0 3px 10px rgba(0,0,0,0.1);
}

.thumbnail:hover{
    transform:scale(1.05);
    opacity:0.9;
}

/* MODAL */
.modal{
    display:none;
    position:fixed;
    z-index:999;
    padding-top:60px;
    left:0;
    top:0;
    width:100%;
    height:100%;
    background:rgba(0,0,0,0.8);
}

.modal-content{
    display:block;
    margin:auto;
    max-width:85%;
    max-height:85%;
    border-radius:10px;
}

.close{
    position:absolute;
    top:20px;
    right:35px;
    color:white;
    font-size:40px;
    cursor:pointer;
}

.back{
    display:inline-block;
    margin-top:20px;
    text-decoration:none;
    color:#2563eb;
}
</style>
</head>
<body>

<div class="container">

<h2>Detail Laporan</h2>

<div class="info"><span class="label">Kamar:</span> <?= htmlspecialchars($data['nomor_kamar']) ?></div>
<div class="info"><span class="label">Kategori:</span> <?= htmlspecialchars($data['kategori']) ?></div>
<div class="info"><span class="label">Deskripsi:</span> <?= htmlspecialchars($data['deskripsi']) ?></div>

<div class="info">
<span class="label">Status:</span> 
<span class="status-<?= $data['status'] ?>">
    <?= ucfirst($data['status']) ?>
</span>
</div>

<?php if(count($fotos) > 0): ?>
<div class="photo-grid">
    <?php foreach($fotos as $foto): ?>
        <img src="../uploads/<?= htmlspecialchars(trim($foto)) ?>" class="thumbnail">
    <?php endforeach; ?>
</div>
<?php else: ?>
<p><i>Tidak ada foto</i></p>
<?php endif; ?>

<a href="dashboard.php" class="back">← Kembali</a>

</div>

<!-- MODAL -->
<div id="imgModal" class="modal">
<span class="close">&times;</span>
<img class="modal-content" id="modalImg">
</div>

<script>
const modal = document.getElementById("imgModal");
const modalImg = document.getElementById("modalImg");
const closeBtn = document.querySelector(".close");

document.querySelectorAll(".thumbnail").forEach(img => {
    img.onclick = function(){
        modal.style.display = "block";
        modalImg.src = this.src;
    }
});

closeBtn.onclick = function(){
    modal.style.display = "none";
};

window.onclick = function(e){
    if(e.target == modal){
        modal.style.display = "none";
    }
};
</script>
<?php include "../inc/footer.php"; ?>

</body>
</html>