<?php
session_start();

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

/* ==========================
   CEK LOGIN ADMIN
========================== */
if (!isset($_SESSION['admin_id'])) {
    header("Location: ../login.php");
    exit;
}

/* ==========================
   AMBIL DAFTAR KAMAR
========================== */
$kamar = $conn->query("SELECT id, nomor_kamar FROM kamar ORDER BY nomor_kamar ASC");

/* ==========================
   KAMAR DIPILIH
========================== */
$kamar_id = $_GET['kamar_id'] ?? '';

$riwayat = null;

if ($kamar_id != "") {

    $stmt = $conn->prepare("
        SELECT nama,no_hp,tanggal_masuk,tanggal_keluar,status
        FROM penghuni
        WHERE kamar_id=?
        ORDER BY tanggal_masuk ASC
    ");

    $stmt->bind_param("i", $kamar_id);
    $stmt->execute();

    $riwayat = $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 Kamar - 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;
background:#f4f6f9;
}

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

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

.container{
padding:30px;
}

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

select{
padding:8px;
margin-right:10px;
}

button{
padding:8px 15px;
background:#2563eb;
color:white;
border:none;
border-radius:5px;
cursor:pointer;
}

/* =====================
   RESPONSIVE TABLE
===================== */

.table-responsive{
width:100%;
overflow-x:auto;
}

table{
width:100%;
min-width:650px;
border-collapse:collapse;
margin-top:20px;
}

th,td{
padding:12px;
border-bottom:1px solid #ddd;
text-align:center;
white-space:nowrap;
}

th{
background:#f1f5f9;
}

tbody tr:nth-child(even){
background:#f9fafb;
}

tbody tr:hover{
background:#eef2ff;
}

</style>

</head>

<body>

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

<div class="page-title">
    <h1>Riwayat Kamar</h1>
    <p>Monitoring Riwayat Kamar kost LuminaCare</p>
</div>

<div class="container">

<div class="section">

<h3>Pilih Kamar</h3>

<form method="GET">

<select name="kamar_id" required>

<option value="">Pilih Kamar</option>

<?php while($k = $kamar->fetch_assoc()): ?>

<option value="<?= $k['id'] ?>"
<?= ($kamar_id==$k['id']) ? 'selected' : '' ?>>

Kamar <?= $k['nomor_kamar'] ?>

</option>

<?php endwhile; ?>

</select>

<button type="submit">Lihat Riwayat</button>

</form>

</div>


<?php if($riwayat): ?>

<div class="section">

<h3>Riwayat Penghuni</h3>

<div class="table-responsive">

<table>

<thead>

<tr>
<th>Nama</th>
<th>No HP</th>
<th>Tanggal Masuk</th>
<th>Tanggal Keluar</th>
<th>Status</th>
<th>Lama Tinggal</th>
</tr>

</thead>

<tbody>

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

<tr>

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

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

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

<td>

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

</td>

<td><?= ucfirst($row['status']) ?></td>

<td>

<?php

$masuk = strtotime($row['tanggal_masuk']);

if(!empty($row['tanggal_keluar'])){
$keluar = strtotime($row['tanggal_keluar']);
}else{
$keluar = time();
}

$selisih = $keluar - $masuk;

$hari = floor($selisih / (60*60*24));

echo $hari." hari";

?>

</td>

</tr>

<?php endwhile; ?>

</tbody>

</table>

</div>

</div>

<?php endif; ?>

</div>

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

</body>
</html>