<?php
session_start();
require_once "../config/db.php";

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

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

/* 🔥 AMBIL NOMOR KAMAR */
$kamar = $conn->query("
    SELECT nomor_kamar 
    FROM kamar 
    WHERE id = $id
")->fetch_assoc();

$nomor_kamar = $kamar['nomor_kamar'] ?? '-';

/* ==========================
   DATA HISTORI
========================== */
$data = $conn->query("
SELECT * FROM ac_history 
WHERE kamar_id = $id
ORDER BY tanggal_service DESC
");
?>

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

<style>
.container{
padding:20px;
}

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

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

table{
width:100%;
border-collapse:collapse;
min-width:600px;
}

th, td{
padding:10px;
border-bottom:1px solid #ddd;
text-align:left;
font-size:14px;
white-space:nowrap;
}

th{
background:#1e3a8a;
color:white;
}

.back-btn{
display:inline-block;
margin-bottom:15px;
padding:8px 12px;
background:#2563eb;
color:white;
text-decoration:none;
border-radius:5px;
}

.back-btn:hover{
background:#1e40af;
}
</style>

</head>

<body>

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

<div class="container">

<a href="ac_service.php" class="back-btn">← Kembali</a>

<div class="section">

<h3>Histori AC Kamar <?= $nomor_kamar ?></h3>

<p>Total Riwayat: <b><?= $data->num_rows ?></b></p>

<div class="table-wrapper">

<table>
<tr>
<th>Tanggal Service</th>
<th>Next Service</th>
<th>Biaya</th>
<th>Catatan</th>
</tr>

<?php while($h = $data->fetch_assoc()): ?>
<tr>
<td><?= $h['tanggal_service'] ?></td>
<td><?= $h['next_service'] ?></td>
<td>Rp <?= number_format($h['biaya']) ?></td>
<td><?= $h['catatan'] ?? '-' ?></td>
</tr>
<?php endwhile; ?>

</table>

</div>

</div>

</div>

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

</body>
</html>