<!DOCTYPE html>
<html lang="id">

<head>
    <meta charset="UTF-8">
    <title>E-Surat Desa - Dashboard</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="{{ asset('assets/css/sb-admin-2.min.css') }}" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>

<body id="page-top">

    <div id="wrapper">

        <ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">

            <a class="sidebar-brand d-flex align-items-center justify-content-center"
                href="{{ Auth::user()->role === 'admin' ? route('dashboard') : route('dashboarduser') }}">
                <div class="sidebar-brand-icon rotate-n-15">
                    <i class="fas fa-envelope-open-text"></i>
                </div>
                <div class="sidebar-brand-text mx-3">E-Surat Desa</div>
            </a>

            <hr class="sidebar-divider my-0">

            <li class="nav-item active">
                <a class="nav-link" href="{{ route('dashboarduser') }}">
                    <i class="fas fa-fw fa-tachometer-alt"></i>
                    <span>Dashboard</span>
                </a>
            </li>

            <hr class="sidebar-divider">

            <div class="sidebar-heading">Kependudukan</div>

            <li class="nav-item {{ Request::is('user/validasi*') ? 'active' : '' }}">
                <a class="nav-link" href="{{ route('user.validasi.index') }}">
                    <i class="fas fa-fw fa-id-card"></i>
                    <span>Validasi Penduduk</span>
                </a>
            </li>

            <hr class="sidebar-divider">

            <div class="sidebar-heading">Layanan</div>

            <li class="nav-item {{ Request::is('user/pengajuan*') ? 'active' : '' }}">
                <a class="nav-link" href="{{ route('user.pengajuan.index') }}">
                    <i class="fas fa-fw fa-file-alt"></i>
                    <span>Pengajuan Surat</span>
                </a>
            </li>

            <hr class="sidebar-divider d-none d-md-block">

            <li class="nav-item">
                <a href="{{ route('logout') }}" class="nav-link">
                    <i class="fas fa-fw fa-sign-out-alt"></i>
                    <span>Logout</span>
                </a>
            </li>

        </ul>

        <div id="content-wrapper" class="d-flex flex-column">

            <div id="content">

                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                    <ul class="navbar-nav ml-auto">
                        <li class="nav-item dropdown no-arrow mx-1">
                            <a class="nav-link dropdown-toggle" href="#" id="alertsDropdown" role="button"
                                data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="hilangkanBadge()">
                                <i class="fas fa-bell fa-fw"></i>

                                @if(isset($totalNotifUser) && $totalNotifUser > 0)
                                <span id="badge-notif" class="badge badge-danger badge-counter">{{ $totalNotifUser }}</span>
                                @endif
                            </a>

                            <div class="dropdown-list dropdown-menu dropdown-menu-right shadow animated--grow-in" aria-labelledby="alertsDropdown">
                                <h6 class="dropdown-header bg-success border-success text-white">
                                    🔔 Notifikasi Status Pengajuan
                                </h6>

                                @if(isset($notifikasiUser) && count($notifikasiUser) > 0)
                                @foreach($notifikasiUser as $notif)
                                @php
                                // Memastikan status dibaca sebagai huruf kecil untuk pengecekan warna
                                $statusLower = strtolower($notif->status);
                                $isSetuju = ($statusLower == 'disetujui');
                                @endphp

                                <a class="dropdown-item d-flex align-items-center border-bottom" href="{{ route($notif->rute) }}?highlight={{ $notif->id }}">
                                    <div class="mr-3">
                                        <div class="icon-circle {{ $isSetuju ? 'bg-success' : 'bg-danger' }}">
                                            <i class="fas fa-file-alt text-white"></i>
                                        </div>
                                    </div>
                                    <div>
                                        <div class="small text-gray-500">
                                            {{ \Carbon\Carbon::parse($notif->created_at)->diffForHumans() }}
                                        </div>
                                        <span class="font-weight-bold">Surat {{ $notif->tipe }}</span> Anda telah
                                        <span class="{{ $isSetuju ? 'text-success' : 'text-danger' }} font-weight-bold">
                                            {{ strtoupper($notif->status) }}
                                        </span>
                                    </div>
                                </a>
                                @endforeach
                                @else
                                <div class="dropdown-item text-center small text-gray-500 py-3">
                                    Tidak ada notifikasi baru
                                </div>
                                @endif

                                <a class="dropdown-item text-center small text-gray-500 bg-light" href="{{ route('user.pengajuan.index') }}">
                                    Lihat semua pengajuan saya
                                </a>
                            </div>
                        </li>

                        <div class="topbar-divider d-none d-sm-block"></div>

                        <li class="nav-item">
                            <span class="nav-link text-gray-600 small">
                                Halo, <strong>{{ Auth::user()->username }}</strong>
                            </span>
                        </li>
                    </ul>
                </nav>

                <div class="container-fluid">

                    <div class="d-sm-flex align-items-center justify-content-between mb-4">
                        <h1 class="h3 mb-0 text-gray-800">Ringkasan Aktivitas</h1>
                    </div>

                    <div class="row">
                        <div class="col-xl-4 col-md-6 mb-4">
                            <div class="card border-left-warning shadow h-100 py-2">
                                <div class="card-body">
                                    <div class="row no-gutters align-items-center">
                                        <div class="col mr-2">
                                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                                Pengajuan Menunggu</div>
                                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ $totalMenunggu ?? 0 }} Surat</div>
                                        </div>
                                        <div class="col-auto">
                                            <i class="fas fa-clock fa-2x text-gray-300"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-4 col-md-6 mb-4">
                            <div class="card border-left-success shadow h-100 py-2">
                                <div class="card-body">
                                    <div class="row no-gutters align-items-center">
                                        <div class="col mr-2">
                                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                                Pengajuan Disetujui</div>
                                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ $totalDisetujui ?? 0 }} Surat</div>
                                        </div>
                                        <div class="col-auto">
                                            <i class="fas fa-check-circle fa-2x text-gray-300"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-4 col-md-6 mb-4">
                            <div class="card border-left-danger shadow h-100 py-2">
                                <div class="card-body">
                                    <div class="row no-gutters align-items-center">
                                        <div class="col mr-2">
                                            <div class="text-xs font-weight-bold text-danger text-uppercase mb-1">
                                                Pengajuan Ditolak</div>
                                            <div class="h5 mb-0 font-weight-bold text-gray-800">{{ $totalDitolak ?? 0 }} Surat</div>
                                        </div>
                                        <div class="col-auto">
                                            <i class="fas fa-times-circle fa-2x text-gray-300"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-12">
                            <div class="card shadow mb-4">
                                <div class="card-header py-3">
                                    <h6 class="m-0 font-weight-bold text-primary">Panduan Layanan Digital Desa</h6>
                                </div>

                                <div class="card-body border-bottom">
                                    <h6 class="font-weight-bold text-dark"><i class="fas fa-id-card mr-2"></i> Langkah 1: Validasi Data Diri</h6>
                                    <p class="small">Sebelum mengajukan surat, pastikan Anda telah mengunggah data NIK dan Foto KTP pada menu Validasi Penduduk untuk diverifikasi oleh admin.</p>

                                    <a href="{{ route('user.validasi.index') }}" class="btn btn-info btn-sm">
                                        Lengkapi Data Validasi
                                    </a>
                                </div>

                                <div class="card-body">
                                    <h6 class="font-weight-bold text-dark"><i class="fas fa-file-signature mr-2"></i> Langkah 2: Pengajuan Surat</h6>
                                    <p class="small">Jika data sudah tervalidasi, Anda dapat mengajukan berbagai jenis surat keterangan secara mandiri melalui tombol di bawah ini.</p>
                                    <a href="{{ route('user.pengajuan.index') }}" class="btn btn-primary btn-sm">
                                        <i class="fas fa-plus fa-sm mr-1"></i> Buat Pengajuan Baru
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>

            <footer class="sticky-footer bg-white">
                <div class="container my-auto">
                    <div class="copyright text-center my-auto">
                        <span>Copyright &copy; E-Surat Desa 2026</span>
                    </div>
                </div>
            </footer>

        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
    <script src="{{ asset('assets/js/sb-admin-2.min.js') }}"></script>
    <script>
        $(document).ready(function() {
            // 1. Ambil jumlah notif dari database (lewat variabel PHP)
            const totalNotifSekarang = {
                {
                    $totalNotifUser ?? 0
                }
            };

            // 2. Ambil data dari memori browser (jumlah yang terakhir kali dilihat user)
            const terakhirDilihat = localStorage.getItem('jumlahNotifTerakhir') || 0;

            // 3. LOGIKA: Jika jumlah di database lebih banyak dari yang terakhir dilihat
            // Artinya ada surat baru yang diupdate admin (Munculkan Merah)
            if (totalNotifSekarang > terakhirDilihat) {
                $('#badge-notif').show();
            } else {
                // Jika jumlahnya sama atau lebih sedikit, artinya sudah dibaca
                $('#badge-notif').hide();
            }

            // --- Fitur Highlight Baris (Tetap Ada) ---
            const urlParams = new URLSearchParams(window.location.search);
            const highlightId = urlParams.get('highlight');
            if (highlightId) {
                const targetRow = $('#surat-' + highlightId);
                if (targetRow.length) {
                    $('html, body').animate({
                        scrollTop: targetRow.offset().top - 100
                    }, 1000);
                    targetRow.addClass('table-warning');
                    setTimeout(function() {
                        targetRow.removeClass('table-warning');
                    }, 3000);
                }
            }
        });

        // 4. FUNGSI SAAT BEL DIKLIK
        function hilangkanBadge() {
            $('#badge-notif').fadeOut();

            // Simpan jumlah notif saat ini ke memori browser
            // Jadi saat refresh, browser tahu user sudah melihat angka ini
            const totalNotifSekarang = {
                {
                    $totalNotifUser ?? 0
                }
            };
            localStorage.setItem('jumlahNotifTerakhir', totalNotifSekarang);
        }
    </script>
</body>

</html>