<?php
$title = "Riwayat Diagnosa";
require_once 'includes/header.php';

// Pagination Settings
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
$offset = ($page - 1) * $limit;

// Count Total
$totalRows = $pdo->query("SELECT COUNT(*) FROM riwayat_diagnosa")->fetchColumn();
$totalPages = ceil($totalRows / $limit);

$riwayat_query = $pdo->prepare("
    SELECT r.*, s.nama as siswa_nama, d.nama as diagnosa_nama, k.nama as kelas_nama
    FROM riwayat_diagnosa r
    JOIN siswa s ON r.nis = s.nis
    JOIN diagnosa d ON r.id_diagnosa = d.id
    LEFT JOIN kelas k ON s.id_kelas = k.id
    ORDER BY r.tanggal DESC
    LIMIT :limit OFFSET :offset
");
$riwayat_query->bindValue(':limit', $limit, PDO::PARAM_INT);
$riwayat_query->bindValue(':offset', $offset, PDO::PARAM_INT);
$riwayat_query->execute();
$riwayat = $riwayat_query->fetchAll();
?>

<div class="flex justify-between items-center mb-8">
    <div>
        <h3 class="text-xl font-bold text-slate-800">Riwayat Diagnosa</h3>
        <p class="text-slate-500 text-sm">Daftar seluruh sesi diagnosa yang dilakukan oleh siswa.</p>
    </div>
</div>

<div class="card">
    <div class="overflow-x-auto">
        <table class="w-full text-left">
            <thead class="bg-slate-50 border-b border-slate-200">
                <tr>
                    <th class="px-8 py-4 text-xs font-bold text-slate-500 uppercase tracking-widest">Waktu</th>
                    <th class="px-8 py-4 text-xs font-bold text-slate-500 uppercase tracking-widest">Siswa</th>
                    <th class="px-8 py-4 text-xs font-bold text-slate-500 uppercase tracking-widest">Kelas</th>
                    <th class="px-8 py-4 text-xs font-bold text-slate-500 uppercase tracking-widest">Hasil Diagnosa</th>
                    <th class="px-8 py-4 text-xs font-bold text-slate-500 uppercase tracking-widest">Certainty</th>
                    <th class="px-8 py-4 text-xs font-bold text-slate-500 uppercase tracking-widest text-right">Detail</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                <?php if (empty($riwayat)): ?>
                    <tr>
                        <td colspan="6" class="px-8 py-10 text-center text-slate-400 italic">Belum ada data riwayat.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($riwayat as $r): ?>
                        <tr class="hover:bg-slate-50 transition-colors text-sm">
                            <td class="px-8 py-4 text-slate-500 font-mono">
                                <?= date('d/m/Y', strtotime($r['tanggal'])) ?><br>
                                <span class="text-[10px] text-slate-400"><?= date('H:i', strtotime($r['tanggal'])) ?></span>
                            </td>
                            <td class="px-8 py-4 font-bold text-slate-800"><?= $r['siswa_nama'] ?></td>
                            <td class="px-8 py-4 text-slate-600"><?= isset($r['kelas_nama']) ? $r['kelas_nama'] : '-' ?></td>
                            <td class="px-8 py-4 font-bold text-primary"><?= $r['diagnosa_nama'] ?></td>
                            <td class="px-8 py-4">
                                <div class="flex items-center gap-2">
                                    <div class="flex-1 bg-slate-100 h-2 rounded-full overflow-hidden max-w-[60px]">
                                        <div class="bg-primary h-full" style="width: <?= $r['total_cf'] * 100 ?>%"></div>
                                    </div>
                                    <div class="flex flex-col">
                                        <span class="font-bold text-slate-800"><?= number_format($r['total_cf'] * 100, 2) ?>%</span>
                                        <span class="text-[10px] font-bold text-slate-400 uppercase"><?= getKategoriCF($pdo, $r['id_diagnosa'], $r['total_cf']) ?></span>
                                    </div>
                                </div>
                            </td>
                            <td class="px-8 py-4 text-right">
                                <a href="detail-riwayat.php?id=<?= $r['id'] ?>" class="text-primary hover:text-primary-dark font-bold">Lihat</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?= renderPagination($page, $totalPages, 'riwayat.php?') ?>

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