<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hasil Rekomendasi - ASGARD</title>

    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">

    <style>
        body {
            font-family: 'Plus Jakarta Sans', sans-serif;
            background:
                radial-gradient(circle at 8% 12%, rgba(16, 185, 129, 0.10), transparent 32%),
                radial-gradient(circle at 92% 8%, rgba(59, 130, 246, 0.08), transparent 30%),
                linear-gradient(180deg, #f8fafc 0%, #f3f7fb 50%, #f8fafc 100%);
        }

        nav {
            box-shadow: 0 10px 35px rgba(15, 23, 42, 0.05);
        }

        nav a {
            position: relative;
        }

        nav a::after {
            content: "";
            position: absolute;
            left: 0;
            bottom: -10px;
            width: 0;
            height: 2px;
            border-radius: 999px;
            background: #10b981;
            transition: width 0.25s ease;
        }

        nav a:hover::after,
        nav a.text-emerald-600::after {
            width: 100%;
        }

        .card-shadow {
            box-shadow: 0 18px 50px rgba(15, 23, 42, 0.08);
        }

        .card-hover {
            transition: transform 0.25s ease, box-shadow 0.25s ease, border-color 0.25s ease, background-color 0.25s ease;
        }

        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 24px 60px rgba(15, 23, 42, 0.11);
            border-color: rgba(16, 185, 129, 0.22);
        }

        main > div > .text-center {
            background: rgba(255, 255, 255, 0.78);
            border: 1px solid rgba(226, 232, 240, 0.85);
            border-radius: 32px;
            padding: 34px 24px;
            box-shadow: 0 18px 55px rgba(15, 23, 42, 0.055);
            backdrop-filter: blur(16px);
        }

        main section.bg-gradient-to-br {
            box-shadow: 0 28px 75px rgba(16, 185, 129, 0.23);
        }

        main section.bg-white,
        main div.bg-white {
            background: rgba(255, 255, 255, 0.94);
            backdrop-filter: blur(14px);
        }

        main .bg-slate-50 {
            transition: transform 0.22s ease, box-shadow 0.22s ease, border-color 0.22s ease;
        }

        main .bg-slate-50:hover {
            transform: translateY(-2px);
            box-shadow: 0 16px 35px rgba(15, 23, 42, 0.06);
            border-color: rgba(16, 185, 129, 0.18);
        }

        footer {
            box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
        }

        @media (max-width: 768px) {
            main > div > .text-center {
                border-radius: 26px;
                padding: 26px 18px;
            }
        }
    </style>
</head>

