<?php
require_once 'includes/header.php';
checkAdmin();

if (!isset($_GET['id'])) {
    redirect('riwayat.php');
}

$id = $_GET['id'];

// Get Riwayat Data
$stmt = $pdo->prepare("
    SELECT r.*, s.nama as siswa_nama, s.nis, s.kelamin, k.nama as kelas_nama, d.nama as diagnosa_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
    WHERE r.id = ?
");
$stmt->execute(array($id));
$riwayat = $stmt->fetch();

if (!$riwayat) {
    redirect('riwayat.php');
}

// Get Symptoms for this session
$stmt_gejala = $pdo->prepare("
    SELECT g.kode, g.nama, j.cf_user
    FROM jawaban_user j
    JOIN gejala g ON j.id_gejala = g.id
    WHERE j.id_riwayat = ?
    ORDER BY g.kode ASC
");
$stmt_gejala->execute(array($id));
$jawaban = $stmt_gejala->fetchAll();
?>

<div class="mb-8">
    <a href="riwayat.php" class="inline-flex items-center gap-2 text-sm font-bold text-slate-400 hover:text-primary transition-all mb-4">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path></svg>
        Kembali ke Riwayat
    </a>
    <h3 class="text-xl font-bold text-slate-800">Detail Laporan Diagnosa</h3>
    <p class="text-slate-500 text-sm">Informasi lengkap hasil diagnosa mandiri siswa.</p>
</div>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
    <!-- Student Header Card -->
    <div class="card p-8 h-fit">
        <div class="flex items-center gap-4 mb-8">
            <div class="w-16 h-16 bg-primary/10 text-primary rounded-full flex items-center justify-center text-xl font-black">
                <?= substr($riwayat['siswa_nama'], 0, 1) ?>
            </div>
            <div>
                <h4 class="text-lg font-bold text-slate-800"><?= $riwayat['siswa_nama'] ?></h4>
                <p class="text-xs text-slate-400 font-bold uppercase tracking-widest"><?= $riwayat['nis'] ?> • <?= $riwayat['kelas_nama'] ?></p>
            </div>
        </div>
        
        <div class="space-y-4">
            <div class="flex justify-between text-sm py-3 border-b border-slate-50">
                <span class="text-slate-400">Jenis Kelamin</span>
                <span class="font-bold text-slate-700"><?= $riwayat['kelamin'] === 'L' ? 'Laki-laki' : 'Perempuan' ?></span>
            </div>
            <div class="flex justify-between text-sm py-3 border-b border-slate-50">
                <span class="text-slate-400">Tanggal Diagnosa</span>
                <span class="font-bold text-slate-700"><?= date('d F Y', strtotime($riwayat['tanggal'])) ?></span>
            </div>
            <div class="flex justify-between text-sm py-3 border-b border-slate-50">
                <span class="text-slate-400">Jam</span>
                <span class="font-bold text-slate-700"><?= date('H:i', strtotime($riwayat['tanggal'])) ?></span>
            </div>
        </div>
    </div>

    <!-- Diagnosis Result Card -->
    <div class="lg:col-span-2 space-y-8">
        <div class="card p-10 border-none bg-primary text-white shadow-xl shadow-primary/20">
            <div class="flex justify-between items-start mb-6">
                <div>
                    <p class="text-[10px] font-black uppercase tracking-[0.2em] opacity-60 mb-2">Hasil Akhir Diagnosa</p>
                    <h4 class="text-3xl font-black"><?= $riwayat['diagnosa_nama'] ?></h4>
                </div>
                <div class="text-right">
                    <p class="text-5xl font-black"><?= number_format($riwayat['total_cf'] * 100, 2) ?>%</p>
                    <p class="text-[10px] font-bold uppercase tracking-widest opacity-60">Certainty Score</p>
                </div>
            </div>
            
            <div class="bg-white/10 rounded-2xl p-6 border border-white/10 backdrop-blur-sm">
                <p class="text-sm font-bold opacity-60 uppercase tracking-widest mb-1">Kategori Interpretasi:</p>
                <p class="text-xl font-bold italic">"<?= getKategoriCF($pdo, $riwayat['id_diagnosa'], $riwayat['total_cf']) ?>"</p>
            </div>

            <?php 
                $stmt_solusi = $pdo->prepare("SELECT saran FROM solusi WHERE id_diagnosa = ? AND ? BETWEEN min_cf AND max_cf LIMIT 1");
                $stmt_solusi->execute(array($riwayat['id_diagnosa'], $riwayat['total_cf']));
                $saran = $stmt_solusi->fetchColumn();
            ?>
            <?php if ($saran): ?>
                <div class="mt-8 pt-8 border-t border-white/10">
                    <p class="text-[10px] font-black uppercase tracking-[0.2em] opacity-60 mb-3">Saran yang Diberikan:</p>
                    <p class="text-primary-light font-medium bg-white px-6 py-4 rounded-xl text-slate-700"><?= $saran ?></p>
                </div>
            <?php endif; ?>
        </div>

        <div class="card">
            <div class="bg-slate-50 px-8 py-5 border-b border-slate-200">
                <h4 class="text-sm font-bold text-slate-800 uppercase tracking-widest">Detail Gejala yang Dipilih</h4>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full text-left">
                    <thead class="bg-white border-b border-slate-100">
                        <tr>
                            <th class="px-8 py-4 text-[10px] font-bold text-slate-400 uppercase tracking-widest">Kode</th>
                            <th class="px-8 py-4 text-[10px] font-bold text-slate-400 uppercase tracking-widest">Nama Gejala</th>
                            <th class="px-8 py-4 text-[10px] font-bold text-slate-400 uppercase tracking-widest text-right">Skala Keyakinan</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100">
                        <?php foreach ($jawaban as $j): ?>
                            <tr class="text-sm">
                                <td class="px-8 py-4 font-bold text-primary"><?= $j['kode'] ?></td>
                                <td class="px-8 py-4 text-slate-700"><?= $j['nama'] ?></td>
                                <td class="px-8 py-4 text-right">
                                    <span class="font-mono font-bold bg-slate-100 px-3 py-1 rounded-lg"><?= number_format($j['cf_user'], 1) ?></span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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