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

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

$penghuni_id = intval($_GET['id']);

/* =========================
   AMBIL DATA PENGHUNI
========================= */
$stmt = $conn->prepare("SELECT nama FROM penghuni WHERE id=?");
$stmt->bind_param("i",$penghuni_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();

/* =========================
   AMBIL RIWAYAT KAMAR
========================= */
$riwayat = $conn->prepare("
SELECT kamar.nomor_kamar, riwayat_kamar.tanggal_masuk, riwayat_kamar.tanggal_keluar
FROM riwayat_kamar
JOIN kamar ON riwayat_kamar.kamar_id = kamar.id
WHERE riwayat_kamar.penghuni_id=?
ORDER BY tanggal_masuk DESC
");

$riwayat->bind_param("i",$penghuni_id);
$riwayat->execute();
$data = $riwayat->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 Kamar Penghuni</title>

<style>

.container{
padding:30px;
}

.section{
background:white;
padding:20px;
border-radius:8px;
box-shadow:0 4px 10px rgba(0,0,0,0.05);
}

table{
width:100%;
border-collapse:collapse;
margin-top:15px;
}

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

th{
background:#f1f5f9;
}

</style>

</head>

<body>

<div class="header">
<?php include "../inc/navbar_admin.php"; ?>
</div>

<div class="container">

<div class="section">

<h3>Riwayat Kamar: <?= htmlspecialchars($user['nama']) ?></h3>

<table>

<tr>
<th>Kamar</th>
<th>Tanggal Masuk</th>
<th>Tanggal Keluar</th>
</tr>

<?php while($row = $data->fetch_assoc()): ?>

<tr>

<td><?= $row['nomor_kamar'] ?></td>

<td>
<?= date('d-m-Y',strtotime($row['tanggal_masuk'])) ?>
</td>

<td>

<?php
if($row['tanggal_keluar']){
echo date('d-m-Y',strtotime($row['tanggal_keluar']));
}else{
echo "Masih Menempati";
}
?>

</td>

</tr>

<?php endwhile; ?>

</table>

<br>

<a href="penghuni.php">← Kembali</a>

</div>

</div>

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

</body>
</html>