<?php
$pageTitle = 'Dashboard';
require_once __DIR__ . '/includes/header.php';

$db = getDB();

$totalSiswa = (int) $db->query('SELECT COUNT(*) FROM siswa')->fetchColumn();
$layak = (int) $db->query("SELECT COUNT(*) FROM siswa WHERE status_kelayakan = 'Layak'")->fetchColumn();
$tidakLayak = (int) $db->query("SELECT COUNT(*) FROM siswa WHERE status_kelayakan = 'Tidak Layak'")->fetchColumn();
$belum = max(0, $totalSiswa - $layak - $tidakLayak);
$sudah = $layak + $tidakLayak;
$latestSiswa = $db->query('SELECT nama, pekerjaan_ortu, status_kelayakan FROM siswa ORDER BY created_at DESC, id DESC LIMIT 5')->fetchAll();

$layakPct = $totalSiswa > 0 ? round(($layak / $totalSiswa) * 100, 1) : 0;
$tidakPct = $totalSiswa > 0 ? round(($tidakLayak / $totalSiswa) * 100, 1) : 0;
$belumPct = $totalSiswa > 0 ? round(($belum / $totalSiswa) * 100, 1) : 0;
$progressPct = $totalSiswa > 0 ? round(($sudah / $totalSiswa) * 100, 1) : 0;

$layakDeg = $totalSiswa > 0 ? ($layak / $totalSiswa) * 360 : 0;
$tidakDeg = $totalSiswa > 0 ? ($tidakLayak / $totalSiswa) * 360 : 0;
$belumDeg = max(0, 360 - $layakDeg - $tidakDeg);
$deg1 = $layakDeg;
$deg2 = $layakDeg + $tidakDeg;
$donutBg = "conic-gradient(#6abf4b 0deg {$deg1}deg, #ef4e4e {$deg1}deg {$deg2}deg, #d9d9d6 {$deg2}deg " . ($deg2 + $belumDeg) . "deg)";

$success = flash('success');
?>

<div class="page-header">
  <h1>Dashboard</h1>
</div>
<div class="page-body">
  <?php if ($success): ?>
    <div class="alert alert-success"><?= e($success) ?></div>
  <?php endif; ?>

  <div class="stats-grid dashboard-stats">
    <div class="stat-card total">
      <div class="stat-info">
        <span>Total Siswa</span>
        <h3><?= $totalSiswa ?></h3>
      </div>
      <div class="stat-icon"><i class="fas fa-user-graduate"></i></div>
    </div>
    <div class="stat-card layak">
      <div class="stat-info">
        <span>Layak</span>
        <h3><?= $layak ?></h3>
        <div class="pct"><?= $layakPct ?>%</div>
      </div>
      <div class="stat-icon"><i class="fas fa-check-circle"></i></div>
    </div>
    <div class="stat-card tidak">
      <div class="stat-info">
        <span>Tidak Layak</span>
        <h3><?= $tidakLayak ?></h3>
        <div class="pct"><?= $tidakPct ?>%</div>
      </div>
      <div class="stat-icon"><i class="fas fa-times-circle"></i></div>
    </div>
    <div class="stat-card belum">
      <div class="stat-info">
        <span>Belum Dihitung</span>
        <h3><?= $belum ?></h3>
        <div class="pct"><?= $belumPct ?>%</div>
      </div>
      <div class="stat-icon"><i class="far fa-clock"></i></div>
    </div>
  </div>

  <div class="dashboard-grid">
    <div class="dashboard-card">
      <h3 class="text-center">Distribusi Klasifikasi</h3>
      <div class="donut-wrap">
        <div class="donut" style="background: <?= e($donutBg) ?>;"></div>
        <div class="legend">
          <div class="legend-item">
            <div class="legend-left"><span class="dot" style="background:#6abf4b"></span><span>Layak</span></div>
            <strong><?= $layak ?> (<?= $layakPct ?>%)</strong>
          </div>
          <div class="legend-item">
            <div class="legend-left"><span class="dot" style="background:#ef4e4e"></span><span>Tidak Layak</span></div>
            <strong><?= $tidakLayak ?> (<?= $tidakPct ?>%)</strong>
          </div>
          <div class="legend-item">
            <div class="legend-left"><span class="dot" style="background:#d9d9d6"></span><span>Belum Dihitung</span></div>
            <strong><?= $belum ?> (<?= $belumPct ?>%)</strong>
          </div>
        </div>
      </div>
    </div>

    <div class="dashboard-stack">
      <div class="dashboard-card">
        <h3>Progres Klasifikasi</h3>
        <div class="progress-row">
          <span>Sudah diklasifikasi</span>
          <strong><?= $sudah ?> / <?= $totalSiswa ?></strong>
        </div>
        <div class="progress-line">
          <span style="width: <?= min(100, $progressPct) ?>%;"></span>
        </div>
        <p class="muted" style="margin-top:0.6rem">
          <?= $belum ?> siswa belum dihitung - segera selesaikan klasifikasi
        </p>
      </div>

      <div class="dashboard-card">
        <h3>Data Siswa Terbaru</h3>
        <table class="mini-table">
          <thead>
            <tr>
              <th>Nama</th>
              <th>Pekerjaan Orang Tua</th>
              <th>Hasil</th>
            </tr>
          </thead>
          <tbody>
            <?php if (empty($latestSiswa)): ?>
            <tr>
              <td colspan="3" class="muted">Belum ada data siswa.</td>
            </tr>
            <?php endif; ?>
            <?php foreach ($latestSiswa as $row): ?>
            <tr>
              <td><?= e($row['nama']) ?></td>
              <td><?= e($row['pekerjaan_ortu']) ?></td>
              <td>
                <?php if (!empty($row['status_kelayakan'])): ?>
                <span class="badge <?= $row['status_kelayakan'] === 'Layak' ? 'badge-layak' : 'badge-tidak' ?>">
                  <?= e($row['status_kelayakan']) ?>
                </span>
                <?php else: ?>
                <span class="badge badge-belum">Belum Diklasifikasikan</span>
                <?php endif; ?>
              </td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
