<?php
include "../config/koneksi.php";
include "../middleware/auth.php";
$adminNotifCount = (int) (mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) AS jml FROM notifikasi WHERE role_tujuan='admin' AND is_read=0"))['jml'] ?? 0);

if($_SESSION['role'] != 'admin'){
    die("Akses ditolak");
}

// ================== STATISTIK ==================
$total = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) as jml FROM komplain"))['jml'];
$pending = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) as jml FROM komplain WHERE status='Pending'"))['jml'];
$proses = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) as jml FROM komplain WHERE status='Proses'"))['jml'];
$selesai = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) as jml FROM komplain WHERE status='Selesai'"))['jml'];

// ================== CHART STATUS ==================
$q = mysqli_query($conn,"SELECT status, COUNT(*) jml FROM komplain GROUP BY status");
$label=[]; $data=[];
while($d=mysqli_fetch_assoc($q)){
    $label[]=$d['status'];
    $data[]=$d['jml'];
}

// ================== CHART BULAN ==================
$q2 = mysqli_query($conn,"
SELECT MONTH(tanggal) bulan, COUNT(*) jml 
FROM komplain GROUP BY MONTH(tanggal)
");
$bulan=[]; $jml_bulan=[];
while($d=mysqli_fetch_assoc($q2)){
    $bulan[]="Bulan ".$d['bulan'];
    $jml_bulan[]=$d['jml'];
}

// ================== DATA TABEL ==================
$dataKomplain = mysqli_query($conn,"SELECT * FROM komplain ORDER BY id DESC");
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dashboard Admin | PT. ALVINDO</title>

<!-- ADMINLTE -->
<link rel="stylesheet" href="../adminlte/plugins/fontawesome-free/css/all.min.css">
<link rel="stylesheet" href="../adminlte/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
<link rel="stylesheet" href="../adminlte/dist/css/adminlte.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">

<!-- JS -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<link rel="stylesheet" href="../assets/sikompro-ux.css?v=20260722.11">
</head>

<body class="hold-transition sidebar-mini layout-fixed dashboard-shell">
<div class="wrapper">

<!-- NAVBAR -->
<nav class="main-header navbar navbar-expand navbar-white navbar-light">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" data-widget="pushmenu"><i class="fas fa-bars"></i></a>
</li>
</ul>
</nav>

<!-- SIDEBAR -->
<aside class="main-sidebar sidebar-dark-primary elevation-4">
<a class="brand-link">
<span class="brand-text">PT. ALVINDO</span>
</a>

<div class="sidebar">
<nav>
<ul class="nav nav-pills nav-sidebar flex-column">

<li class="nav-item">
<a class="nav-link active"><i class="nav-icon fas fa-home"></i> Dashboard</a>
</li>

<li class="nav-item">
<a href="cetak_pdf.php" class="nav-link"><i class="nav-icon fas fa-print"></i> Cetak PDF</a>
</li>

<li class="nav-item">
<a href="notif.php" class="nav-link"><i class="nav-icon fas fa-bell"></i> Notifikasi <span class="badge badge-warning float-right"><?= $adminNotifCount ?></span></a>
</li>

<li class="nav-item">
<a href="../middleware/logout.php" class="nav-link"><i class="nav-icon fas fa-sign-out-alt"></i> Logout</a>
</li>

</ul>
</nav>
</div>
</aside>

<!-- CONTENT -->
<div class="content-wrapper">
<section class="content">
<div class="container-fluid">

<h3>Dashboard Admin</h3>
<p>Selamat datang, <b><?= $_SESSION['username'] ?></b></p>

<!-- CARD -->
<div class="row">

<div class="col-lg-3">
<div class="small-box bg-info">
<div class="inner">
<h3><?= $total ?></h3>
<p>Total Komplain</p>
</div>
</div>
</div>

<div class="col-lg-3">
<div class="small-box bg-warning">
<div class="inner">
<h3><?= $pending ?></h3>
<p>Pending</p>
</div>
</div>
</div>

<div class="col-lg-3">
<div class="small-box bg-primary">
<div class="inner">
<h3><?= $proses ?></h3>
<p>Proses</p>
</div>
</div>
</div>

<div class="col-lg-3">
<div class="small-box bg-success">
<div class="inner">
<h3><?= $selesai ?></h3>
<p>Selesai</p>
</div>
</div>
</div>

<?php if($d['status'] == 'menunggu_admin'): ?>
<form method="post" action="terima_admin.php">
    <input type="hidden" name="id" value="<?= $d['id'] ?>">
    <button class="btn btn-success mt-2">
        <i class="fas fa-check"></i> Terima dari Pelaksana
    </button>
</form>
<?php endif; ?>

</div>

<!-- CHART -->
<div class="row">

<div class="col-md-6">
<div class="card">
<div class="card-header">Grafik Status</div>
<div class="card-body">
<canvas id="chartStatus"></canvas>
</div>
</div>
</div>

<div class="col-md-6">
<div class="card">
<div class="card-header">Grafik Bulanan</div>
<div class="card-body">
<canvas id="chartBulan"></canvas>
</div>
</div>
</div>

</div>

<!-- DATATABLE -->
<div class="card">
<div class="card-header">Data Komplain</div>
<div class="card-body">

<table id="example" class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Lokasi</th>
<th>Kerusakan</th>
<th>Prioritas</th>
<th>Status</th>
<th>Tanggal</th>
</tr>
</thead>

<tbody>
<?php $no=1; while($d=mysqli_fetch_assoc($dataKomplain)){ ?>
<tr>
<td><?= $no++ ?></td>
<td><?= $d['lokasi'] ?></td>
<td><?= $d['kerusakan'] ?></td>
<td><?= $d['prioritas'] ?></td>
<td><?= $d['status'] ?></td>
<td><?= $d['tanggal'] ?></td>
</tr>
<?php } ?>
</tbody>

</table>

</div>
</div>

</div>
</section>
</div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
<script src="../assets/bootstrap5-compat.js"></script>
<script src="../assets/sikompro-theme.js?v=20260722.11"></script>
<script>
// chart status
new Chart(document.getElementById('chartStatus'), {
type:'doughnut',
data:{
labels: <?= json_encode($label) ?>,
datasets:[{data: <?= json_encode($data) ?>}]
}
});

// chart bulan
new Chart(document.getElementById('chartBulan'), {
type:'line',
data:{
labels: <?= json_encode($bulan) ?>,
datasets:[{data: <?= json_encode($jml_bulan) ?>}]
}
});
</script>

<?php include '../partials/mobile_nav.php'; ?>
</body>
</html>