<body class="flex flex-col min-h-screen text-slate-800">

    <nav class="sticky top-0 z-50 bg-white/95 backdrop-blur-md border-b border-slate-100 px-6 py-4">
        <div class="max-w-7xl mx-auto flex items-center justify-between gap-4">
            <div class="flex items-center gap-4">
                <div class="w-10 h-10 bg-emerald-100 rounded-xl flex items-center justify-center text-emerald-600 font-bold">
                    AS
                </div>

                <span class="font-black text-xl tracking-tighter text-emerald-950 uppercase italic">
                    ASGARD.
                </span>
            </div>

            <div class="hidden md:flex items-center gap-8 text-[11px] font-bold uppercase tracking-widest text-slate-400">
                <a href="{{ route('student.profile') }}" class="hover:text-emerald-600 transition">
                    Beranda
                </a>

                <a href="{{ route('student.prediction') }}" class="hover:text-emerald-600 transition">
                    Cek Rekomendasi
                </a>

                <a href="#" onclick="goToLastResult(event)" class="text-emerald-600">
                    Hasil Rekomendasi
                </a>
            </div>

            <div class="flex items-center gap-3">
                <div class="hidden sm:flex items-center gap-3 bg-emerald-50 px-4 py-2 rounded-full border border-emerald-100">
                    <div class="w-2 h-2 bg-emerald-500 rounded-full animate-pulse"></div>

                    <span class="text-[10px] font-bold text-emerald-700 uppercase">
                        Siswa
                    </span>
                </div>

                <button onclick="handleLogout()" class="bg-red-50 hover:bg-red-100 text-red-600 border border-red-100 px-4 py-2 rounded-full transition-all">
                    <span class="text-[10px] font-bold uppercase tracking-widest">
                        Logout
                    </span>
                </button>
            </div>
        </div>
    </nav>

    <main class="flex-grow px-6 py-10">
        <div class="max-w-5xl mx-auto">

            <div class="text-center mb-8">
                <div class="inline-flex items-center gap-2 bg-emerald-100 text-emerald-700 px-4 py-2 rounded-full text-xs font-bold">
                    Hasil rekomendasi berhasil diproses ✅
                </div>

                <h1 class="mt-5 text-4xl md:text-5xl font-extrabold tracking-tight text-slate-900">
                    Hasil Rekomendasi Jurusan
                </h1>

                <p class="text-slate-500 mt-3 max-w-2xl mx-auto leading-relaxed">
                    Hasil ini tersimpan sebagai rekomendasi terakhir. Setelah kembali ke dashboard, menu <b>Hasil Rekomendasi</b> tetap bisa digunakan untuk membuka halaman ini.
                </p>
            </div>

            <section class="bg-gradient-to-br from-emerald-600 via-emerald-500 to-teal-600 rounded-[2.5rem] p-8 md:p-14 text-white card-shadow mb-8 relative overflow-hidden">
                <div class="absolute inset-0 opacity-10">
                    <div class="absolute -top-10 -left-10 w-40 h-40 bg-white rounded-full blur-3xl"></div>
                    <div class="absolute -bottom-10 -right-10 w-56 h-56 bg-white rounded-full blur-3xl"></div>
                </div>

                <div class="relative z-10 text-center">
                    <p class="text-[11px] md:text-sm font-bold uppercase tracking-[0.35em] text-emerald-100 mb-5">
                        Rekomendasi Jurusan
                    </p>

                    <h2 class="text-3xl md:text-6xl font-black uppercase leading-tight tracking-tight mb-8">
                        {{ $riwayat->hasil_rekomendasi_jurusan }}
                    </h2>

                    <p class="max-w-3xl mx-auto text-emerald-50 leading-relaxed mb-8">
                        Jurusan ini diperoleh dari pengolahan data nilai rapor, kecenderungan minat, dan kondisi ekonomi yang telah dimasukkan.
                    </p>

                    <div class="flex flex-col sm:flex-row gap-3 justify-center">
                        <a href="{{ route('student.profile') }}"
                           onclick="saveLastRecommendationResult()"
                           class="inline-flex items-center justify-center bg-white hover:bg-slate-100 text-emerald-700 font-bold py-4 px-8 rounded-2xl transition">
                            Kembali ke Dashboard
                        </a>

                        <a href="{{ route('student.prediction') }}"
                           onclick="saveLastRecommendationResult()"
                           class="inline-flex items-center justify-center bg-emerald-800/50 hover:bg-emerald-800 text-white font-bold py-4 px-8 rounded-2xl transition border border-white/10">
                            Cek Rekomendasi Baru
                        </a>
                    </div>
                </div>
            </section>

            <section class="bg-white rounded-[2rem] border border-slate-100 card-shadow p-6 md:p-8 mb-8">
                <div class="flex items-center gap-3 mb-6">
                    <div class="w-10 h-10 rounded-2xl bg-violet-100 flex items-center justify-center text-xl">
                        👤
                    </div>

                    <h3 class="text-2xl font-extrabold text-slate-800">
                        Rekapan Data yang Digunakan
                    </h3>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    <div class="bg-slate-50 border border-slate-100 rounded-3xl p-5 card-hover">
                        <p class="text-[11px] font-bold uppercase tracking-widest text-slate-400 mb-3">
                            Kondisi Ekonomi
                        </p>

                        <div class="flex items-center gap-4">
                            <div class="w-14 h-14 rounded-2xl bg-amber-100 flex items-center justify-center text-2xl">
                                💰
                            </div>

                            <div>
                                <p class="font-extrabold text-slate-800 leading-snug">
                                    {{ $labelEkonomi }}
                                </p>
                            </div>
                        </div>
                    </div>

                    <div class="bg-slate-50 border border-slate-100 rounded-3xl p-5 card-hover">
                        <p class="text-[11px] font-bold uppercase tracking-widest text-slate-400 mb-3">
                            Minat Utama
                        </p>

                        <div class="flex items-center gap-4">
                            <div class="w-14 h-14 rounded-2xl bg-pink-100 flex items-center justify-center text-2xl">
                                🎯
                            </div>

                            <div>
                                <p class="font-extrabold text-slate-800 leading-snug">
                                    {{ $minatUtama }}
                                </p>
                            </div>
                        </div>
                    </div>

                    <div class="bg-slate-50 border border-slate-100 rounded-3xl p-5 card-hover">
                        <p class="text-[11px] font-bold uppercase tracking-widest text-slate-400 mb-3">
                            Data Akademik
                        </p>

                        <div class="flex items-center gap-4">
                            <div class="w-14 h-14 rounded-2xl bg-cyan-100 flex items-center justify-center text-2xl">
                                📚
                            </div>

                            <div>
                                <p class="font-extrabold text-slate-800 leading-snug">
                                    15 Mata Pelajaran
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <section class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
                <div class="bg-white rounded-[2rem] border border-slate-100 card-shadow p-6 md:p-8">
                    <h3 class="text-xl font-extrabold text-slate-800 mb-4">
                        Ringkasan Analisis
                    </h3>

                    <p class="text-slate-600 leading-relaxed">
                        Sistem membaca hubungan antara nilai rapor, minat utama, dan kondisi ekonomi yang diinputkan. Berdasarkan hasil pengolahan tersebut, jurusan
                        <span class="font-bold text-slate-800">{{ $riwayat->hasil_rekomendasi_jurusan }}</span>
                        menjadi pilihan yang paling sesuai untuk dipertimbangkan pada tahap ini.
                    </p>
                </div>

                <div class="bg-white rounded-[2rem] border border-slate-100 card-shadow p-6 md:p-8">
                    <h3 class="text-xl font-extrabold text-slate-800 mb-4">
                        Peluang Karier
                    </h3>

                    <div class="text-slate-600 leading-relaxed">
                        {!! $prospekKerja !!}
                    </div>
                </div>
            </section>

            <section class="bg-white rounded-[2rem] border border-slate-100 card-shadow p-6 md:p-8">
                <h3 class="text-xl font-extrabold text-slate-800 mb-5">
                    Langkah Setelah Mendapatkan Hasil
                </h3>

                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    <div class="bg-slate-50 border border-slate-100 rounded-3xl p-5">
                        <h4 class="font-extrabold text-slate-800 mb-2">
                            1. Cek Kembali Data
                        </h4>

                        <p class="text-sm text-slate-500 leading-relaxed">
                            Pastikan nilai dan jawaban minat yang dimasukkan sudah sesuai dengan kondisi sebenarnya.
                        </p>
                    </div>

                    <div class="bg-slate-50 border border-slate-100 rounded-3xl p-5">
                        <h4 class="font-extrabold text-slate-800 mb-2">
                            2. Cari Informasi Jurusan
                        </h4>

                        <p class="text-sm text-slate-500 leading-relaxed">
                            Pelajari mata kuliah, biaya, peluang kerja, dan kampus yang menyediakan jurusan tersebut.
                        </p>
                    </div>

                    <div class="bg-slate-50 border border-slate-100 rounded-3xl p-5">
                        <h4 class="font-extrabold text-slate-800 mb-2">
                            3. Konsultasi Guru BK
                        </h4>

                        <p class="text-sm text-slate-500 leading-relaxed">
                            Gunakan hasil ini sebagai bahan diskusi dengan Guru BK sebelum menentukan pilihan akhir.
                        </p>
                    </div>
                </div>
            </section>
        </div>
    </main>

    <footer class="bg-[#062C22] text-white py-16 px-6 mt-16">
        <div class="max-w-7xl mx-auto grid grid-cols-1 md:grid-cols-3 gap-12">
            <div class="space-y-6">
                <span class="font-black text-2xl tracking-tighter uppercase italic">
                    ASGARD.
                </span>

                <p class="text-slate-300 text-sm leading-relaxed max-w-xs">
                    Sistem rekomendasi jurusan kuliah SMAN 4 Jember berbasis profil siswa dan data akademik.
                </p>
            </div>

            <div>
                <h5 class="font-bold text-xs uppercase tracking-widest text-emerald-400 mb-6">
                    Informasi
                </h5>

                <ul class="space-y-4 text-sm text-slate-300">
                    <li>
                        <a href="{{ route('panduan') }}" class="hover:text-white transition">
                            Panduan Sistem
                        </a>
                    </li>
                </ul>
            </div>

            <div>
                <h5 class="font-bold text-xs uppercase tracking-widest text-emerald-400 mb-6">
                    Sekolah
                </h5>

                <ul class="space-y-4 text-sm text-slate-300">
                    <li>SMAN 4 Jember</li>
                    <li>Jl. Wolter Monginsidi No.24</li>
                    <li>Jember, Jawa Timur</li>
                </ul>
            </div>
        </div>

        <div class="max-w-7xl mx-auto mt-16 pt-8 border-t border-white/10 text-center">
            <p class="text-[10px] font-medium uppercase tracking-[0.35em] text-slate-400">
                © 2026 GALANG SEFIAN ADJI - POLITEKNIK NEGERI JEMBER
            </p>
        </div>
    </footer>

    <script>
        function getAppBaseUrl() {
            const origin = window.location.origin;
            const segments = window.location.pathname.split('/').filter(Boolean);
            const studentIndex = segments.indexOf('student');

            if (studentIndex > 0) {
                return origin + '/' + segments.slice(0, studentIndex).join('/');
            }

            return origin;
        }

        const APP_BASE_URL = getAppBaseUrl();

        const routeLogin = `${APP_BASE_URL}/login`;
        const routeDashboard = `${APP_BASE_URL}/student/profile`;
        const routePrediction = `${APP_BASE_URL}/student/prediction`;
        const routeAdminDashboard = `${APP_BASE_URL}/admin/dashboard`;
        const apiLogoutUrl = `${APP_BASE_URL}/api/logout`;
        const studentResultUrlTemplate = `${APP_BASE_URL}/student/result/__ID__`;

        function getAccessToken() {
            return localStorage.getItem('access_token') ||
                localStorage.getItem('token') ||
                sessionStorage.getItem('access_token') ||
                sessionStorage.getItem('token') ||
                '';
        }

        function getUserRole() {
            return localStorage.getItem('user_role') ||
                localStorage.getItem('role') ||
                sessionStorage.getItem('user_role') ||
                sessionStorage.getItem('role') ||
                '';
        }

        function clearLoginStorage() {
            localStorage.removeItem('access_token');
            localStorage.removeItem('token');
            localStorage.removeItem('user_role');
            localStorage.removeItem('role');
            localStorage.removeItem('user_name');
            localStorage.removeItem('name');

            sessionStorage.removeItem('access_token');
            sessionStorage.removeItem('token');
            sessionStorage.removeItem('user_role');
            sessionStorage.removeItem('role');
        }

        function redirectByRole(role) {
            if (role === 'admin') {
                window.location.href = routeAdminDashboard;
                return;
            }

            if (role === 'siswa' || role === 'student') {
                window.location.href = routeDashboard;
                return;
            }

            window.location.href = routeLogin;
        }

        function makeResultUrl(id) {
            return id ? studentResultUrlTemplate.replace('__ID__', encodeURIComponent(id)) : '';
        }

        function normalizeAppUrl(url) {
            if (!url) return '';

            const text = String(url);

            if (text.startsWith('http://') || text.startsWith('https://')) {
                return text;
            }

            if (text.startsWith('/')) {
                return APP_BASE_URL + text;
            }

            return `${APP_BASE_URL}/${text}`;
        }

        function getStoredLastResult() {
            try {
                const saved = localStorage.getItem('asgard_last_result');
                return saved ? JSON.parse(saved) : null;
            } catch (error) {
                return null;
            }
        }

        function goToLastResult(event) {
            event.preventDefault();

            saveLastRecommendationResult();

            const saved = getStoredLastResult();

            if (saved && saved.url) {
                window.location.href = normalizeAppUrl(saved.url);
                return;
            }

            if (saved && saved.id) {
                window.location.href = makeResultUrl(saved.id);
                return;
            }

            alert('Belum ada hasil rekomendasi yang bisa dibuka. Silakan lakukan cek rekomendasi terlebih dahulu.');
            window.location.href = routePrediction;
        }

        function saveLastRecommendationResult() {
            const resultId = @json($riwayat->id ?? '');
            const resultUrl = resultId ? makeResultUrl(resultId) : window.location.href;

            const resultData = {
                id: resultId,
                jurusan: @json($riwayat->hasil_rekomendasi_jurusan ?? ''),
                label_ekonomi: @json($labelEkonomi ?? ''),
                minat_utama: @json($minatUtama ?? ''),
                prospek_kerja: @json($prospekKerja ?? ''),
                url: resultUrl,
                tanggal: new Date().toLocaleString('id-ID', {
                    day: '2-digit',
                    month: 'long',
                    year: 'numeric',
                    hour: '2-digit',
                    minute: '2-digit'
                })
            };

            localStorage.setItem('asgard_last_result', JSON.stringify(resultData));
        }

        async function handleLogout() {
            const token = getAccessToken();

            if (token) {
                try {
                    await fetch(apiLogoutUrl, {
                        method: 'POST',
                        headers: {
                            'Authorization': `Bearer ${token}`,
                            'Accept': 'application/json'
                        }
                    });
                } catch (error) {
                    console.error('Logout error:', error);
                }
            }

            clearLoginStorage();
            localStorage.removeItem('asgard_last_result');
            window.location.href = routeLogin;
        }

        window.addEventListener('pageshow', function () {
            saveLastRecommendationResult();
        });

        document.addEventListener('DOMContentLoaded', function () {
            const token = getAccessToken();
            const role = getUserRole();

            if (!token) {
                window.location.href = routeLogin;
                return;
            }

            if (role !== 'siswa' && role !== 'student') {
                redirectByRole(role);
                return;
            }

            saveLastRecommendationResult();
        });
    </script>
</body>
</html>