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

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

// ====== TAMBAHKAN KODE INI DI SINI ======
if(isset($_GET['hapus'])){
    $id = mysqli_real_escape_string($koneksi, $_GET['hapus']);
    $query = mysqli_query($koneksi, "DELETE FROM pengaduan WHERE id='$id'");
    if($query){
        header("Location: pengaduan_admin.php");
        exit();
    }
}

// Tandai sebagai dibaca jika ada parameter id
if(isset($_GET['id'])){
    $id = mysqli_real_escape_string($koneksi, $_GET['id']);
    mysqli_query($koneksi, "UPDATE pengaduan SET status='dibaca' WHERE id='$id'");
}

// Proses update status
if(isset($_POST['update_status'])){
    $id = mysqli_real_escape_string($koneksi, $_POST['id']);
    $status = mysqli_real_escape_string($koneksi, $_POST['status']);
    mysqli_query($koneksi, "UPDATE pengaduan SET status='$status' WHERE id='$id'");
    echo "<script>alert('✅ Status berhasil diupdate!'); window.location='pengaduan_admin.php';</script>";
    exit();
}

// Ambil semua pengaduan
$query = mysqli_query($koneksi, "SELECT * FROM pengaduan ORDER BY created_at DESC");
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manajemen Pengaduan | Admin DPAD</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::-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;
        }

        .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;
        }

        .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.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;
        }

        @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;
            }
        }

        /* ========== MAIN CONTENT PREMIUM ========== */
        .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);
        }

        @media (max-width: 768px) {
            .main-content {
                margin-left: 72px;
                padding: 15px;
            }
        }

        /* Topbar */
        .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;
        }

        .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: 20px;
            margin-bottom: 35px;
        }

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

        .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;
        }

        .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;
            flex-wrap: wrap;
            gap: 15px;
        }

        .title-left {
            display: flex;
            align-items: center;
            gap: 15px;
        }

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

        .title-left .total-count {
            background: #e2e8f0;
            padding: 4px 14px;
            border-radius: 30px;
            font-size: 0.75rem;
            font-weight: 600;
            color: #475569;
        }

        /* ====== SEARCH BOX ====== */
        .search-box {
            display: flex;
            align-items: center;
            background: white;
            border: 2px solid #e8edf2;
            border-radius: 12px;
            padding: 0 10px;
            transition: all 0.3s ease;
            min-width: 260px;
        }

        .search-box:focus-within {
            border-color: #1a5f4b;
            box-shadow: 0 0 0 4px rgba(26,95,75,0.08);
        }

        .search-box .search-icon {
            color: #94a3b8;
            font-size: 0.9rem;
            margin-right: 8px;
        }

        .search-box input {
            border: none;
            padding: 12px 8px;
            width: 100%;
            font-family: 'Inter', sans-serif;
            font-size: 0.9rem;
            background: transparent;
            outline: none;
            color: #1e293b;
        }

        .search-box input::placeholder {
            color: #94a3b8;
        }

        .search-box .btn-filter {
            background: none;
            border: none;
            color: #94a3b8;
            cursor: pointer;
            padding: 8px 10px;
            transition: 0.3s;
            display: none;
        }

        .search-box .btn-filter.show {
            display: block;
        }

        .search-box .btn-filter:hover {
            color: #ef4444;
        }

        /* ====== FILTER WRAP ====== */
        .filter-wrap {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
            margin-bottom: 25px;
            padding: 15px 20px;
            background: white;
            border-radius: 16px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.04);
            border: 1px solid #f1f5f9;
        }

        .filter-buttons {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .filter-btn {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 8px 16px;
            border: 2px solid #e8edf2;
            border-radius: 30px;
            background: white;
            color: #64748b;
            font-size: 0.75rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .filter-btn:hover {
            border-color: #1a5f4b;
            color: #1a5f4b;
            transform: translateY(-2px);
        }

        .filter-btn.active {
            background: linear-gradient(135deg, #1a5f4b, #0f3b2c);
            color: white;
            border-color: #1a5f4b;
            box-shadow: 0 4px 12px rgba(26,95,75,0.25);
        }

        .filter-btn i {
            font-size: 0.8rem;
        }

        .filter-status select {
            padding: 10px 16px;
            border: 2px solid #e8edf2;
            border-radius: 30px;
            font-size: 0.8rem;
            font-weight: 500;
            background: white;
            color: #1e293b;
            cursor: pointer;
            outline: none;
            transition: 0.3s;
            font-family: 'Inter', sans-serif;
            min-width: 150px;
        }

        .filter-status select:focus {
            border-color: #1a5f4b;
            box-shadow: 0 0 0 4px rgba(26,95,75,0.08);
        }

        /* ====== RESULT INFO ====== */
        #searchResult {
            margin-bottom: 20px;
            display: none;
        }

        .result-info {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 12px 20px;
            background: #f0fdf4;
            border-radius: 12px;
            border-left: 4px solid #1a5f4b;
            font-size: 0.9rem;
            color: #065f46;
            flex-wrap: wrap;
        }

        .result-info i {
            font-size: 1.2rem;
            color: #1a5f4b;
        }

        .result-info strong {
            color: #0f3b2c;
        }

        .result-info #resultCount {
            margin-left: auto;
            background: #dcfce7;
            padding: 2px 12px;
            border-radius: 30px;
            font-size: 0.75rem;
            font-weight: 600;
        }

        /* ====== NO RESULT ====== */
        .no-result {
            text-align: center;
            padding: 60px 20px;
            background: white;
            border-radius: 20px;
            border: 1px solid #f1f5f9;
        }

        .no-result i {
            font-size: 50px;
            color: #cbd5e1;
            margin-bottom: 15px;
        }

        .no-result h3 {
            color: #1e293b;
            margin-bottom: 5px;
        }

        .no-result p {
            color: #64748b;
            font-size: 0.9rem;
        }

        /* ========== PENGADUAN CARD ========== */
        .pengaduan-list {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .pengaduan-card {
            background: white;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.04);
            transition: all 0.3s;
            border: 1px solid #f1f5f9;
        }

        .pengaduan-card:hover {
            box-shadow: 0 8px 25px rgba(0,0,0,0.08);
            transform: translateY(-2px);
        }

        .card-header {
            padding: 18px 25px;
            background: #fafbfc;
            border-bottom: 1px solid #f0f2f5;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 12px;
        }

        .badge-id {
            background: #e2e8f0;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 0.7rem;
            font-weight: 600;
            color: #475569;
        }

        .badge-kategori {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 0.7rem;
            font-weight: 500;
        }

        .kategori-layanan { background: #dcfce7; color: #1a5f4b; }
        .kategori-arsip { background: #fef3c7; color: #d97706; }
        .kategori-saran { background: #dbeafe; color: #2563eb; }
        .kategori-keluhan { background: #fee2e2; color: #dc2626; }
        .kategori-informasi { background: #e0e7ff; color: #4f46e5; }
        .kategori-lainnya { background: #f1f5f9; color: #64748b; }

        .status {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 4px 14px;
            border-radius: 30px;
            font-size: 0.7rem;
            font-weight: 600;
        }

        .status-baru { background: #fee2e2; color: #dc2626; }
        .status-dibaca { background: #fef3c7; color: #d97706; }
        .status-diproses { background: #dbeafe; color: #2563eb; }
        .status-selesai { background: #dcfce7; color: #059669; }
        .status-ditolak { background: #f1f5f9; color: #64748b; }

        .card-body {
            padding: 22px 25px;
        }

        .card-body h4 {
            font-size: 1rem;
            font-weight: 700;
            color: #1e293b;
            margin-bottom: 12px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .card-body p {
            color: #475569;
            line-height: 1.6;
            font-size: 0.9rem;
            margin-bottom: 16px;
        }

        .lampiran-link {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background: #f8fafc;
            padding: 6px 14px;
            border-radius: 30px;
            font-size: 0.75rem;
            color: #1a5f4b;
            text-decoration: none;
            margin-bottom: 18px;
            transition: 0.2s;
        }

        .lampiran-link:hover {
            background: #1a5f4b;
            color: white;
        }

        .card-footer {
            padding: 15px 25px;
            background: #fafbfc;
            border-top: 1px solid #f0f2f5;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }

        .info-meta {
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
            font-size: 0.75rem;
            color: #64748b;
        }

        .info-meta i {
            width: 16px;
            margin-right: 4px;
        }

        .action-form {
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .action-form select {
            padding: 8px 16px;
            border: 1px solid #e2e8f0;
            border-radius: 30px;
            font-size: 0.8rem;
            background: white;
            cursor: pointer;
        }

        .btn-update {
            background: linear-gradient(135deg, #1a5f4b, #0f3b2c);
            color: white;
            border: none;
            padding: 8px 20px;
            border-radius: 30px;
            cursor: pointer;
            font-size: 0.75rem;
            font-weight: 600;
            transition: 0.2s;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }

        .btn-update:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(26,95,75,0.3);
        }

        .empty-state {
            background: white;
            border-radius: 20px;
            text-align: center;
            padding: 60px;
        }

        .empty-state i {
            font-size: 60px;
            color: #cbd5e1;
            margin-bottom: 15px;
        }

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

        @media (max-width: 768px) {
            .stats-grid {
                grid-template-columns: 1fr;
            }
            
            .card-header, .card-footer {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .action-form {
                width: 100%;
                flex-direction: column;
            }
            
            .action-form select, .btn-update {
                width: 100%;
            }
            
            .section-title {
                flex-direction: column;
                align-items: stretch;
            }
            
            .search-box {
                min-width: 100%;
            }
            
            .filter-wrap {
                flex-direction: column;
                align-items: stretch;
                padding: 15px;
            }
            
            .filter-buttons {
                justify-content: center;
            }
            
            .filter-status select {
                width: 100%;
            }
            
            .title-left {
                justify-content: space-between;
            }
        }

        @media (max-width: 480px) {
            .filter-buttons {
                gap: 5px;
            }
            
            .filter-btn {
                padding: 6px 12px;
                font-size: 0.65rem;
            }
            
            .result-info {
                flex-wrap: wrap;
            }
            
            .result-info #resultCount {
                margin-left: 0;
            }
        }
    </style>
</head>
<body>

<!-- SIDEBAR MODERN PREMIUM -->
<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">
        <a href="index.php" class="nav-item">
            <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>
        <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>
        <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>
        <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 foto</small></div>
        </a>
        <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>
        <a href="pengaduan_admin.php" class="nav-item active">
            <div class="nav-icon"><i class="fas fa-comments"></i></div>
            <div class="nav-text"><span>Pengaduan</span><small>Kelola pengaduan</small></div>
        </a>
        <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">
        <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="welcome">
            <h1>Manajemen <span>Pengaduan</span></h1>
            <p>Kelola dan tindak lanjuti pengaduan dari masyarakat</p>
        </div>
        <div class="admin-info">
            <i class="fas fa-user-circle"></i>
            <div class="admin-name">Admin</div>
        </div>
    </div>

    <?php
    $total = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT COUNT(*) as total FROM pengaduan"))['total'];
    $baru = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT COUNT(*) as total FROM pengaduan WHERE status='baru'"))['total'];
    $diproses = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT COUNT(*) as total FROM pengaduan WHERE status='diproses'"))['total'];
    $selesai = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT COUNT(*) as total FROM pengaduan WHERE status='selesai'"))['total'];
    ?>
    
    <!-- STATISTIK GRID -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-info">
                <h3>TOTAL PENGADUAN</h3>
                <div class="number"><?php echo $total; ?></div>
            </div>
            <div class="stat-icon">
                <i class="fas fa-envelope"></i>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-info">
                <h3>BELUM DIPROSES</h3>
                <div class="number"><?php echo $baru; ?></div>
            </div>
            <div class="stat-icon">
                <i class="fas fa-clock"></i>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-info">
                <h3>SEDANG DIPROSES</h3>
                <div class="number"><?php echo $diproses; ?></div>
            </div>
            <div class="stat-icon">
                <i class="fas fa-spinner"></i>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-info">
                <h3>SELESAI</h3>
                <div class="number"><?php echo $selesai; ?></div>
            </div>
            <div class="stat-icon">
                <i class="fas fa-check-circle"></i>
            </div>
        </div>
    </div>

    <!-- DAFTAR PENGADUAN -->
    <div class="section-title">
        <div class="title-left">
            <h2><i class="fas fa-list"></i> Semua Pengaduan</h2>
            <span class="total-count"><?php echo mysqli_num_rows($query); ?> Pengaduan</span>
        </div>
        <div class="search-box">
            <i class="fas fa-search search-icon"></i>
            <input type="text" id="searchInput" placeholder="Cari pengaduan..." onkeyup="filterPengaduan()">
            <button class="btn-filter" onclick="resetFilter()">
                <i class="fas fa-times"></i>
            </button>
        </div>
    </div>

    <!-- FILTER KATEGORI & STATUS -->
    <div class="filter-wrap">
        <div class="filter-buttons">
            <button class="filter-btn active" onclick="filterByCategory('semua')">
                <i class="fas fa-th-list"></i> Semua
            </button>
            <button class="filter-btn" onclick="filterByCategory('layanan')">
                <i class="fas fa-book"></i> Layanan
            </button>
            <button class="filter-btn" onclick="filterByCategory('saran')">
                <i class="fas fa-lightbulb"></i> Saran
            </button>
            <button class="filter-btn" onclick="filterByCategory('keluhan')">
                <i class="fas fa-exclamation-triangle"></i> Keluhan
            </button>
            <button class="filter-btn" onclick="filterByCategory('informasi')">
                <i class="fas fa-info-circle"></i> Informasi
            </button>
        </div>
        <div class="filter-status">
            <select id="statusFilter" onchange="filterByStatus()">
                <option value="semua">Semua Status</option>
                <option value="baru">📋 Baru</option>
                <option value="dibaca">👁️ Dibaca</option>
                <option value="diproses">⚙️ Diproses</option>
                <option value="selesai">✅ Selesai</option>
                <option value="ditolak">❌ Ditolak</option>
            </select>
        </div>
    </div>

    <!-- HASIL PENCARIAN -->
    <div id="searchResult">
        <div class="result-info">
            <i class="fas fa-search"></i>
            Menampilkan hasil pencarian untuk: 
            <strong id="searchKeyword"></strong>
            <span id="resultCount"></span>
        </div>
    </div>

    <!-- DAFTAR PENGADUAN CARD -->
    <div class="pengaduan-list" id="pengaduanList">
        <?php if(mysqli_num_rows($query) > 0): ?>
            <?php while($row = mysqli_fetch_array($query)): 
                $statusClass = '';
                $statusIcon = '';
                switch($row['status']) {
                    case 'baru': $statusClass = 'status-baru'; $statusIcon = 'fa-exclamation-circle'; break;
                    case 'dibaca': $statusClass = 'status-dibaca'; $statusIcon = 'fa-eye'; break;
                    case 'diproses': $statusClass = 'status-diproses'; $statusIcon = 'fa-cogs'; break;
                    case 'selesai': $statusClass = 'status-selesai'; $statusIcon = 'fa-check-circle'; break;
                    default: $statusClass = 'status-ditolak'; $statusIcon = 'fa-times-circle';
                }
                
                $kategoriClass = '';
                $kategoriIcon = '';
                switch($row['kategori']) {
                    case 'layanan': $kategoriClass = 'kategori-layanan'; $kategoriIcon = 'fa-book'; break;
                    case 'arsip': $kategoriClass = 'kategori-arsip'; $kategoriIcon = 'fa-archive'; break;
                    case 'saran': $kategoriClass = 'kategori-saran'; $kategoriIcon = 'fa-lightbulb'; break;
                    case 'keluhan': $kategoriClass = 'kategori-keluhan'; $kategoriIcon = 'fa-exclamation-triangle'; break;
                    case 'informasi': $kategoriClass = 'kategori-informasi'; $kategoriIcon = 'fa-info-circle'; break;
                    default: $kategoriClass = 'kategori-lainnya'; $kategoriIcon = 'fa-tag';
                }
            ?>
            <div class="pengaduan-card" data-aos="fade-up" data-aos-delay="50" 
                 data-kategori="<?php echo $row['kategori']; ?>" 
                 data-status="<?php echo $row['status']; ?>">
                <div class="card-header">
                    <div style="display: flex; gap: 10px; flex-wrap: wrap;">
                        <span class="badge-id"><i class="fas fa-hashtag"></i> <?php echo str_pad($row['id'], 4, '0', STR_PAD_LEFT); ?></span>
                        <span class="badge-kategori <?php echo $kategoriClass; ?>">
                            <i class="fas <?php echo $kategoriIcon; ?>"></i> <?php echo ucfirst($row['kategori']); ?>
                        </span>
                        <span class="status <?php echo $statusClass; ?>">
                            <i class="fas <?php echo $statusIcon; ?>"></i> <?php echo strtoupper($row['status']); ?>
                        </span>
                    </div>
                    <div class="card-date">
                        <i class="far fa-calendar-alt"></i> <?php echo date('d F Y, H:i', strtotime($row['created_at'])); ?>
                    </div>
                </div>
                <div class="card-body">
                    <h4>
                        <i class="fas fa-user-circle" style="color: #1a5f4b;"></i>
                        <?php echo htmlspecialchars($row['nama']); ?>
                    </h4>
                    <h4 style="font-size: 0.95rem;">
                        <i class="fas fa-heading" style="color: #f5c542;"></i>
                        <?php echo htmlspecialchars($row['judul']); ?>
                    </h4>
                    <p><?php echo nl2br(htmlspecialchars($row['pesan'])); ?></p>
                    
                    <?php if(!empty($row['lampiran']) && file_exists("../uploads/pengaduan/".$row['lampiran'])): ?>
                        <a href="../uploads/pengaduan/<?php echo $row['lampiran']; ?>" target="_blank" class="lampiran-link">
                            <i class="fas fa-paperclip"></i> Lihat Lampiran
                        </a>
                    <?php endif; ?>
                </div>
            
                <div class="card-footer">
    <div class="info-meta">
        <span><i class="fas fa-envelope"></i> <?php echo htmlspecialchars($row['email']); ?></span>
        <span><i class="fas fa-phone-alt"></i> <?php echo $row['no_hp'] ?: 'Tidak diisi'; ?></span>
    </div>
    <div style="display: flex; gap: 10px; align-items: center; flex-wrap: wrap;">
        <form method="POST" class="action-form" style="display: inline-flex;">
            <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
            <select name="status">
                <option value="baru" <?php echo $row['status']=='baru' ? 'selected' : ''; ?>>📋 Baru</option>
                <option value="dibaca" <?php echo $row['status']=='dibaca' ? 'selected' : ''; ?>>👁️ Dibaca</option>
                <option value="diproses" <?php echo $row['status']=='diproses' ? 'selected' : ''; ?>>⚙️ Diproses</option>
                <option value="selesai" <?php echo $row['status']=='selesai' ? 'selected' : ''; ?>>✅ Selesai</option>
                <option value="ditolak" <?php echo $row['status']=='ditolak' ? 'selected' : ''; ?>>❌ Ditolak</option>
            </select>
            <button type="submit" name="update_status" class="btn-update">
                <i class="fas fa-save"></i> Update
            </button>
        </form>
        <!-- TAMBAHKAN TOMBOL HAPUS DI SINI -->
        <a href="?hapus=<?php echo $row['id']; ?>" 
           onclick="return confirm('Yakin ingin menghapus pengaduan ini?')" 
           style="background: #ef4444; color: white; padding: 8px 16px; border-radius: 30px; text-decoration: none; font-size: 0.75rem; font-weight: 600; display: inline-flex; align-items: center; gap: 6px;">
            <i class="fas fa-trash"></i> Hapus
        </a>
    </div>
</div>
            </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="empty-state">
                <i class="fas fa-inbox"></i>
                <h3>Belum Ada Pengaduan</h3>
                <p style="color: #64748b; margin-top: 10px;">Belum ada pengaduan dari masyarakat yang masuk ke sistem.</p>
            </div>
        <?php endif; ?>
    </div>

    

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

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

    // ====== FILTER & SEARCH ======
    let currentCategory = 'semua';
    let currentStatus = 'semua';

    function filterPengaduan() {
        const input = document.getElementById('searchInput');
        const keyword = input.value.toLowerCase().trim();
        const cards = document.querySelectorAll('.pengaduan-card');
        const searchResult = document.getElementById('searchResult');
        const searchKeyword = document.getElementById('searchKeyword');
        const resultCount = document.getElementById('resultCount');
        
        let visibleCount = 0;
        
        cards.forEach(card => {
            const text = card.innerText.toLowerCase();
            const category = card.dataset.kategori || '';
            const status = card.dataset.status || '';
            
            const matchCategory = currentCategory === 'semua' || category === currentCategory;
            const matchStatus = currentStatus === 'semua' || status === currentStatus;
            const matchSearch = keyword === '' || text.includes(keyword);
            
            if (matchCategory && matchStatus && matchSearch) {
                card.style.display = 'block';
                visibleCount++;
            } else {
                card.style.display = 'none';
            }
        });
        
        // Update search result info
        if (keyword !== '') {
            searchResult.style.display = 'block';
            searchKeyword.textContent = '"' + keyword + '"';
            resultCount.textContent = visibleCount + ' hasil ditemukan';
            
            // Show reset button
            document.querySelector('.btn-filter').classList.add('show');
        } else {
            searchResult.style.display = 'none';
            document.querySelector('.btn-filter').classList.remove('show');
        }
        
        // Check if no results
        const noResult = document.querySelector('.no-result');
        if (visibleCount === 0 && cards.length > 0) {
            if (!noResult) {
                const container = document.getElementById('pengaduanList');
                const empty = document.createElement('div');
                empty.className = 'no-result';
                empty.id = 'noResult';
                empty.innerHTML = `
                    <i class="fas fa-search"></i>
                    <h3>Tidak Ada Hasil</h3>
                    <p>Tidak ada pengaduan yang sesuai dengan kriteria pencarian.</p>
                `;
                container.appendChild(empty);
            }
        } else {
            const noResultEl = document.getElementById('noResult');
            if (noResultEl) noResultEl.remove();
        }
    }

    function filterByCategory(category) {
        currentCategory = category;
        
        // Update active button
        document.querySelectorAll('.filter-btn').forEach(btn => {
            btn.classList.remove('active');
        });
        document.querySelectorAll('.filter-btn').forEach(btn => {
            if (btn.textContent.toLowerCase().includes(category) || 
                (category === 'semua' && btn.textContent.includes('Semua'))) {
                btn.classList.add('active');
            }
        });
        
        filterPengaduan();
    }

    function filterByStatus() {
        const select = document.getElementById('statusFilter');
        currentStatus = select.value;
        filterPengaduan();
    }

    function resetFilter() {
        document.getElementById('searchInput').value = '';
        document.querySelector('.btn-filter').classList.remove('show');
        filterPengaduan();
    }
</script>
</body>
</html>