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

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

/* ==========================
   FILTER
========================== */
$where = "";
if(isset($_GET['filter'])){
    if($_GET['filter'] == 'overdue'){
        $where = "WHERE next_service_ac <= CURDATE()";
    } elseif($_GET['filter'] == 'soon'){
        $where = "WHERE next_service_ac BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)";
    } elseif($_GET['filter'] == 'month'){
        $where = "WHERE next_service_ac BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 30 DAY)";
    }
}

/* ==========================
   ACTION PER KAMAR
========================== */

if(isset($_GET['reset'])){
    $id = intval($_GET['reset']);
    $conn->query("UPDATE kamar SET last_service_ac=NULL, next_service_ac=NULL WHERE id=$id");
    header("Location: ac_service.php"); exit;
}

/* INPUT */
if(isset($_POST['save'])){
    $id = intval($_POST['kamar_id']);
    $tanggal = $_POST['tanggal'];
    $next = date('Y-m-d', strtotime('+3 months', strtotime($tanggal)));

    $conn->query("UPDATE kamar SET last_service_ac='$tanggal', next_service_ac='$next' WHERE id=$id");

    $conn->query("
        INSERT INTO ac_history (kamar_id, tanggal_service, next_service)
        VALUES ($id, '$tanggal', '$next')
    ");

    header("Location: ac_service.php"); exit;
}

/* DONE */
if (isset($_GET['done'])) {
    $id = intval($_GET['done']);
    $today = date('Y-m-d');
    $next  = date('Y-m-d', strtotime('+3 months'));

    $conn->query("UPDATE kamar SET last_service_ac='$today', next_service_ac='$next' WHERE id=$id");

    $conn->query("
        INSERT INTO ac_history (kamar_id, tanggal_service, next_service)
        VALUES ($id, '$today', '$next')
    ");

    header("Location: ac_service.php"); exit;
}

/* DATA */
$data = $conn->query("SELECT * FROM kamar $where ORDER BY nomor_kamar ASC");
?>

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

<style>
body{font-family:Arial;background:#f4f6f9;margin:0;}
.container{padding:20px;}
.section{background:#fff;padding:15px;border-radius:8px;margin-bottom:20px;}

.btn{
padding:10px;
border:none;
border-radius:6px;
color:#fff;
cursor:pointer;
font-size:14px;
width:100%;
}

.btn-input{background:#16a34a;}
.btn{background:#2563eb;}
.btn-reset{background:#dc2626;}

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

.back-btn:hover{
background:#1e40af;
}

/* FILTER */
.filter{
display:flex;
gap:10px;
flex-wrap:wrap;
margin-bottom:15px;
}

.filter a{
padding:8px 12px;
background:#e5e7eb;
border-radius:6px;
text-decoration:none;
font-size:13px;
color:#111;
}

.filter a.active{
background:#2563eb;
color:white;
}

table{width:100%;border-collapse:collapse;}
th,td{padding:10px;border-bottom:1px solid #ddd;}
th{background:#1e3a8a;color:white;}

.status{padding:5px 10px;border-radius:5px;color:white;font-size:12px;}
.overdue{background:#dc2626;}
.soon{background:#f59e0b;}
.safe{background:#16a34a;}

.card-list{display:none;}

.card{
background:#fff;
padding:15px;
margin-bottom:10px;
border-radius:8px;
box-shadow:0 2px 5px rgba(0,0,0,0.05);
}

.btn-group{
display:flex;
flex-direction:column;
gap:8px;
margin-top:10px;
}

@media(max-width:768px){
table{display:none;}
.card-list{display:block;}
}
</style>
</head>

<body>

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

<div class="container">

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

<div class="section">

<h3>Maintenance AC</h3>

<!-- FILTER -->
<div class="filter">
<a href="ac_service.php" class="<?= !isset($_GET['filter']) ? 'active' : '' ?>">Semua</a>
<a href="?filter=overdue" class="<?= ($_GET['filter'] ?? '')=='overdue' ? 'active' : '' ?>">Overdue</a>
<a href="?filter=soon" class="<?= ($_GET['filter'] ?? '')=='soon' ? 'active' : '' ?>">7 Hari</a>
<a href="?filter=month" class="<?= ($_GET['filter'] ?? '')=='month' ? 'active' : '' ?>">30 Hari</a>
</div>

<!-- DESKTOP -->
<table>
<tr>
<th>Kamar</th>
<th>Status</th>
<th>Last</th>
<th>Next</th>
<th>Aksi</th>
</tr>

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

$status_class="safe"; $status_text="Aman";

if($k['next_service_ac']){
    if($k['next_service_ac'] <= date('Y-m-d')){
        $status_class="overdue"; $status_text="Overdue";
    } elseif($k['next_service_ac'] <= date('Y-m-d', strtotime('+7 days'))){
        $status_class="soon"; $status_text="Segera";
    }
}else{
    $status_class="overdue"; $status_text="Belum";
}
?>

<tr>
<td><?= $k['nomor_kamar'] ?></td>
<td><span class="status <?= $status_class ?>"><?= $status_text ?></span></td>
<td><?= $k['last_service_ac'] ?? '-' ?></td>
<td><?= $k['next_service_ac'] ?? '-' ?></td>
<td>

<?php if(empty($k['last_service_ac'])): ?>
<button class="btn btn-input" onclick="openModal(<?= $k['id'] ?>)">Input</button>
<?php else: ?>
<a href="?done=<?= $k['id'] ?>" class="btn">Selesai</a>
<a href="?reset=<?= $k['id'] ?>" class="btn btn-reset">Reset</a>
<?php endif; ?>

<a href="ac_history.php?kamar_id=<?= $k['id'] ?>" class="btn">Histori</a>

</td>
</tr>

<?php endwhile; ?>
</table>

<!-- MOBILE -->
<div class="card-list">
<?php 
$data->data_seek(0);
while($k = $data->fetch_assoc()): 
?>

<div class="card">

<b>Kamar <?= $k['nomor_kamar'] ?></b><br><br>

Status:
<span class="status"><?= $status_text ?></span><br><br>

Last: <?= $k['last_service_ac'] ?? '-' ?><br>
Next: <?= $k['next_service_ac'] ?? '-' ?><br>

<div class="btn-group">

<a href="?done=<?= $k['id'] ?>" class="btn">Selesai</a>
<a href="?reset=<?= $k['id'] ?>" class="btn btn-reset">Reset</a>
<a href="ac_history.php?kamar_id=<?= $k['id'] ?>" class="btn">Histori</a>

</div>

</div>

<?php endwhile; ?>
</div>

</div>

</div>

<!-- MODAL -->
<div id="modal" style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.5);">
<div style="background:white;padding:20px;width:300px;margin:100px auto;border-radius:8px;">

<h3>Input Jadwal</h3>

<form method="POST">
<input type="hidden" name="kamar_id" id="kamar_id">
<input type="date" name="tanggal" required><br><br>

<button type="submit" name="save" class="btn">Simpan</button>
<button type="button" onclick="closeModal()" class="btn">Batal</button>
</form>

</div>
</div>

<script>
function openModal(id){
document.getElementById("modal").style.display="block";
document.getElementById("kamar_id").value=id;
}
function closeModal(){
document.getElementById("modal").style.display="none";
}
</script>
<?php include "../inc/footer.php"; ?>

</body>
</html>