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

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

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

$kamar_id = $_GET['kamar_id'] ?? '';

$laporan = null;
$total_biaya = 0;
$total_laporan = 0;

if ($kamar_id != "") {

    $stmt = $conn->prepare("
        SELECT laporan.*, kamar.nomor_kamar
        FROM laporan
        JOIN kamar ON laporan.kamar_id = kamar.id
        WHERE laporan.kamar_id = ?
        ORDER BY laporan.tanggal_lapor DESC
    ");

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

    /* total biaya */
    $sum = $conn->prepare("
        SELECT COUNT(*) as total_laporan,
        SUM(biaya) as total_biaya
        FROM laporan
        WHERE kamar_id=?
        AND status='selesai'
    ");

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

    $data = $sum->get_result()->fetch_assoc();

    $total_biaya = $data['total_biaya'] ?? 0;
    $total_laporan = $data['total_laporan'] ?? 0;
}
?>

<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../assets/style.css">

<title>Riwayat Laporan 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;
}

.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:700px;
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;
}

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

.summary{
margin-top:10px;
font-weight:bold;
line-height:1.6;
}

</style>

</head>

<body>

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

<div class="page-title">
<h1>Riwayat Laporan Kamar</h1>
<p>Monitoring Riwayat Laporan 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($laporan): ?>

<div class="section">

<h3>Riwayat Laporan</h3>

<div class="summary">
Total Laporan : <?= $total_laporan ?><br>
Total Biaya Maintenance : Rp <?= number_format($total_biaya) ?>
</div>

<div class="table-responsive">

<table>

<thead>
<tr>
<th>Tanggal</th>
<th>Kategori</th>
<th>Deskripsi</th>
<th>Status</th>
<th>Biaya</th>
</tr>
</thead>

<tbody>

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

<tr>

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

<td><?= htmlspecialchars($row['kategori']) ?></td>

<td><?= htmlspecialchars($row['deskripsi']) ?></td>

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

<td>
<?php
if($row['biaya']>0){
echo "Rp ".number_format($row['biaya']);
}else{
echo "-";
}
?>
</td>

</tr>

<?php endwhile; ?>

</tbody>

</table>

</div>

</div>

<?php endif; ?>

</div>

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

</body>
</html>