<?php
session_start();

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

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

/* ==========================
   STATISTIK LAPORAN
========================== */
$total_baru = $conn->query("SELECT COUNT(*) as total FROM laporan WHERE status='baru'")
    ->fetch_assoc()['total'];

$total_diproses = $conn->query("SELECT COUNT(*) as total FROM laporan WHERE status='diproses'")
    ->fetch_assoc()['total'];

$total_selesai = $conn->query("SELECT COUNT(*) as total FROM laporan WHERE status='selesai'")
    ->fetch_assoc()['total'];

/* ==========================
   BULAN INI
========================== */
$bulan_ini = date('m');
$tahun_ini = date('Y');

$total_bulan = $conn->query("
    SELECT COUNT(*) as total 
    FROM laporan 
    WHERE MONTH(tanggal_lapor)='$bulan_ini' 
    AND YEAR(tanggal_lapor)='$tahun_ini'
")->fetch_assoc()['total'];

$total_biaya_bulan = $conn->query("
    SELECT SUM(biaya) as total 
    FROM laporan 
    WHERE status='selesai'
    AND MONTH(tanggal_lapor)='$bulan_ini'
    AND YEAR(tanggal_lapor)='$tahun_ini'
")->fetch_assoc()['total'] ?? 0;

/* ==========================
   STATISTIK OKUPANSI
========================== */
$total_kamar = $conn->query("SELECT COUNT(*) as total FROM kamar")
    ->fetch_assoc()['total'];

$total_terisi = $conn->query("SELECT COUNT(*) as total FROM kamar WHERE status='terisi'")
    ->fetch_assoc()['total'];

$total_kosong = $conn->query("SELECT COUNT(*) as total FROM kamar WHERE status='kosong'")
    ->fetch_assoc()['total'];

$total_maintenance = $conn->query("SELECT COUNT(*) as total FROM kamar WHERE status='maintenance'")
    ->fetch_assoc()['total'];

$okupansi = 0;
if ($total_kamar > 0) {
    $okupansi = round(($total_terisi / $total_kamar) * 100);
}

/* ==========================
   JADWAL AC
========================== */
$total_ac_overdue = $conn->query("
    SELECT COUNT(*) as total 
    FROM kamar 
    WHERE next_service_ac IS NOT NULL 
    AND next_service_ac <= CURDATE()
")->fetch_assoc()['total'];

$total_ac_soon = $conn->query("
    SELECT COUNT(*) as total 
    FROM kamar 
    WHERE next_service_ac IS NOT NULL 
    AND next_service_ac BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)
")->fetch_assoc()['total'];

$total_ac_30 = $conn->query("
    SELECT COUNT(*) as total 
    FROM kamar 
    WHERE next_service_ac IS NOT NULL 
    AND next_service_ac BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 30 DAY)
")->fetch_assoc()['total'];

/* LAST SERVICE GLOBAL */
$last_service_global = $conn->query("
    SELECT MAX(last_service_ac) as last_service 
    FROM kamar
")->fetch_assoc()['last_service'];

/* KAMAR TERAKHIR SERVICE */
$last_kamar = $conn->query("
    SELECT nomor_kamar, last_service_ac 
    FROM kamar 
    WHERE last_service_ac IS NOT NULL 
    ORDER BY last_service_ac DESC 
    LIMIT 1
")->fetch_assoc();
?>

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

<style>

body{
margin:0;
font-family:Arial, sans-serif;
background:#f4f6f9;
}

.container{
padding:30px;
}

.cards{
display:flex;
gap:20px;
margin-bottom:30px;
flex-wrap:wrap;
}

.card-link{
text-decoration:none;
flex:1;
min-width:180px;
}

.card{
padding:20px;
border-radius:10px;
color:white;
font-size:18px;
font-weight:bold;
box-shadow:0 4px 10px rgba(0,0,0,0.1);
transition:0.2s ease;
cursor:pointer;
}

.card:hover{
transform:translateY(-5px);
box-shadow:0 8px 20px rgba(0,0,0,0.15);
opacity:0.95;
}

.card small{
display:block;
margin-top:8px;
font-size:13px;
font-weight:normal;
opacity:0.9;
}

.baru{ background:#dc2626; }
.diproses{ background:#f59e0b; }
.selesai{ background:#16a34a; }

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

.row{
display:flex;
gap:20px;
flex-wrap:wrap;
}

.row .section{
flex:1;
min-width:280px;
}

.progress-container{
background:#e5e7eb;
border-radius:20px;
overflow:hidden;
height:20px;
margin-top:10px;
}

.progress-bar{
height:100%;
text-align:center;
color:white;
font-size:12px;
line-height:20px;
}

.progress-green{ background:#16a34a; }
.progress-yellow{ background:#f59e0b; }
.progress-red{ background:#dc2626; }

.link-btn{
display:inline-block;
margin-top:15px;
padding:10px 15px;
background:#1e40af;
color:white;
text-decoration:none;
border-radius:5px;
transition:0.2s;
}

.link-btn:hover{
opacity:0.9;
}

/* MOBILE */
@media(max-width:768px){

.container{
padding:15px;
}

.cards{
gap:12px;
}

.card-link{
min-width:calc(50% - 6px);
}

.card{
font-size:16px;
padding:18px;
}

.row{
flex-direction:column;
}

.link-btn{
display:block;
margin-bottom:10px;
text-align:center;
}

}

</style>
</head>

<body>

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

<div class="container">

<div class="cards">

<a href="laporan.php?status=baru" class="card-link">
<div class="card baru">
Baru<br><?= $total_baru ?>
<small>Klik lihat laporan baru</small>
</div>
</a>

<a href="laporan.php?status=diproses" class="card-link">
<div class="card diproses">
Diproses<br><?= $total_diproses ?>
<small>Klik lihat laporan diproses</small>
</div>
</a>

<a href="laporan.php?status=selesai" class="card-link">
<div class="card selesai">
Selesai<br><?= $total_selesai ?>
<small>Klik lihat laporan selesai</small>
</div>
</a>

<a href="ac_service.php?filter=overdue" class="card-link">
<div class="card" style="background:#7c3aed;">
AC Overdue<br><?= $total_ac_overdue ?>
<small>AC telat maintenance</small>
</div>
</a>

<a href="ac_service.php?filter=soon" class="card-link">
<div class="card" style="background:#0ea5e9;">
AC 7 Hari Lagi<br><?= $total_ac_soon ?>
<small>Jadwal service mendekat</small>
</div>
</a>

<a href="ac_service.php?filter=month" class="card-link">
<div class="card" style="background:#6366f1;">
AC 30 Hari Lagi<br><?= $total_ac_30 ?>
<small>Jadwal bulan ini</small>
</div>
</a>

</div>

<div class="row">

<div class="section">
<h3>Bulan Ini (<?= date('F Y') ?>)</h3>

<p>Total Laporan: <b><?= $total_bulan ?></b></p>

<p>Total Biaya Maintenance:
<b>Rp <?= number_format($total_biaya_bulan) ?></b>
</p>

</div>

<div class="section">

<h3 style="<?= $total_ac_overdue > 0 ? 'color:red;' : '' ?>">
Maintenance AC
</h3>

<p>
Last Service Terakhir:
<b><?= $last_service_global ? $last_service_global : '-' ?></b>
</p>

<p>
Kamar Terakhir Service:
<b>
<?= $last_kamar ? $last_kamar['nomor_kamar'].' ('.$last_kamar['last_service_ac'].')' : '-' ?>
</b>
</p>

<p>
AC Telat Cuci:
<b style="color:red;"><?= $total_ac_overdue ?></b>
</p>

<p>
Akan Jatuh Tempo (7 hari):
<b style="color:orange;"><?= $total_ac_soon ?></b>
</p>

<p>
30 Hari Lagi:
<b style="color:blue;"><?= $total_ac_30 ?></b>
</p>

<a href="ac_service.php" class="link-btn">
Kelola Jadwal AC
</a>

</div>

</div>

<div class="section">

<h3>Statistik Okupansi</h3>

<p>Total Kamar: <b><?= $total_kamar ?></b></p>
<p>Terisi: <b><?= $total_terisi ?></b></p>
<p>Kosong: <b><?= $total_kosong ?></b></p>
<p>Maintenance: <b><?= $total_maintenance ?></b></p>
<p>Okupansi: <b><?= $okupansi ?>%</b></p>

<div class="progress-container">

<?php
$progress_class="progress-green";

if($okupansi<60){
$progress_class="progress-red";
}elseif($okupansi<80){
$progress_class="progress-yellow";
}
?>

<div class="progress-bar <?= $progress_class ?>" style="width: <?= $okupansi ?>%;">
<?= $okupansi ?>%
</div>

</div>

</div>

<a href="laporan.php" class="link-btn">
Lihat Semua Laporan
</a>

<a href="kamar.php" class="link-btn" style="margin-left:10px;">
Manajemen Kamar
</a>

<a href="penghuni.php" class="link-btn" style="margin-left:10px;">
Kelola Penghuni
</a>

</div>

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

</body>
</html>