<?php
require_once 'includes/header-siswa.php';
checkSiswa();

$nis = $_SESSION['siswa_auth']['nis'];

// Get latest diagnosis only (max 1 as requested)
$stmt = $pdo->prepare("
    SELECT r.*, d.nama as diagnosa_nama 
    FROM riwayat_diagnosa r
    JOIN diagnosa d ON r.id_diagnosa = d.id
    WHERE r.nis = ?
    ORDER BY r.tanggal DESC LIMIT 1
");
$stmt->execute(array($nis));
$latest = $stmt->fetch();
?>

<div class="container mx-auto px-6 py-12">
    <div class="max-w-2xl mx-auto">
        <div class="text-center mb-12">
            <h2 class="text-3xl font-extrabold text-slate-800 mb-2">Riwayat Diagnosa Anda</h2>
            <p class="text-slate-500">Berikut adalah hasil diagnosa kesehatan mental terbaru Anda.</p>
        </div>

        <?php if (!$latest): ?>
            <div class="card p-20 text-center">
                <div class="w-20 h-20 bg-slate-100 rounded-full flex items-center justify-center mx-auto mb-6 text-slate-300">
                    <svg class="w-10 h-10" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg>
                </div>
                <h3 class="text-xl font-bold text-slate-800 mb-2">Belum ada riwayat</h3>
                <p class="text-slate-500 mb-8">Anda belum pernah melakukan diagnosa kesehatan mental.</p>
                <a href="diagnosa.php" class="btn-primary py-3 px-10">Mulai Diagnosa</a>
            </div>
        <?php else: ?>
            <div class="card p-8 border-l-8 border-primary">
                <div class="flex justify-between items-start mb-6">
                    <div>
                        <p class="text-xs font-bold text-slate-400 uppercase tracking-widest mb-1"><?= date('d F Y • H:i', strtotime($latest['tanggal'])) ?></p>
                        <h3 class="text-2xl font-bold text-slate-800"><?= $latest['diagnosa_nama'] ?></h3>
                    </div>
                    <div class="text-right">
                        <span class="text-3xl font-black text-primary"><?= number_format($latest['total_cf'] * 100, 2) ?>%</span>
                        <p class="text-[10px] font-bold text-slate-400 uppercase tracking-widest">Certainty</p>
                    </div>
                </div>

                <div class="bg-slate-50 rounded-xl p-6 mb-8 text-sm text-slate-600 leading-relaxed italic">
                    Interpretasi: "<?= getKategoriCF($pdo, $latest['id_diagnosa'], $latest['total_cf']) ?>"
                </div>

                <div class="flex gap-4">
                    <a href="diagnosa.php" class="flex-1 btn-primary py-3 text-sm">Diagnosa Ulang</a>
                    <a href="index.php" class="flex-1 py-3 text-sm text-center font-bold text-slate-500 hover:bg-slate-100 rounded-xl transition-all">Kembali</a>
                </div>
            </div>
            
            <p class="text-center text-slate-400 text-xs mt-10">
                Catatan: Sistem hanya menampilkan riwayat diagnosa terakhir untuk menjaga fokus pemulihan Anda.
            </p>
        <?php endif; ?>
    </div>
</div>

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