<?php
session_start();
include '../koneksi.php';

if(!isset($_SESSION['admin'])){
    header("Location: login.php");
    exit();
}

// Ambil nama admin dari session jika ada
$nama_admin = isset($_SESSION['nama_admin']) ? $_SESSION['nama_admin'] : 'Admin';

// ========== AMBIL DATA STATISTIK ==========
// Total Berita
$query_total_berita = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM berita");
$total_berita = mysqli_fetch_assoc($query_total_berita)['total'] ?? 0;

// Total Galeri Foto
$query_galeri = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM arsip_foto");
$total_galeri = mysqli_fetch_assoc($query_galeri)['total'] ?? 0;

// Total Video
$query_video = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM arsip_video");
$total_video = mysqli_fetch_assoc($query_video)['total'] ?? 0;

// Total Pengaduan
$query_pengaduan = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM pengaduan");
$total_pengaduan = mysqli_fetch_assoc($query_pengaduan)['total'] ?? 0;

// ========== NOTIFIKASI ==========
// Buat tabel notifikasi jika belum ada
mysqli_query($koneksi, "
    CREATE TABLE IF NOT EXISTS notifications (
        id INT AUTO_INCREMENT PRIMARY KEY,
        jenis VARCHAR(50) NOT NULL,
        pesan TEXT NOT NULL,
        link VARCHAR(255),
        is_read TINYINT DEFAULT 0,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )
");

// Tambahkan notifikasi contoh jika tabel kosong
$cek_notif = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM notifications");
if($cek_notif && mysqli_num_rows($cek_notif) > 0){
    $row_notif = mysqli_fetch_assoc($cek_notif);
    if($row_notif['total'] == 0){
        mysqli_query($koneksi, "
            INSERT INTO notifications (jenis, pesan, link, is_read) VALUES 
            ('berita', 'Selamat datang di Dashboard Admin DPAD', '#', 0),
            ('pengaduan', 'Sistem pengaduan masyarakat siap digunakan', 'pengaduan_admin.php', 0),
            ('arsip', 'Kelola arsip foto dan video di menu Arsip', 'arsip_poto.php', 0)
        ");
    }
}

// Hitung notifikasi belum dibaca
$query_notif = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM notifications WHERE is_read = 0");
$total_notif = 0;
if($query_notif && mysqli_num_rows($query_notif) > 0){
    $row = mysqli_fetch_assoc($query_notif);
    $total_notif = $row['total'];
}

// Ambil 5 notifikasi terbaru
$query_list = mysqli_query($koneksi, "
    SELECT * FROM notifications 
    ORDER BY created_at DESC 
    LIMIT 5
");
$notifications = [];
if($query_list){
    while($row = mysqli_fetch_array($query_list)){
        $notifications[] = $row;
    }
}

// Ambil notifikasi pengaduan untuk bagian notifikasi
$query_pengaduan_list = mysqli_query($koneksi, "
    SELECT * FROM pengaduan 
    WHERE status = 'baru' 
    ORDER BY created_at DESC 
    LIMIT 5
");
$pengaduan_list = [];
if($query_pengaduan_list){
    while($row = mysqli_fetch_array($query_pengaduan_list)){
        $pengaduan_list[] = $row;
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard Admin | DPAD Solok Selatan</title>

    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;14..32,400;14..32,500;14..32,600;14..32,700;14..32,800&display=swap" rel="stylesheet">
    
    <!-- Font Awesome 6 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    
    <!-- AOS Animation -->
    <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: #f0f4f8;
            overflow-x: hidden;
        }

        /* ========== SIDEBAR MODERN PREMIUM ========== */
        .sidebar {
            width: 280px;
            height: 100vh;
            background: linear-gradient(135deg, #0a3d32 0%, #202706 50%, #0a2d24 100%);
            backdrop-filter: blur(10px);
            position: fixed;
            left: 0;
            top: 0;
            display: flex;
            flex-direction: column;
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            z-index: 999;
            box-shadow: 8px 0 30px rgba(0, 0, 0, 0.3), inset -1px 0 0 rgba(255,255,255,0.05);
            overflow-y: auto;
            overflow-x: hidden;
            border-right: 1px solid rgba(255,255,255,0.08);
        }

        .sidebar::after {
            content: '';
            position: absolute;
            top: 0;
            right: 0;
            width: 4px;
            height: 100%;
            background: linear-gradient(180deg, #f5c542, #eab308, #f5c542);
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .sidebar:hover::after {
            opacity: 0.6;
        }

        .sidebar::-webkit-scrollbar {
            width: 4px;
        }
        .sidebar::-webkit-scrollbar-track {
            background: rgba(255,255,255,0.05);
            border-radius: 10px;
        }
        .sidebar::-webkit-scrollbar-thumb {
            background: linear-gradient(180deg, #f5c542, #eab308);
            border-radius: 10px;
        }
        .sidebar::-webkit-scrollbar-thumb:hover {
            background: linear-gradient(180deg, #fbbf24, #f59e0b);
        }

        /* ========== LOGO AREA PREMIUM ========== */
        .logo {
            text-align: center;
            padding: 35px 20px 30px;
            border-bottom: 1px solid rgba(255,255,255,0.08);
            margin-bottom: 25px;
            position: relative;
        }

        .logo::before {
            content: '✦';
            position: absolute;
            bottom: -12px;
            left: 50%;
            transform: translateX(-50%);
            color: #f5c542;
            font-size: 14px;
            background: #0a3d32;
            padding: 0 10px;
        }

        .logo-img {
            width: 85px;
            height: 85px;
            margin: 0 auto 15px;
            background: linear-gradient(135deg, rgba(255,255,255,0.15), rgba(255,255,255,0.05));
            border-radius: 25px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
            overflow: hidden;
            cursor: pointer;
            box-shadow: 0 10px 20px rgba(0,0,0,0.2);
            border: 1px solid rgba(255,255,255,0.1);
        }

        .logo-img:hover {
            transform: scale(1.05) rotate(5deg);
            background: linear-gradient(135deg, rgba(245,197,66,0.2), rgba(245,197,66,0.05));
            box-shadow: 0 15px 30px rgba(0,0,0,0.3);
            border-color: rgba(245,197,66,0.3);
        }

        .logo-img img {
            width: 60px;
            height: 60px;
            object-fit: contain;
            transition: all 0.3s ease;
        }

        .logo-img:hover img {
            transform: scale(1.1);
        }

        .logo-text h2 {
            color: white;
            font-size: 1.4rem;
            font-weight: 800;
            letter-spacing: -0.5px;
            margin-bottom: 8px;
            background: linear-gradient(135deg, #fff, #f5c542);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            text-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .logo-text p {
            color: rgba(255,255,255,0.6);
            font-size: 0.7rem;
            margin-bottom: 4px;
            letter-spacing: 0.5px;
        }

        .logo-text .subtitle {
            color: #f5c542;
            font-size: 0.68rem;
            margin-top: 5px;
            font-weight: 700;
            letter-spacing: 1px;
            text-transform: uppercase;
            background: rgba(245,197,66,0.15);
            display: inline-block;
            padding: 3px 12px;
            border-radius: 20px;
        }

        /* ========== NAVIGATION MENU PREMIUM ========== */
        .nav-menu {
            flex: 1;
            padding: 0 15px;
        }

        .nav-footer {
            padding: 20px 15px;
            border-top: 1px solid rgba(255,255,255,0.08);
            margin-top: auto;
            position: relative;
        }

        .nav-footer::before {
            content: '';
            position: absolute;
            top: -2px;
            left: 20px;
            right: 20px;
            height: 1px;
            background: linear-gradient(90deg, transparent, rgba(245,197,66,0.3), transparent);
        }

        .nav-item {
            display: flex;
            align-items: center;
            gap: 15px;
            padding: 12px 16px;
            margin-bottom: 10px;
            border-radius: 16px;
            text-decoration: none;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            position: relative;
            overflow: hidden;
        }

        .nav-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            width: 0;
            background: linear-gradient(90deg, rgba(245,197,66,0.15), rgba(245,197,66,0.05));
            transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            border-radius: 16px;
        }

        .nav-item:hover::before {
            width: 100%;
        }

        .nav-item:hover {
            transform: translateX(8px);
        }

        .nav-item::after {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            width: 3px;
            background: linear-gradient(180deg, #f5c542, #eab308);
            border-radius: 3px;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .nav-item:hover::after {
            opacity: 1;
        }

        .nav-item .nav-icon {
            width: 44px;
            height: 44px;
            background: rgba(255,255,255,0.06);
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
            position: relative;
            z-index: 1;
            border: 1px solid rgba(255,255,255,0.05);
        }

        .nav-item .nav-icon i {
            font-size: 1.25rem;
            color: rgba(255,255,255,0.7);
            transition: all 0.3s ease;
        }

        .nav-item .nav-text {
            position: relative;
            z-index: 1;
        }

        .nav-item .nav-text span {
            display: block;
            color: rgba(255,255,255,0.85);
            font-weight: 600;
            font-size: 0.9rem;
            transition: all 0.3s ease;
            letter-spacing: -0.2px;
        }

        .nav-item .nav-text small {
            display: block;
            color: rgba(255,255,255,0.4);
            font-size: 0.65rem;
            margin-top: 3px;
            transition: all 0.3s ease;
            letter-spacing: 0.3px;
        }

        .nav-item:not(.logout):hover .nav-icon {
            background: linear-gradient(135deg, rgba(245,197,66,0.2), rgba(245,197,66,0.05));
            transform: scale(1.05) translateY(-2px);
            border-color: rgba(245,197,66,0.3);
        }

        .nav-item:not(.logout):hover .nav-icon i {
            color: #f5c542;
            text-shadow: 0 0 5px rgba(245,197,66,0.5);
            transform: scale(1.05);
        }

        .nav-item:not(.logout):hover .nav-text span {
            color: white;
            letter-spacing: -0.1px;
        }

        .nav-item:not(.logout):hover .nav-text small {
            color: rgba(245,197,66,0.7);
        }

        .nav-item.active {
            background: linear-gradient(135deg, #f5c542, #eab308);
            box-shadow: 0 6px 20px rgba(245,197,66,0.35), inset 0 1px 0 rgba(255,255,255,0.2);
        }

        .nav-item.active .nav-icon {
            background: rgba(0,0,0,0.12);
            transform: scale(1.02);
            border-color: rgba(0,0,0,0.1);
        }

        .nav-item.active .nav-icon i {
            color: #0f3b2c;
            text-shadow: none;
        }

        .nav-item.active .nav-text span {
            color: #0f3b2c;
            font-weight: 800;
        }

        .nav-item.active .nav-text small {
            color: rgba(15,59,44,0.75);
            font-weight: 500;
        }

        .nav-item.active:hover {
            transform: translateX(5px);
        }

        .nav-item.logout {
            margin-top: 8px;
        }

        .nav-item.logout .nav-icon {
            background: rgba(239,68,68,0.12);
            border-color: rgba(239,68,68,0.15);
        }

        .nav-item.logout .nav-icon i {
            color: #ef4444;
        }

        .nav-item.logout:hover .nav-icon {
            background: rgba(239,68,68,0.25);
            transform: scale(1.05);
            border-color: rgba(239,68,68,0.3);
        }

        .nav-item.logout:hover .nav-icon i {
            color: #f87171;
            text-shadow: 0 0 8px rgba(239,68,68,0.5);
        }

        .nav-item.logout:hover .nav-text span {
            color: #f87171;
        }

        /* ========== NOTIFIKASI ICON DI HEADER ========== */
        .header-notif {
            position: relative;
            display: inline-block;
            margin-left: 15px;
        }

        .header-notif .badge {
            position: absolute;
            top: -8px;
            right: -8px;
            background: #ef4444;
            color: white;
            font-size: 0.6rem;
            font-weight: 700;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            border: 2px solid white;
        }

        .notif-dropdown-admin {
            position: absolute;
            top: 50px;
            right: 0;
            width: 380px;
            max-height: 420px;
            background: white;
            border-radius: 16px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.15);
            overflow: hidden;
            opacity: 0;
            visibility: hidden;
            transform: translateY(10px);
            transition: all 0.3s ease;
            z-index: 1000;
        }

        .notif-dropdown-admin.active {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }

        .notif-header-admin {
            padding: 15px 20px;
            border-bottom: 1px solid #e2e8f0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: white;
        }

        .notif-header-admin span {
            font-weight: 700;
            color: #1e293b;
            font-size: 0.95rem;
        }

        .notif-header-admin a {
            font-size: 0.7rem;
            color: #1a5f4b;
            text-decoration: none;
        }

        .notif-header-admin a:hover {
            text-decoration: underline;
        }

        .notif-list-admin {
            max-height: 340px;
            overflow-y: auto;
        }

        .notif-list-admin::-webkit-scrollbar {
            width: 4px;
        }

        .notif-list-admin::-webkit-scrollbar-track {
            background: #f1f5f9;
        }

        .notif-list-admin::-webkit-scrollbar-thumb {
            background: #cbd5e1;
            border-radius: 10px;
        }

        .notif-item-admin {
            padding: 12px 20px;
            border-bottom: 1px solid #f1f5f9;
            display: flex;
            align-items: center;
            gap: 12px;
            transition: 0.2s;
            cursor: pointer;
        }

        .notif-item-admin:hover {
            background: #f8fafc;
        }

        .notif-item-admin.unread {
            background: #f0fdf4;
            border-left: 3px solid #1a5f4b;
        }

        .notif-item-admin .icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }

        .notif-item-admin .icon.berita {
            background: #dbeafe;
            color: #2563eb;
        }

        .notif-item-admin .icon.pengaduan {
            background: #fef3c7;
            color: #d97706;
        }

        .notif-item-admin .icon.arsip {
            background: #dcfce7;
            color: #1a5f4b;
        }

        .notif-item-admin .content {
            flex: 1;
        }

        .notif-item-admin .content .pesan {
            font-size: 0.85rem;
            color: #1e293b;
        }

        .notif-item-admin .content .waktu {
            font-size: 0.65rem;
            color: #94a3b8;
            margin-top: 3px;
        }

        .notif-empty-admin {
            text-align: center;
            padding: 40px;
            color: #94a3b8;
        }

        .notif-empty-admin i {
            font-size: 30px;
            margin-bottom: 10px;
            display: block;
        }

        /* ========== RESPONSIVE ========== */
        @media (max-width: 992px) {
            .sidebar {
                width: 260px;
            }
            .main-content {
                margin-left: 260px;
            }
        }

        @media (max-width: 768px) {
            .sidebar {
                width: 72px;
            }
            
            .sidebar:hover {
                width: 260px;
            }
            
            .sidebar:hover .logo-text h2,
            .sidebar:hover .logo-text p,
            .sidebar:hover .logo-text .subtitle,
            .sidebar:hover .nav-text {
                display: block;
            }
            
            .logo {
                padding: 20px 0;
            }
            
            .logo-img {
                width: 48px;
                height: 48px;
            }
            
            .logo-img img {
                width: 34px;
                height: 34px;
            }
            
            .logo-text h2,
            .logo-text p,
            .logo-text .subtitle,
            .nav-text {
                display: none;
            }
            
            .sidebar:hover .logo {
                padding: 30px 15px;
            }
            
            .sidebar:hover .logo-img {
                width: 75px;
                height: 75px;
                margin: 0 auto 15px;
            }
            
            .sidebar:hover .logo-img img {
                width: 52px;
                height: 52px;
            }
            
            .nav-item {
                justify-content: center;
                padding: 12px;
                margin-bottom: 8px;
            }
            
            .sidebar:hover .nav-item {
                justify-content: flex-start;
                padding: 12px 16px;
            }
            
            .nav-item .nav-icon {
                margin: 0;
            }
            
            .nav-footer {
                padding: 15px 0;
            }
            
            .sidebar:hover .nav-footer {
                padding: 20px 15px;
            }
            
            .main-content {
                margin-left: 72px;
                padding: 15px;
            }

            .notif-dropdown-admin {
                width: 320px;
                right: -80px;
            }
        }

        @media (max-width: 480px) {
            .notif-dropdown-admin {
                width: 290px;
                right: -100px;
            }
        }

        /* ========== MAIN CONTENT ========== */
        .main-content {
            margin-left: 280px;
            padding: 25px 35px;
            min-height: 100vh;
            background: linear-gradient(135deg, #f0f4f8 0%, #e8edf2 100%);
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            position: relative;
        }

        .main-content::before {
            content: '';
            position: fixed;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            background-image: radial-gradient(circle at 10% 20%, rgba(245,197,66,0.02) 0%, transparent 50%);
            pointer-events: none;
            z-index: -1;
        }

        /* Topbar / Header */
        .topbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: white;
            padding: 18px 28px;
            border-radius: 20px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.05);
            margin-bottom: 30px;
            position: relative;
        }

        .topbar-left {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .welcome h1 {
            font-size: 1.6rem;
            font-weight: 700;
            color: #1e2f3a;
        }

        .welcome h1 span {
            color: #1a5f4b;
        }

        .welcome p {
            color: #6c7a89;
            font-size: 0.85rem;
            margin-top: 5px;
        }

        .admin-info {
            display: flex;
            align-items: center;
            gap: 15px;
            background: #f8fafc;
            padding: 8px 20px;
            border-radius: 50px;
        }

        .admin-info i {
            font-size: 2rem;
            color: #1a5f4b;
        }

        .admin-info .admin-name {
            font-weight: 600;
            color: #1e2f3a;
        }

        /* Statistik Cards */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 25px;
            margin-bottom: 35px;
        }

        .stat-card {
            background: white;
            padding: 25px;
            border-radius: 24px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-left: 4px solid #f5c542;
        }

        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.1);
        }

        .stat-info h3 {
            font-size: 0.85rem;
            font-weight: 600;
            color: #6c7a89;
            letter-spacing: 1px;
        }

        .stat-info .number {
            font-size: 2.5rem;
            font-weight: 800;
            color: #1a5f4b;
            margin-top: 8px;
        }

        .stat-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #e8f5e9, #c8e6d9);
            border-radius: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: 0.3s;
        }

        .stat-card:hover .stat-icon {
            transform: scale(1.05) rotate(5deg);
        }

        .stat-icon i {
            font-size: 2rem;
            color: #1a5f4b;
        }

        /* Section Title */
        .section-title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 30px 0 20px 0;
        }

        .section-title h2 {
            font-size: 1.3rem;
            font-weight: 700;
            color: #1e2f3a;
        }

        /* Menu Cards (Aksi Cepat) */
        .action-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 35px;
        }

        .action-card {
            background: white;
            padding: 25px 20px;
            border-radius: 20px;
            text-align: center;
            transition: all 0.3s;
            text-decoration: none;
            color: #1e2f3a;
            box-shadow: 0 4px 10px rgba(0,0,0,0.05);
            border: 1px solid #eef2f5;
        }

        .action-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 30px rgba(0,0,0,0.1);
            border-color: #1a5f4b;
        }

        .action-card i {
            font-size: 2.5rem;
            color: #1a5f4b;
            margin-bottom: 15px;
        }

        .action-card h3 {
            font-size: 1rem;
            margin-bottom: 8px;
        }

        .action-card p {
            font-size: 0.75rem;
            color: #7f8c8d;
        }

        /* Notification Section */
        .notification-section {
            background: white;
            border-radius: 24px;
            padding: 20px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
        }

        .notification-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eef2f5;
        }

        .notification-header h3 {
            font-size: 1.1rem;
            color: #1e2f3a;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .notification-header h3 i {
            color: #f5c542;
        }

        .view-all {
            color: #1a5f4b;
            text-decoration: none;
            font-size: 0.8rem;
            font-weight: 500;
            transition: 0.3s;
        }

        .view-all:hover {
            color: #0f3b2c;
            transform: translateX(3px);
        }

        .notification-list {
            max-height: 300px;
            overflow-y: auto;
        }

        .notif-item {
            display: flex;
            align-items: flex-start;
            gap: 15px;
            padding: 15px;
            border-radius: 16px;
            margin-bottom: 10px;
            background: #fff9e6;
            transition: all 0.3s;
            cursor: pointer;
            text-decoration: none;
            color: inherit;
            border-left: 3px solid #f5c542;
        }

        .notif-item:hover {
            background: #f0fdf4;
            transform: translateX(5px);
        }

        .notif-icon {
            width: 45px;
            height: 45px;
            background: #dcfce7;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }

        .notif-icon i {
            font-size: 20px;
            color: #1a5f4b;
        }

        .notif-content {
            flex: 1;
        }

        .notif-title {
            font-weight: 700;
            color: #1e2f3a;
            margin-bottom: 5px;
        }

        .notif-badge {
            background: #ef4444;
            color: white;
            font-size: 0.6rem;
            padding: 2px 8px;
            border-radius: 30px;
            font-weight: 600;
            margin-left: 8px;
        }

        .notif-message {
            font-size: 0.8rem;
            color: #64748b;
            margin-bottom: 5px;
            line-height: 1.4;
        }

        .notif-time {
            font-size: 0.65rem;
            color: #94a3b8;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .empty-notif {
            text-align: center;
            padding: 30px;
            color: #94a3b8;
        }

        .empty-notif i {
            font-size: 40px;
            margin-bottom: 10px;
            opacity: 0.5;
        }

        /* Footer */
        .footer {
            text-align: center;
            margin-top: 40px;
            padding: 20px;
            color: #8ba0aa;
            font-size: 0.75rem;
        }

        /* Responsive */
        @media (max-width: 992px) {
            .stats-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        @media (max-width: 768px) {
            .topbar {
                flex-direction: column;
                text-align: center;
                gap: 10px;
            }
            .topbar-left {
                flex-direction: column;
            }
            .stats-grid {
                grid-template-columns: 1fr;
            }
            .action-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>

<!-- SIDEBAR MODERN -->
<div class="sidebar">
    <div class="logo">
        <div class="logo-img">
            <img src="../img/logo.png" alt="Logo DPAD" onerror="this.src='https://placehold.co/60x60/1a5f4b/white?text=DPAD'">
        </div>
        <div class="logo-text">
            <h2>ADMIN</h2>
            <p>Dinas Perpustakaan </p>
            <p class="subtitle">Kab. Solok Selatan</p>
        </div>
    </div>

    <div class="nav-menu">
        <!-- DASHBOARD -->
        <a href="index.php" class="nav-item active">
            <div class="nav-icon"><i class="fas fa-tachometer-alt"></i></div>
            <div class="nav-text"><span>Dashboard</span><small>Halaman Utama</small></div>
        </a>

        <!-- TAMBAH BERITA -->
        <a href="tambah_berita.php" class="nav-item">
            <div class="nav-icon"><i class="fas fa-plus-circle"></i></div>
            <div class="nav-text"><span>Tambah Berita</span><small>Publikasi berita baru</small></div>
        </a>

        <!-- DATA BERITA -->
        <a href="../berita.php" class="nav-item">
            <div class="nav-icon"><i class="fas fa-newspaper"></i></div>
            <div class="nav-text"><span>Data Berita</span><small>Kelola semua berita</small></div>
        </a>

        <!-- EDIT BERITA -->
        <a href="../edit_berita.php" class="nav-item">
            <div class="nav-icon"><i class="fas fa-edit"></i></div>
            <div class="nav-text"><span>Edit Berita</span><small>Edit atau update berita</small></div>
        </a>

        <!-- ARSIP FOTO SEJARAH -->
        <a href="arsip_poto.php" class="nav-item">
            <div class="nav-icon"><i class="fas fa-history"></i></div>
            <div class="nav-text"><span>Arsip Foto</span><small>Kelola arsip Poto</small></div>
        </a>

        <!-- EDIT VISI MISI -->
        <a href="edit_visi_misi.php" class="nav-item">
            <div class="nav-icon"><i class="fas fa-edit"></i></div>
            <div class="nav-text"><span>Edit Visi & Misi</span><small>Kelola Visi & Misi</small></div>
        </a>

        <!-- PENGADUAN -->
        <a href="pengaduan_admin.php" class="nav-item">
            <div class="nav-icon"><i class="fas fa-comments"></i></div>
            <div class="nav-text"><span>Pengaduan</span><small>Kelola pengaduan</small></div>
        </a>

        <!-- VIDEO -->
        <a href="tambah_video.php" class="nav-item">
            <div class="nav-icon"><i class="fas fa-video"></i></div>
            <div class="nav-text"><span>Video</span><small>Upload video baru</small></div>
        </a>
    </div>

    <div class="nav-footer">
        <!-- LOGOUT -->
        <a href="logout.php" class="nav-item logout">
            <div class="nav-icon"><i class="fas fa-sign-out-alt"></i></div>
            <div class="nav-text"><span>Logout</span><small>Keluar dari dashboard</small></div>
        </a>
    </div>
</div>

<!-- MAIN CONTENT -->
<div class="main-content">

    <!-- TOPBAR -->
    <div class="topbar">
        <div class="topbar-left">
            <div class="welcome">
                <h1>Selamat Datang, <span><?php echo htmlspecialchars($nama_admin); ?></span></h1>
                <p>Kelola berita dan informasi publik Dinas Kearsipan Solok Selatan</p>
            </div>
        </div>
        <div class="admin-info">
            <!-- ICON NOTIFIKASI DI HEADER -->
            <div class="header-notif">
                <a href="#" onclick="toggleNotifAdmin()" style="color: #1a5f4b; font-size: 1.3rem; text-decoration: none; position: relative;">
                    <i class="fas fa-bell"></i>
                    <span class="badge" id="notifBadgeAdmin"><?php echo $total_notif > 0 ? $total_notif : ''; ?></span>
                </a>
                
                <!-- DROPDOWN NOTIFIKASI -->
                <div class="notif-dropdown-admin" id="notifDropdownAdmin">
                    <div class="notif-header-admin">
                        <span><i class="fas fa-bell" style="color:#1a5f4b;"></i> Notifikasi</span>
                        <a href="#" onclick="markAllReadAdmin()">Tandai semua dibaca</a>
                    </div>
                    <div class="notif-list-admin" id="notifListAdmin">
                        <?php if(count($notifications) > 0): ?>
                            <?php foreach($notifications as $notif): ?>
                                <?php 
                                    $iconClass = $notif['jenis'] == 'berita' ? 'berita' : ($notif['jenis'] == 'pengaduan' ? 'pengaduan' : 'arsip');
                                    $icon = $notif['jenis'] == 'berita' ? 'fa-newspaper' : ($notif['jenis'] == 'pengaduan' ? 'fa-comment-dots' : 'fa-archive');
                                    $unread = $notif['is_read'] == 0 ? 'unread' : '';
                                ?>
                                <div class="notif-item-admin <?php echo $unread; ?>" onclick="markReadAdmin(<?php echo $notif['id']; ?>)">
                                    <div class="icon <?php echo $iconClass; ?>">
                                        <i class="fas <?php echo $icon; ?>"></i>
                                    </div>
                                    <div class="content">
                                        <div class="pesan"><?php echo htmlspecialchars($notif['pesan']); ?></div>
                                        <div class="waktu"><?php echo date('d/m/Y H:i', strtotime($notif['created_at'])); ?></div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <div class="notif-empty-admin">
                                <i class="fas fa-inbox"></i>
                                <p>Belum ada notifikasi</p>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <i class="fas fa-user-circle"></i>
            <div class="admin-name"><?php echo htmlspecialchars($nama_admin); ?></div>
        </div>
    </div>

    <!-- STATISTIK CARD -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-info">
                <h3>TOTAL BERITA</h3>
                <div class="number"><?php echo $total_berita; ?></div>
            </div>
            <div class="stat-icon">
                <i class="fas fa-newspaper"></i>
            </div>
        </div>
        <div class="stat-card" style="border-left-color: #3b82f6;">
            <div class="stat-info">
                <h3>GALERI FOTO</h3>
                <div class="number"><?php echo $total_galeri; ?></div>
            </div>
            <div class="stat-icon" style="background: linear-gradient(135deg, #dbeafe, #bfdbfe);">
                <i class="fas fa-images" style="color: #2563eb;"></i>
            </div>
        </div>
        <div class="stat-card" style="border-left-color: #f59e0b;">
            <div class="stat-info">
                <h3>ARSIP VIDEO</h3>
                <div class="number"><?php echo $total_video; ?></div>
            </div>
            <div class="stat-icon" style="background: linear-gradient(135deg, #fef3c7, #fde68a);">
                <i class="fas fa-video" style="color: #d97706;"></i>
            </div>
        </div>
        <div class="stat-card" style="border-left-color: #8b5cf6;">
            <div class="stat-info">
                <h3>PENGADUAN</h3>
                <div class="number"><?php echo $total_pengaduan; ?></div>
            </div>
            <div class="stat-icon" style="background: linear-gradient(135deg, #ede9fe, #ddd6fe);">
                <i class="fas fa-comments" style="color: #7c3aed;"></i>
            </div>
        </div>
    </div>

    <!-- NOTIFIKASI PENGADUAN -->
    <div class="notification-section">
        <div class="notification-header">
            <h3><i class="fas fa-bell"></i> Notifikasi Pengaduan</h3>
            <a href="pengaduan_admin.php" class="view-all">Lihat Semua <i class="fas fa-arrow-right"></i></a>
        </div>
        <div class="notification-list" id="notificationList">
            <?php if(count($pengaduan_list) > 0): ?>
                <?php foreach($pengaduan_list as $pengaduan): ?>
                    <a href="pengaduan_admin.php?id=<?php echo $pengaduan['id']; ?>" class="notif-item">
                        <div class="notif-icon">
                            <i class="fas fa-comment-dots"></i>
                        </div>
                        <div class="notif-content">
                            <div class="notif-title">
                                <?php echo htmlspecialchars($pengaduan['judul']); ?>
                                <span class="notif-badge">Baru</span>
                            </div>
                            <div class="notif-message">
                                Dari: <?php echo htmlspecialchars($pengaduan['nama']); ?> | <?php echo htmlspecialchars($pengaduan['kategori']); ?>
                            </div>
                            <div class="notif-time">
                                <i class="far fa-clock"></i> <?php echo date('d/m/Y H:i', strtotime($pengaduan['created_at'])); ?>
                            </div>
                        </div>
                    </a>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="empty-notif">
                    <i class="fas fa-inbox"></i>
                    <p>Tidak ada pengaduan baru</p>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- MENU AKSI CEPAT -->
    <div class="section-title">
        <h2><i class="fas fa-bolt"></i> Aksi Cepat</h2>
    </div>
    <div class="action-grid">
        <a href="tambah_berita.php" class="action-card">
            <i class="fas fa-plus-circle"></i>
            <h3>Tambah Berita</h3>
            <p>Publikasikan berita baru</p>
        </a>
        <a href="../berita.php" class="action-card">
            <i class="fas fa-list"></i>
            <h3>Data Berita</h3>
            <p>Lihat semua berita</p>
        </a>
        <a href="../edit_berita.php" class="action-card">
            <i class="fas fa-edit"></i>
            <h3>Edit Berita</h3>
            <p>Perbarui informasi</p>
        </a>
        <a href="tambah_video.php" class="action-card">
            <i class="fas fa-video"></i>
            <h3>Upload Video</h3>
            <p>Upload video baru</p>
        </a>
        <a href="arsip_poto.php" class="action-card">
            <i class="fas fa-images"></i>
            <h3>Kelola Galeri</h3>
            <p>Kelola foto & galeri</p>
        </a>
        <a href="pengaduan_admin.php" class="action-card">
            <i class="fas fa-comments"></i>
            <h3>Pengaduan</h3>
            <p>Kelola pengaduan</p>
        </a>
    </div>

    <div class="footer">
        &copy; <?php echo date('Y'); ?> Dinas Perpustakaan dan Arsip Daerah Solok Selatan. All rights reserved.
    </div>
</div>

<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
<script>
    AOS.init({
        duration: 600,
        once: true,
        offset: 50
    });

    // ========== FUNGSI NOTIFIKASI ==========
    function toggleNotifAdmin() {
        const dropdown = document.getElementById('notifDropdownAdmin');
        dropdown.classList.toggle('active');
        if(dropdown.classList.contains('active')) {
            loadNotificationsAdmin();
        }
    }

    function loadNotificationsAdmin() {
        fetch('get_notifikasi_admin.php')
            .then(response => response.json())
            .then(data => {
                const badge = document.getElementById('notifBadgeAdmin');
                const list = document.getElementById('notifListAdmin');
                
                if(data.total > 0) {
                    badge.textContent = data.total > 99 ? '99+' : data.total;
                    badge.style.display = 'flex';
                } else {
                    badge.style.display = 'none';
                }
                
                if(data.list && data.list.length > 0) {
                    let html = '';
                    data.list.forEach(notif => {
                        const iconClass = notif.jenis === 'berita' ? 'berita' : 
                                         notif.jenis === 'pengaduan' ? 'pengaduan' : 'arsip';
                        const icon = notif.jenis === 'berita' ? 'fa-newspaper' :
                                    notif.jenis === 'pengaduan' ? 'fa-comment-dots' : 'fa-archive';
                        const unreadClass = notif.is_read == 0 ? 'unread' : '';
                        
                        html += `
                            <div class="notif-item-admin ${unreadClass}" onclick="markReadAdmin(${notif.id})">
                                <div class="icon ${iconClass}">
                                    <i class="fas ${icon}"></i>
                                </div>
                                <div class="content">
                                    <div class="pesan">${notif.pesan}</div>
                                    <div class="waktu">${notif.waktu}</div>
                                </div>
                            </div>
                        `;
                    });
                    list.innerHTML = html;
                } else {
                    list.innerHTML = `
                        <div class="notif-empty-admin">
                            <i class="fas fa-inbox"></i>
                            <p>Belum ada notifikasi</p>
                        </div>
                    `;
                }
            })
            .catch(error => {
                console.error('Error:', error);
            });
    }

    function markReadAdmin(id) {
        fetch('mark_notif_read_admin.php?id=' + id)
            .then(response => response.json())
            .then(data => {
                if(data.success) {
                    loadNotificationsAdmin();
                    // Update badge
                    fetch('get_notifikasi_admin.php')
                        .then(res => res.json())
                        .then(data => {
                            const badge = document.getElementById('notifBadgeAdmin');
                            if(data.total > 0) {
                                badge.textContent = data.total > 99 ? '99+' : data.total;
                                badge.style.display = 'flex';
                            } else {
                                badge.style.display = 'none';
                            }
                        });
                }
            });
    }

    function markAllReadAdmin() {
        fetch('mark_all_read_admin.php')
            .then(response => response.json())
            .then(data => {
                if(data.success) {
                    document.getElementById('notifBadgeAdmin').style.display = 'none';
                    document.getElementById('notifDropdownAdmin').classList.remove('active');
                    loadNotificationsAdmin();
                }
            });
    }

    // Tutup dropdown saat klik di luar
    document.addEventListener('click', function(e) {
        const dropdown = document.getElementById('notifDropdownAdmin');
        const btn = document.querySelector('.header-notif a');
        if(dropdown && !dropdown.contains(e.target) && !btn?.contains(e.target)) {
            dropdown.classList.remove('active');
        }
    });

    // Load notifikasi saat halaman dimuat
    document.addEventListener('DOMContentLoaded', function() {
        loadNotificationsAdmin();
        // Refresh notifikasi setiap 30 detik
        setInterval(loadNotificationsAdmin, 30000);
    });
</script>

</body>
</html>