<?php
$title = "Laporan & Grafik";
require_once 'includes/header.php';

// Data for Chart 1: Distribution by Result
$dist_stmt = $pdo->query("
    SELECT d.nama, COUNT(r.id) as jumlah
    FROM diagnosa d
    LEFT JOIN riwayat_diagnosa r ON d.id = r.id_diagnosa
    GROUP BY d.id
");
$dist_data = $dist_stmt->fetchAll();
$dist_labels = array();
$dist_counts = array();
foreach ($dist_data as $row) {
    $dist_labels[] = $row['nama'];
    $dist_counts[] = $row['jumlah'];
}

// Data for Chart 2: History by Year
$year_stmt = $pdo->query("
    SELECT YEAR(tanggal) as tahun, COUNT(*) as jumlah 
    FROM riwayat_diagnosa 
    GROUP BY tahun 
    ORDER BY tahun DESC
");
$year_data = $year_stmt->fetchAll();
$year_labels = array();
$year_counts = array();
foreach ($year_data as $row) {
    $year_labels[] = $row['tahun'];
    $year_counts[] = $row['jumlah'];
}
?>

<!-- Kop Surat / Print Header -->
<div class="mb-10 p-8 bg-white rounded-2xl border-b-4 border-primary/20 flex items-center justify-between">
    <div class="flex items-center gap-6">
        <img src="<?= baseUrl('logo.png') ?>" alt="Logo SMAN 1 Barumun" class="w-24 h-24 object-contain">
        <div class="border-l-2 border-slate-100 pl-6">
            <h1 class="text-3xl font-black text-slate-800 tracking-tight uppercase">SMA Negeri 1 Barumun</h1>
            <p class="text-sm font-bold text-primary uppercase tracking-widest mb-2">Kabupaten Padang Lawas, Sumatera Utara</p>
            <div class="flex items-center gap-4 text-xs text-slate-500 font-medium">
                <span class="flex items-center gap-1">
                    <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
                    Jl. Ki Hajar Dewantara No. 1, Sibuhuan
                </span>
                <span class="flex items-center gap-1">
                    <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"></path></svg>
                    (0636) 123456
                </span>
            </div>
        </div>
    </div>
    <div class="text-right hidden md:block">
        <p class="text-[10px] font-black text-slate-400 uppercase tracking-[0.2em] mb-1">Status Laporan:</p>
        <span class="px-4 py-1.5 bg-green-50 text-green-600 rounded-full text-[10px] font-black uppercase border border-green-100 italic">Dokumen Terverifikasi</span>
    </div>
</div>

<div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-12">
    <!-- Chart 1 -->
    <div class="card p-8">
        <h3 class="text-lg font-bold text-slate-800 mb-8 text-center">Hasil Diagnosa</h3>
        <div class="h-80">
            <canvas id="resultChart"></canvas>
        </div>
    </div>
    
    <!-- Chart 2 -->
    <div class="card p-8">
        <h3 class="text-lg font-bold text-slate-800 mb-8 text-center">Diagnosa per Tahun</h3>
        <div class="h-80">
            <canvas id="yearChart"></canvas>
        </div>
    </div>
</div>

<div class="card">
    <div class="px-8 py-5 border-b border-slate-200">
        <h3 class="text-lg font-bold text-slate-800">Tabel Rincian Laporan</h3>
    </div>
    <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">Kategori Diagnosa</th>
                    <th class="px-8 py-4 text-xs font-bold text-slate-500 uppercase tracking-widest text-center">Total Siswa</th>
                    <th class="px-8 py-4 text-xs font-bold text-slate-500 uppercase tracking-widest text-right">Persentase</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                <?php 
                $total_all = array_sum($dist_counts);
                if ($total_all == 0) $total_all = 1; // Prevent div by zero
                foreach ($dist_data as $row): 
                    $percent = ($row['jumlah'] / $total_all) * 100;
                ?>
                    <tr class="hover:bg-slate-50 transition-colors">
                        <td class="px-8 py-4 text-sm font-bold text-slate-800"><?= $row['nama'] ?></td>
                        <td class="px-8 py-4 text-sm text-center font-mono"><?= $row['jumlah'] ?></td>
                        <td class="px-8 py-4 text-right">
                            <span class="text-sm font-bold text-primary"><?= round($percent, 1) ?>%</span>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="mt-8 flex justify-end">
    <button onclick="window.print()" class="btn-primary flex items-center gap-2">
        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2-2H7a2 2 0 00-2 2v4h12z"></path></svg>
        Cetak Laporan
    </button>
</div>

<script>
// Chart 1: Pie
const ctx1 = document.getElementById('resultChart').getContext('2d');
new Chart(ctx1, {
    type: 'doughnut',
    data: {
        labels: <?= json_encode($dist_labels) ?>,
        datasets: [{
            data: <?= json_encode($dist_counts) ?>,
            backgroundColor: [
                '#5b18c8', '#fada22', '#3b82f6', '#10b981', '#f97316', '#ef4444'
            ],
            borderWidth: 0
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: { position: 'bottom', labels: { padding: 20, font: { weight: 'bold' } } }
        },
        cutout: '70%'
    }
});

// Chart 2: Bar
const ctx2 = document.getElementById('yearChart').getContext('2d');
new Chart(ctx2, {
    type: 'bar',
    data: {
        labels: <?= json_encode($year_labels) ?>,
        datasets: [{
            label: 'Total Diagnosa',
            data: <?= json_encode($year_counts) ?>,
            backgroundColor: '#5b18c8',
            borderRadius: 8
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: { display: false }
        },
        scales: {
            y: { beginAtZero: true, grid: { borderDash: [2, 4] } },
            x: { grid: { display: false } }
        }
    }
});
</script>

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