@extends('admin.layout')

@section('content')
<div class="mb-4">
    <h4 class="fw-bold text-dark">Overview Statistik Aplikasi</h4>
    <p class="text-muted small">Selamat datang di panel kendali sistem rekomendasi jurusan SMAN 4 Jember.</p>
</div>

<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-info">
            <p>Total Siswa</p>
            <h3 id="stat-total-siswa">0</h3>
        </div>
        <div class="stat-icon icon-siswa">
            <i class="fas fa-user-graduate"></i>
        </div>
    </div>

    <div class="stat-card">
        <div class="stat-info">
            <p>Rekomendasi Diproses</p>
            <h3 id="stat-total-rekomendasi">0</h3>
        </div>
        <div class="stat-icon icon-rekom">
            <i class="fas fa-check-circle"></i>
        </div>
    </div>

    <div class="stat-card">
        <div class="stat-info">
            <p>Rata-rata Akurasi</p>
            <h3 id="stat-rata-akurasi">0%</h3>
        </div>
        <div class="stat-icon icon-akurasi">
            <i class="fas fa-chart-line"></i>
        </div>
    </div>
</div>

<div class="dashboard-section mt-4">
    <div class="chart-container">
        <h5 class="fw-bold mb-4 d-flex align-items-center gap-2">
            <div style="width: 4px; height: 20px; background: var(--primary-green); border-radius: 10px;"></div>
            Distribusi Rekomendasi Jurusan SMAN 4 Jember
        </h5>
        
        <div id="chart-wrapper" style="height: 350px; display: flex; align-items: center; justify-content: center; background: #f8fafc; border-radius: 15px; border: 2px dashed #e2e8f0; padding: 20px;">
            <canvas id="distribusiChart" class="d-none w-100 h-100"></canvas>
            <div id="chart-placeholder" class="text-center">
                <i class="fas fa-chart-pie fa-3x text-light mb-3"></i>
                <p class="text-muted">Memuat grafik distribusi...</p>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', async function() {
        const token = localStorage.getItem('access_token');
        if (!token) {
            window.location.href = '/login';
            return;
        }

        try {
            const response = await fetch('/api/admin/dashboard-stats', {
                headers: {
                    'Authorization': `Bearer ${token}`,
                    'Accept': 'application/json'
                }
            });

            const result = await response.json();
            
            if (result.success) {
                const data = result.data;

                document.getElementById('stat-total-siswa').innerText = data.total_siswa;
                document.getElementById('stat-total-rekomendasi').innerText = data.rekomendasi_diproses;
                document.getElementById('stat-rata-akurasi').innerText = data.rata_rata_akurasi + '%';

                const distribusi = data.distribusi_jurusan;
                const placeholder = document.getElementById('chart-placeholder');
                const canvas = document.getElementById('distribusiChart');
                const wrapper = document.getElementById('chart-wrapper');

                if (distribusi && distribusi.length > 0) {
                    placeholder.classList.add('d-none');
                    canvas.classList.remove('d-none');
                    wrapper.style.border = 'none';
                    wrapper.style.background = 'transparent';

                    const labels = distribusi.map(item => item.jurusan);
                    const counts = distribusi.map(item => item.total);

                    const ctx = canvas.getContext('2d');
                    new Chart(ctx, {
                        type: 'bar',
                        data: {
                            labels: labels,
                            datasets: [{
                                label: 'Jumlah Rekomendasi',
                                data: counts,
                                backgroundColor: 'rgba(45, 122, 71, 0.8)',
                                borderColor: 'rgba(45, 122, 71, 1)',
                                borderWidth: 1,
                                borderRadius: 5
                            }]
                        },
                        options: {
                            responsive: true,
                            maintainAspectRatio: false,
                            scales: {
                                y: {
                                    beginAtZero: true,
                                    ticks: { stepSize: 1 }
                                }
                            },
                            plugins: {
                                legend: { display: false }
                            }
                        }
                    });
                } else {
                    placeholder.querySelector('p').innerText = 'Belum ada data riwayat rekomendasi.';
                }
            }
        } catch (error) {
            console.error('Gagal memuat data dashboard:', error);
            document.getElementById('chart-placeholder').querySelector('p').innerText = 'Gagal memuat data.';
        }
    });
</script>
@endpush
@endsection