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

// ========== HIT COUNTER PENGUNJUNG ==========
$ip = $_SERVER['REMOTE_ADDR'];
$tanggal = date('Y-m-d');

mysqli_query($koneksi, "
    CREATE TABLE IF NOT EXISTS visitor_counter (
        id INT AUTO_INCREMENT PRIMARY KEY,
        ip_address VARCHAR(100),
        tanggal DATE,
        user_agent VARCHAR(255)
    )
");

$cek = mysqli_query($koneksi, "
    SELECT * FROM visitor_counter 
    WHERE ip_address = '$ip' AND tanggal = '$tanggal'
");

if(mysqli_num_rows($cek) == 0){
    mysqli_query($koneksi, "
        INSERT INTO visitor_counter (ip_address, tanggal) 
        VALUES ('$ip', '$tanggal')
    ");
}

$totalVisitor = mysqli_fetch_assoc(mysqli_query($koneksi, "
    SELECT COUNT(DISTINCT ip_address) as total FROM visitor_counter
"))['total'];

// ========== UPDATE VIEWS VIDEO ==========
// Jika ada parameter id_video, update views
if(isset($_GET['play']) && !empty($_GET['play'])) {
    $video_id = mysqli_real_escape_string($koneksi, $_GET['play']);
    mysqli_query($koneksi, "UPDATE arsip_video SET views = views + 1 WHERE id = '$video_id'");
}

// Ambil data video dari database
$query_video = mysqli_query($koneksi, "SELECT * FROM arsip_video ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title>Galeri Video - DPAD Solok Selatan</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <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: #f0f7f2;
            color: #1e293b;
            padding-top: 80px;
        }

        /* ========== HEADER ========== */
        .header {
            background: linear-gradient(135deg, #0a3d32 0%, #1a5f4b 50%, #0f3b2c 100%);
            padding: 0;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 9999;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }

        .header.scrolled {
            padding: 5px 0;
            box-shadow: 0 4px 25px rgba(0,0,0,0.15);
        }

        .header-content {
            max-width: 1400px;
            margin: 0 auto;
            padding: 12px 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }

        .logo-area {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .logo-img {
            width: 60px;
            height: 60px;
            border-radius: 14px;
            background: rgba(255,255,255,0.12);
            padding: 8px;
            border: 2px solid rgba(255,255,255,0.12);
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-img:hover {
            border-color: #f5c542;
            transform: scale(1.05);
        }

        .logo-img img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }

        .logo-img i {
            font-size: 28px;
            color: #f5c542;
        }

        .logo-text h1 {
            font-size: 1.3rem;
            font-weight: 800;
            color: white;
            font-family: 'Playfair Display', serif;
            line-height: 1.2;
        }

        .logo-text p {
            color: rgba(255,255,255,0.7);
            font-size: 0.7rem;
        }

        .header-menu {
            display: flex;
            align-items: center;
            gap: 8px;
            flex-wrap: wrap;
        }

        .menu-item {
            position: relative;
            display: inline-flex;
            align-items: center;
            justify-content: center;
        }

        .menu-icon {
            width: 44px;
            height: 44px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(255,255,255,0.08);
            color: white;
            font-size: 16px;
            border: 1px solid rgba(255,255,255,0.06);
            transition: all 0.3s ease;
            text-decoration: none;
            cursor: pointer;
        }

        .menu-icon:hover {
            background: white;
            color: #1a5f4b;
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(0,0,0,0.15);
        }

        .menu-item .tooltip {
            position: absolute;
            bottom: -38px;
            left: 50%;
            transform: translateX(-50%) scale(0.9);
            background: rgba(0,0,0,0.85);
            backdrop-filter: blur(10px);
            color: white;
            padding: 4px 14px;
            border-radius: 20px;
            font-size: 0.6rem;
            font-weight: 600;
            white-space: nowrap;
            opacity: 0;
            visibility: hidden;
            transition: all 0.25s ease;
            pointer-events: none;
            border: 1px solid rgba(255,255,255,0.05);
        }

        .menu-item:hover .tooltip {
            opacity: 1;
            visibility: visible;
            transform: translateX(-50%) scale(1);
            bottom: -42px;
        }

        /* ========== HERO ========== */
        .hero-page {
            background: linear-gradient(135deg, #0f3b2c, #1a5f4b);
            margin: 20px 40px 30px;
            border-radius: 30px;
            padding: 55px 50px;
            color: white;
            position: relative;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }

        .hero-page::before {
            content: "🎬";
            position: absolute;
            right: -20px;
            top: -20px;
            font-size: 200px;
            opacity: 0.06;
        }

        .hero-page::after {
            content: "📹";
            position: absolute;
            left: -20px;
            bottom: -20px;
            font-size: 200px;
            opacity: 0.06;
        }

        .hero-page .hero-badge {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background: rgba(245,197,66,0.2);
            padding: 6px 18px;
            border-radius: 50px;
            font-size: 0.75rem;
            font-weight: 600;
            color: #f5c542;
            margin-bottom: 15px;
        }

        .hero-page h1 {
            font-size: 2.5rem;
            font-weight: 800;
            font-family: 'Playfair Display', serif;
            position: relative;
            z-index: 2;
            margin-bottom: 8px;
        }

        .hero-page h1 span {
            color: #f5c542;
        }

        .hero-page p {
            font-size: 1.05rem;
            opacity: 0.9;
            max-width: 600px;
            position: relative;
            z-index: 2;
            line-height: 1.7;
        }

        /* ========== BREADCRUMB ========== */
        .breadcrumb-wrap {
            max-width: 1400px;
            margin: -8px auto 0;
            padding: 0 40px 25px;
        }

        .breadcrumb {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 0.8rem;
            color: #94a3b8;
            flex-wrap: wrap;
        }

        .breadcrumb a {
            color: #1a5f4b;
            text-decoration: none;
            font-weight: 500;
            transition: 0.3s;
        }

        .breadcrumb a:hover {
            color: #f5c542;
        }

        .breadcrumb .sep {
            color: #cbd5e1;
        }

        .breadcrumb .active {
            color: #1a5f4b;
            font-weight: 600;
        }

        /* ========== MAIN CONTENT ========== */
        .main-content {
            max-width: 1400px;
            margin: 0 auto;
            padding: 0 40px 50px;
        }

        .section-header {
            text-align: center;
            margin-bottom: 45px;
        }

        .section-header h2 {
            font-size: 1.8rem;
            font-weight: 700;
            color: #0f3b2c;
            position: relative;
            display: inline-block;
            padding-bottom: 12px;
        }

        .section-header h2::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 60px;
            height: 3px;
            background: #f5c542;
            border-radius: 3px;
        }

        .section-header p {
            color: #64748b;
            font-size: 0.95rem;
            margin-top: 10px;
        }

        /* ========== STATISTIK VIDEO ========== */
        .stats-video {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 20px;
            margin-bottom: 40px;
        }

        .stat-video-card {
            background: white;
            border-radius: 16px;
            padding: 20px;
            text-align: center;
            border: 1px solid #e8edf2;
            box-shadow: 0 4px 15px rgba(0,0,0,0.04);
            transition: all 0.3s ease;
        }

        .stat-video-card:hover {
            transform: translateY(-4px);
            border-color: #1a5f4b;
            box-shadow: 0 8px 25px rgba(0,0,0,0.08);
        }

        .stat-video-card .stat-icon {
            font-size: 28px;
            color: #1a5f4b;
            margin-bottom: 8px;
        }

        .stat-video-card .stat-number {
            font-size: 1.6rem;
            font-weight: 800;
            color: #0f3b2c;
        }

        .stat-video-card .stat-label {
            font-size: 0.8rem;
            color: #64748b;
        }

        /* ========== VIDEO GRID ========== */
        .video-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 25px;
        }

        .video-card {
            background: white;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 4px 15px rgba(0,0,0,0.06);
            border: 1px solid #e8edf2;
            transition: all 0.4s ease;
        }

        .video-card:hover {
            transform: translateY(-10px);
            border-color: #1a5f4b;
            box-shadow: 0 20px 40px rgba(0,0,0,0.12);
        }

        .video-wrapper {
            position: relative;
            width: 100%;
            padding-top: 56.25%;
            background: #0a0a0a;
            overflow: hidden;
            cursor: pointer;
        }

        .video-wrapper video {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        /* ===== PLAY OVERLAY ===== */
        .play-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.4);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
            z-index: 5;
        }

        .play-overlay:hover {
            background: rgba(0,0,0,0.2);
        }

        .play-overlay .play-circle {
            width: 80px;
            height: 80px;
            background: rgba(245,197,66,0.9);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
            box-shadow: 0 8px 30px rgba(245,197,66,0.3);
        }

        .play-overlay:hover .play-circle {
            transform: scale(1.1);
            background: #f5c542;
            box-shadow: 0 12px 40px rgba(245,197,66,0.5);
        }

        .play-overlay .play-circle i {
            font-size: 35px;
            color: #0f3b2c;
            margin-left: 5px;
        }

        .play-overlay .play-text {
            color: white;
            font-size: 0.85rem;
            font-weight: 500;
            margin-top: 15px;
            letter-spacing: 0.5px;
            opacity: 0.8;
        }

        /* ===== VIDEO BADGE ===== */
        .video-badge {
            position: absolute;
            top: 15px;
            right: 15px;
            background: rgba(0,0,0,0.7);
            backdrop-filter: blur(5px);
            padding: 5px 14px;
            border-radius: 30px;
            font-size: 0.7rem;
            color: white;
            display: flex;
            align-items: center;
            gap: 6px;
            z-index: 10;
        }

        .video-badge i {
            color: #f5c542;
        }

        .video-badge .duration {
            color: rgba(255,255,255,0.7);
        }

        /* ===== VIDEO CONTROLS ===== */
        .video-controls {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 12px 18px;
            background: linear-gradient(transparent, rgba(0,0,0,0.85));
            display: flex;
            align-items: center;
            gap: 12px;
            opacity: 0;
            transition: all 0.3s ease;
            z-index: 20;
        }

        .video-wrapper:hover .video-controls {
            opacity: 1;
        }

        .video-controls .ctrl-btn {
            background: none;
            border: none;
            color: white;
            font-size: 18px;
            cursor: pointer;
            padding: 5px 8px;
            transition: 0.2s;
            border-radius: 6px;
        }

        .video-controls .ctrl-btn:hover {
            color: #f5c542;
            background: rgba(255,255,255,0.1);
        }

        .video-controls .progress-bar {
            flex: 1;
            height: 4px;
            background: rgba(255,255,255,0.3);
            border-radius: 2px;
            cursor: pointer;
            position: relative;
        }

        .video-controls .progress-bar .progress {
            height: 100%;
            background: linear-gradient(90deg, #f5c542, #eab308);
            border-radius: 2px;
            width: 0%;
            transition: width 0.1s ease;
        }

        .video-controls .time-display {
            color: white;
            font-size: 12px;
            min-width: 80px;
            text-align: center;
            font-weight: 500;
            letter-spacing: 0.3px;
        }

        /* ===== VIDEO INFO ===== */
        .video-info {
            padding: 22px;
        }

        .video-info h3 {
            font-size: 1.1rem;
            font-weight: 700;
            color: #0f3b2c;
            margin-bottom: 8px;
            line-height: 1.4;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .video-info .video-meta {
            display: flex;
            gap: 18px;
            font-size: 0.8rem;
            color: #64748b;
            margin-bottom: 10px;
            flex-wrap: wrap;
        }

        .video-info .video-meta i {
            margin-right: 5px;
            color: #1a5f4b;
        }

        .video-info .video-meta .views-count {
            color: #1a5f4b;
            font-weight: 600;
        }

        .video-info .video-desc {
            font-size: 0.85rem;
            color: #64748b;
            line-height: 1.6;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        /* ===== EMPTY STATE ===== */
        .empty-state {
            text-align: center;
            padding: 80px 40px;
            background: white;
            border-radius: 24px;
            grid-column: 1/-1;
            border: 1px solid #e8edf2;
        }

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

        .empty-state h3 {
            color: #1e293b;
            margin-bottom: 10px;
            font-size: 1.2rem;
        }

        .empty-state p {
            color: #64748b;
        }

        .empty-state .btn-admin {
            display: inline-block;
            margin-top: 20px;
            background: #1a5f4b;
            color: white;
            padding: 10px 28px;
            border-radius: 40px;
            text-decoration: none;
            font-weight: 500;
            transition: 0.3s;
        }

        .empty-state .btn-admin:hover {
            background: #0f3b2c;
            transform: translateY(-2px);
        }

        /* ========== FOOTER ========== */
        .footer-premium {
            background: linear-gradient(135deg, #0a3d32, #0f3b2c);
            color: white;
            padding-top: 50px;
            margin-top: 30px;
            position: relative;
            overflow: hidden;
        }

        .footer-premium::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, #f5c542, #eab308, #f5c542);
        }

        .footer-container {
            max-width: 1280px;
            margin: 0 auto;
            padding: 0 20px;
        }

        .footer-grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 35px;
            margin-bottom: 40px;
        }

        .footer-col h3 {
            font-size: 1.1rem;
            font-weight: 700;
            margin-bottom: 20px;
            padding-bottom: 10px;
            position: relative;
            color: #f5c542;
        }

        .footer-col h3::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 40px;
            height: 2px;
            background: #f5c542;
        }

        .footer-logo {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 15px;
        }

        .footer-logo .logo-icon {
            width: 50px;
            height: 50px;
            background: rgba(255,255,255,0.08);
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .footer-logo .logo-icon i {
            font-size: 28px;
            color: #f5c542;
        }

        .footer-logo .logo-text h3 {
            font-size: 1.1rem;
            font-weight: 700;
            color: white;
            margin-bottom: 2px;
        }

        .footer-logo .logo-text p {
            font-size: 0.7rem;
            opacity: 0.7;
        }

        .footer-description {
            color: #cbd5e1;
            line-height: 1.6;
            font-size: 0.85rem;
            margin-bottom: 15px;
        }

        .footer-menu {
            list-style: none;
        }

        .footer-menu li {
            margin-bottom: 10px;
        }

        .footer-menu li a {
            color: #cbd5e1;
            text-decoration: none;
            transition: all 0.3s;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            font-size: 0.85rem;
        }

        .footer-menu li a i {
            color: #f5c542;
            width: 20px;
        }

        .footer-menu li a:hover {
            color: #f5c542;
            transform: translateX(8px);
        }

        .social-links {
            display: flex;
            gap: 10px;
            margin-top: 15px;
        }

        .social-icon {
            width: 38px;
            height: 38px;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            color: white;
            transition: all 0.3s;
            text-decoration: none;
            font-size: 0.9rem;
        }

        .social-fb { background: #1877f2; }
        .social-ig { background: #e4405f; }
        .social-yt { background: #ff0000; }
        .social-tt { background: #010101; }

        .social-icon:hover {
            transform: translateY(-4px);
            filter: brightness(1.1);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }

        .footer-contact-item {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 12px;
            color: #cbd5e1;
            font-size: 0.85rem;
        }

        .footer-contact-item i {
            width: 35px;
            height: 35px;
            background: rgba(245,197,66,0.1);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #f5c542;
        }

        .footer-bottom {
            border-top: 1px solid rgba(255,255,255,0.06);
            padding-top: 25px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }

        .footer-bottom p {
            color: rgba(255,255,255,0.5);
            font-size: 0.75rem;
        }

        .footer-bottom a {
            color: #f5c542;
            text-decoration: none;
        }

        .visitor-badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            background: rgba(255,255,255,0.06);
            padding: 5px 14px;
            border-radius: 30px;
            font-size: 0.7rem;
            color: #f5c542;
        }

        .dev-credit {
            text-align: center;
            padding-top: 20px;
            margin-top: 20px;
            border-top: 1px solid rgba(255,255,255,0.04);
            color: #94a3b8;
            font-size: 0.75rem;
        }

        .dev-credit a {
            color: #f5c542;
            text-decoration: none;
            font-weight: 500;
        }

        /* ========== RESPONSIVE ========== */
        @media (max-width: 1024px) {
            .video-grid {
                grid-template-columns: repeat(2, 1fr);
                gap: 20px;
            }
            .stats-video {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        @media (max-width: 992px) {
            .footer-grid {
                grid-template-columns: repeat(2, 1fr);
                gap: 30px;
            }
        }

        @media (max-width: 768px) {
            body {
                padding-top: 70px;
            }

            .header-content {
                padding: 8px 15px;
            }
            .logo-img {
                width: 40px;
                height: 40px;
                padding: 6px;
            }
            .logo-text h1 {
                font-size: 0.85rem;
            }
            .logo-text p {
                font-size: 0.6rem;
            }
            .menu-icon {
                width: 36px;
                height: 36px;
                font-size: 13px;
            }
            .header-menu {
                gap: 4px;
            }
            .menu-item .tooltip {
                display: none;
            }

            .hero-page {
                margin: 15px;
                padding: 30px 20px;
                border-radius: 20px;
            }
            .hero-page h1 {
                font-size: 1.8rem;
            }
            .hero-page p {
                font-size: 0.95rem;
            }
            .hero-page .hero-badge {
                font-size: 0.65rem;
                padding: 4px 14px;
            }

            .breadcrumb-wrap {
                padding: 0 15px 18px;
            }
            .breadcrumb {
                font-size: 0.7rem;
            }

            .main-content {
                padding: 0 15px 30px;
            }
            .section-header h2 {
                font-size: 1.5rem;
            }

            .stats-video {
                grid-template-columns: 1fr 1fr;
                gap: 12px;
            }
            .stat-video-card {
                padding: 15px;
            }
            .stat-video-card .stat-number {
                font-size: 1.3rem;
            }

            .video-grid {
                grid-template-columns: 1fr;
                gap: 20px;
            }

            .video-card {
                border-radius: 16px;
            }

            .play-overlay .play-circle {
                width: 60px;
                height: 60px;
            }

            .play-overlay .play-circle i {
                font-size: 25px;
            }

            .play-overlay .play-text {
                font-size: 0.7rem;
                margin-top: 10px;
            }

            .video-controls {
                padding: 8px 14px;
                gap: 10px;
                opacity: 1;
                background: linear-gradient(transparent, rgba(0,0,0,0.8));
            }

            .video-controls .ctrl-btn {
                font-size: 16px;
                padding: 3px 6px;
            }

            .video-controls .time-display {
                font-size: 10px;
                min-width: 60px;
            }

            .video-info {
                padding: 16px;
            }
            .video-info h3 {
                font-size: 1rem;
            }
            .video-info .video-meta {
                font-size: 0.75rem;
                gap: 12px;
            }
            .video-info .video-desc {
                font-size: 0.8rem;
            }

            .footer-grid {
                grid-template-columns: 1fr;
                gap: 25px;
                text-align: center;
            }
            .footer-col h3::after {
                left: 50%;
                transform: translateX(-50%);
            }
            .footer-logo {
                justify-content: center;
            }
            .footer-menu li a {
                justify-content: center;
            }
            .social-links {
                justify-content: center;
            }
            .footer-contact-item {
                justify-content: center;
            }
            .footer-bottom {
                flex-direction: column;
                text-align: center;
            }
        }

        @media (max-width: 480px) {
            .hero-page h1 {
                font-size: 1.4rem;
            }
            .hero-page p {
                font-size: 0.85rem;
            }
            .section-header h2 {
                font-size: 1.2rem;
            }
            .section-header p {
                font-size: 0.85rem;
            }

            .stats-video {
                grid-template-columns: 1fr 1fr;
                gap: 8px;
            }
            .stat-video-card {
                padding: 12px 10px;
                border-radius: 12px;
            }
            .stat-video-card .stat-number {
                font-size: 1.1rem;
            }
            .stat-video-card .stat-label {
                font-size: 0.7rem;
            }
            .stat-video-card .stat-icon {
                font-size: 22px;
            }

            .video-card {
                border-radius: 14px;
            }

            .play-overlay .play-circle {
                width: 50px;
                height: 50px;
            }

            .play-overlay .play-circle i {
                font-size: 20px;
            }

            .video-controls {
                padding: 6px 10px;
                gap: 8px;
            }

            .video-controls .ctrl-btn {
                font-size: 14px;
                padding: 2px 4px;
            }

            .video-controls .time-display {
                font-size: 9px;
                min-width: 50px;
            }

            .video-controls .progress-bar {
                height: 3px;
            }

            .video-info {
                padding: 14px;
            }
            .video-info h3 {
                font-size: 0.9rem;
            }
            .video-info .video-meta {
                font-size: 0.65rem;
                gap: 10px;
            }
            .video-info .video-desc {
                font-size: 0.75rem;
            }

            .video-badge {
                font-size: 0.6rem;
                padding: 3px 10px;
                top: 10px;
                right: 10px;
            }

            .footer-premium {
                padding-top: 30px;
                margin-top: 30px;
            }
            .footer-grid {
                gap: 20px;
            }
            .visitor-badge {
                font-size: 0.6rem;
                padding: 3px 8px;
            }
        }
    </style>
</head>
<body>

<!-- ========== HEADER ========== -->
<div class="header" id="mainHeader">
    <div class="header-content">
        <div class="logo-area">
            <div class="logo-img">
                <?php if(file_exists("img/logo.png")): ?>
                    <img src="img/logo.png" alt="Logo DPAD">
                <?php else: ?>
                    <i class="fas fa-archive"></i>
                <?php endif; ?>
            </div>
            <div class="logo-text">
                <h1>Dinas Arsip Solok Selatan</h1>
                <p>Dinas Arsip Daerah</p>
            </div>
        </div>

        <div class="header-menu">
            <div class="menu-item">
                <a href="dashboard.php" class="menu-icon">
                    <i class="fas fa-home"></i>
                </a>
                <span class="tooltip">Dashboard</span>
            </div>
            <div class="menu-item">
                <a href="layanan_arsip.php" class="menu-icon">
                    <i class="fas fa-concierge-bell"></i>
                </a>
                <span class="tooltip">Layanan</span>
            </div>
            <div class="menu-item">
                <a href="berita.php" class="menu-icon">
                    <i class="fas fa-newspaper"></i>
                </a>
                <span class="tooltip">Berita</span>
            </div>
            <div class="menu-item">
                <a href="sejarah_poto.php" class="menu-icon">
                    <i class="fas fa-images"></i>
                </a>
                <span class="tooltip">Foto</span>
            </div>
        </div>
    </div>
</div>

<!-- ========== HERO ========== -->
<div class="hero-page" data-aos="fade-up">
    <div class="hero-badge">
        <i class="fas fa-star"></i> Galeri Video Arsip Solok Selatan
    </div>
    <h1>Koleksi <span>Video</span></h1>
    <p>Dokumentasi kegiatan dan perjalanan Dinas Perpustakaan dan Arsip Daerah Kabupaten Solok Selatan</p>
</div>

<!-- ========== BREADCRUMB ========== -->
<div class="breadcrumb-wrap">
    <div class="breadcrumb">
        <a href="das.php"><i class="fas fa-home"></i></a>
        <span class="sep">/</span>
        <a href="layanan.php">Layanan</a>
        <span class="sep">/</span>
        <span class="active">Video</span>
    </div>
</div>

<!-- ========== STATISTIK VIDEO ========== -->
<?php
// Hitung total video
$total_video = mysqli_num_rows($query_video);

// Hitung total views semua video
$total_views = 0;
$temp_query = mysqli_query($koneksi, "SELECT SUM(views) as total FROM arsip_video");
if($temp_query && mysqli_num_rows($temp_query) > 0) {
    $total_views = mysqli_fetch_assoc($temp_query)['total'] ?? 0;
}

// Hitung video terbaru (7 hari terakhir)
$video_baru = mysqli_query($koneksi, "SELECT COUNT(*) as total FROM arsip_video WHERE tanggal >= DATE_SUB(NOW(), INTERVAL 7 DAY)");
$video_baru_count = ($video_baru && mysqli_num_rows($video_baru) > 0) ? mysqli_fetch_assoc($video_baru)['total'] : 0;
?>

<div class="main-content">
    <div class="stats-video" data-aos="fade-up" data-aos-delay="50">
        <div class="stat-video-card">
            <div class="stat-icon"><i class="fas fa-video"></i></div>
            <div class="stat-number"><?php echo $total_video; ?></div>
            <div class="stat-label">Total Video</div>
        </div>
        <div class="stat-video-card">
            <div class="stat-icon"><i class="fas fa-eye"></i></div>
            <div class="stat-number"><?php echo number_format($total_views); ?></div>
            <div class="stat-label">Total Tontonan</div>
        </div>
        <div class="stat-video-card">
            <div class="stat-icon"><i class="fas fa-clock"></i></div>
            <div class="stat-number"><?php echo $video_baru_count; ?></div>
            <div class="stat-label">Video Baru (7 Hari)</div>
        </div>
    </div>
</div>

<!-- ========== MAIN CONTENT ========== -->
<div class="main-content">
    <div class="section-header" data-aos="fade-up">
        <h2>Galeri Video</h2>
        <p>Koleksi video dokumentasi kegiatan DPAD Solok Selatan</p>
    </div>

    <div class="video-grid">
        <?php if(mysqli_num_rows($query_video) > 0): ?>
            <?php 
            // Reset pointer query untuk looping
            mysqli_data_seek($query_video, 0);
            while($video = mysqli_fetch_array($query_video)): 
            ?>
            <div class="video-card" data-aos="fade-up" data-aos-delay="50">
                <div class="video-wrapper" onclick="playVideo(<?php echo $video['id']; ?>)">
                    <video id="video-<?php echo $video['id']; ?>" src="img/video/<?php echo $video['video']; ?>" preload="metadata"></video>
                    
                    <!-- Play Overlay -->
                    <div class="play-overlay" id="playOverlay-<?php echo $video['id']; ?>">
                        <div class="play-circle" id="playCircle-<?php echo $video['id']; ?>">
                            <i class="fas fa-play" id="playIcon-<?php echo $video['id']; ?>"></i>
                        </div>
                        <span class="play-text">Putar Video</span>
                    </div>

                    <!-- Badge -->
                    <div class="video-badge">
                        <i class="fas fa-hd"></i> HD
                        <span class="duration">• <?php echo $video['durasi'] ?? '00:00'; ?></span>
                    </div>

                    <!-- Video Controls -->
                    <div class="video-controls" id="controls-<?php echo $video['id']; ?>">
                        <button class="ctrl-btn" onclick="event.stopPropagation(); toggleVideo(<?php echo $video['id']; ?>)">
                            <i class="fas fa-play" id="ctrlPlay-<?php echo $video['id']; ?>"></i>
                        </button>
                        <div class="progress-bar" onclick="event.stopPropagation(); seekVideo(event, <?php echo $video['id']; ?>)">
                            <div class="progress" id="progress-<?php echo $video['id']; ?>"></div>
                        </div>
                        <span class="time-display" id="time-<?php echo $video['id']; ?>">0:00 / 0:00</span>
                    </div>
                </div>
                <div class="video-info">
                    <h3><?php echo htmlspecialchars($video['judul']); ?></h3>
                    <div class="video-meta">
                        <?php if(!empty($video['tanggal'])): ?>
                        <span><i class="fas fa-calendar-alt"></i> <?php echo date('d/m/Y', strtotime($video['tanggal'])); ?></span>
                        <?php else: ?>
                        <span><i class="fas fa-calendar-alt"></i> -</span>
                        <?php endif; ?>
                        <span class="views-count">
                            <i class="fas fa-eye"></i> <?php echo number_format($video['views'] ?? 0); ?> x ditonton
                        </span>
                    </div>
                    <p class="video-desc"><?php echo htmlspecialchars($video['deskripsi']); ?></p>
                </div>
            </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="empty-state" data-aos="fade-up">
                <i class="fas fa-video"></i>
                <h3>Belum Ada Video</h3>
                <p>Belum ada video yang diupload.</p>
                <?php if(isset($_SESSION['admin'])): ?>
                    <a href="admin/tambah_video.php" class="btn-admin">
                        <i class="fas fa-plus"></i> Upload Video
                    </a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<!-- ========== FOOTER ========== -->
<footer class="footer-premium">
    <div class="footer-container">
        <div class="footer-grid">
            <div class="footer-col" data-aos="fade-up">
                <div class="footer-logo">
                    <div class="logo-icon">
                        <?php if(file_exists("img/logo.png")): ?>
                            <img src="img/logo.png" style="width:35px; height:35px; object-fit:contain;">
                        <?php else: ?>
                            <i class="fas fa-archive"></i>
                        <?php endif; ?>
                    </div>
                    <div class="logo-text">
                        <h3>Dinas Arsip Solok Selatan</h3>
                        <p>Dinas Perpustakaan & Arsip Daerah</p>
                    </div>
                </div>
                <p class="footer-description">
                    Melestarikan sejarah, menjaga arsip, dan membangun peradaban literasi untuk masyarakat Solok Selatan yang cerdas dan berbudaya.
                </p>
                <div class="social-links">
                    <a href="https://www.facebook.com/profile.php?id=61573699960407" class="social-icon social-fb" target="_blank"><i class="fab fa-facebook-f"></i></a>
                    <a href="https://www.instagram.com/arsip_solsel/" class="social-icon social-ig" target="_blank"><i class="fab fa-instagram"></i></a>
                    <a href="https://www.youtube.com/@pustakadpksolsel" class="social-icon social-yt" target="_blank"><i class="fab fa-youtube"></i></a>
                    <a href="https://www.tiktok.com/@solselnews" class="social-icon social-tt" target="_blank"><i class="fab fa-tiktok"></i></a>
                </div>
            </div>

            <div class="footer-col" data-aos="fade-up" data-aos-delay="100">
                <h3><i class="fas fa-link"></i> Menu Cepat</h3>
                <ul class="footer-menu">
                    <li><a href="dashboard.php"><i class="fas fa-home"></i> Beranda</a></li>
                    <li><a href="profil_istansi.php"><i class="fas fa-building"></i> Profil</a></li>
                    <li><a href="visi.php"><i class="fas fa-eye"></i> Visi & Misi</a></li>
                    <li><a href="struktur.php"><i class="fas fa-diagram-project"></i> Struktur Organisasi</a></li>
                    <li><a href="berita.php"><i class="fas fa-newspaper"></i> Berita</a></li>
                </ul>
            </div>

            <div class="footer-col" data-aos="fade-up" data-aos-delay="200">
                <h3><i class="fas fa-star"></i> Layanan</h3>
                <ul class="footer-menu">
                    <li><a href="https://perpustakaan.solselkab.go.id/read/166/bidang-kearsipan"><i class="fas fa-book"></i> Perpustakaan</a></li>
                    <li><a href="layanan_arsip.php"><i class="fas fa-archive"></i> Kearsipan</a></li>
                    <li><a href="layanan_kunjungan.php"><i class="fas fa-users"></i> Kunjungan Edukasi</a></li>
                    <li><a href="layanan_konsultasi.php"><i class="fas fa-comments"></i> Konsultasi Arsip</a></li>
                    <li><a href="pengaduan.php"><i class="fas fa-headset"></i> Pengaduan Masyarakat</a></li>
                </ul>
            </div>

            <div class="footer-col" data-aos="fade-up" data-aos-delay="300">
                <h3><i class="fas fa-map-marker-alt"></i> Lokasi & Kontak</h3>
                <div class="footer-contact-item">
                    <i class="fas fa-map-marker-alt"></i>
                    <span>Jl. Lubuk Gadang - Simpang Tigo Rimbo Tangah, Sangir</span>
                </div>
                <div class="footer-contact-item">
                    <i class="fas fa-envelope"></i>
                    <span>pustaka.arsipsolsel@gmail.com</span>
                </div>
                <div class="footer-contact-item">
                    <i class="fas fa-clock"></i>
                    <span>Senin - Jumat, 08.00 - 16.00 WIB</span>
                </div>
            </div>
        </div>

        <div class="footer-bottom">
            <div>
                <p>&copy; <?php echo date('Y'); ?> Dinas Perpustakaan dan Arsip Daerah Kabupaten Solok Selatan</p>
                <p style="margin-top:4px; font-size:0.65rem; opacity:0.5;">All Rights Reserved</p>
            </div>
            <div>
                <p style="font-size:0.7rem; color:#94a3b8;">Sistem Informasi Perpustakaan dan Arsip Daerah</p>
                <div class="visitor-badge">
                    <i class="fas fa-eye"></i> <?php echo number_format($totalVisitor); ?> Pengunjung
                </div>
                <div class="dev-credit" style="margin-top:8px;">
                    Developed by <a href="https://www.pnp.ac.id/download/psdku-solok-selatan/" target="_blank">PSDKU Solok Selatan</a>
                </div>
            </div>
        </div>
    </div>
</footer>

<!-- ========== SCRIPTS ========== -->
<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
<script>
    AOS.init({
        duration: 600,
        once: true,
        offset: 30,
        easing: 'ease-in-out'
    });

    // Header Sticky
    const header = document.getElementById('mainHeader');
    window.addEventListener('scroll', function() {
        if (window.scrollY > 50) {
            header.classList.add('scrolled');
        } else {
            header.classList.remove('scrolled');
        }
    });

    // ========== VIDEO PLAYER ==========
    let videoPlaying = null;

    function playVideo(id) {
        // Update views via AJAX
        updateViews(id);
        
        // Play video
        toggleVideo(id);
    }

    function toggleVideo(id) {
        var video = document.getElementById('video-' + id);
        var playIcon = document.getElementById('playIcon-' + id);
        var ctrlPlay = document.getElementById('ctrlPlay-' + id);
        var playOverlay = document.getElementById('playOverlay-' + id);
        
        if (video.paused) {
            // Pause video lain yang sedang diputar
            if (videoPlaying && videoPlaying !== video) {
                videoPlaying.pause();
                var oldId = videoPlaying.id.replace('video-', '');
                var oldPlayIcon = document.getElementById('playIcon-' + oldId);
                var oldCtrlPlay = document.getElementById('ctrlPlay-' + oldId);
                var oldPlayOverlay = document.getElementById('playOverlay-' + oldId);
                if (oldPlayIcon) oldPlayIcon.className = 'fas fa-play';
                if (oldCtrlPlay) oldCtrlPlay.className = 'fas fa-play';
                if (oldPlayOverlay) {
                    oldPlayOverlay.style.opacity = '1';
                    oldPlayOverlay.style.pointerEvents = 'auto';
                }
            }
            
            video.play();
            videoPlaying = video;
            
            if (playIcon) {
                playIcon.className = 'fas fa-pause';
            }
            if (ctrlPlay) {
                ctrlPlay.className = 'fas fa-pause';
            }
            if (playOverlay) {
                playOverlay.style.opacity = '0';
                playOverlay.style.pointerEvents = 'none';
            }
        } else {
            video.pause();
            videoPlaying = null;
            if (playIcon) {
                playIcon.className = 'fas fa-play';
            }
            if (ctrlPlay) {
                ctrlPlay.className = 'fas fa-play';
            }
            if (playOverlay) {
                playOverlay.style.opacity = '1';
                playOverlay.style.pointerEvents = 'auto';
            }
        }
    }

    // ========== UPDATE VIEWS VIA AJAX ==========
    function updateViews(id) {
        fetch('update_video_views.php?play=' + id)
            .then(response => response.text())
            .then(data => {
                // Update tampilan views di card
                var viewsElement = document.querySelector('#video-' + id).closest('.video-card').querySelector('.views-count');
                if (viewsElement) {
                    var currentViews = parseInt(viewsElement.textContent.replace(/[^0-9]/g, ''));
                    if (!isNaN(currentViews)) {
                        viewsElement.innerHTML = '<i class="fas fa-eye"></i> ' + (currentViews + 1).toLocaleString() + ' x ditonton';
                    }
                }
            })
            .catch(error => console.error('Error updating views:', error));
    }

    // Update progress bar dan waktu
    document.querySelectorAll('video').forEach(function(video) {
        var id = video.id.replace('video-', '');
        var progress = document.getElementById('progress-' + id);
        var timeDisplay = document.getElementById('time-' + id);
        var playOverlay = document.getElementById('playOverlay-' + id);
        
        if (!progress || !timeDisplay) return;

        video.addEventListener('timeupdate', function() {
            if (video.duration) {
                var percent = (video.currentTime / video.duration) * 100;
                progress.style.width = percent + '%';
                
                var currentMin = Math.floor(video.currentTime / 60);
                var currentSec = Math.floor(video.currentTime % 60);
                var durMin = Math.floor(video.duration / 60);
                var durSec = Math.floor(video.duration % 60);
                
                if (currentSec < 10) currentSec = '0' + currentSec;
                if (durSec < 10) durSec = '0' + durSec;
                
                timeDisplay.textContent = currentMin + ':' + currentSec + ' / ' + durMin + ':' + durSec;
            }
        });

        video.addEventListener('ended', function() {
            var playIcon = document.getElementById('playIcon-' + id);
            var ctrlPlay = document.getElementById('ctrlPlay-' + id);
            if (playIcon) playIcon.className = 'fas fa-play';
            if (ctrlPlay) ctrlPlay.className = 'fas fa-play';
            if (progress) progress.style.width = '0%';
            if (playOverlay) {
                playOverlay.style.opacity = '1';
                playOverlay.style.pointerEvents = 'auto';
            }
            videoPlaying = null;
        });

        video.addEventListener('play', function() {
            var playIcon = document.getElementById('playIcon-' + id);
            var ctrlPlay = document.getElementById('ctrlPlay-' + id);
            if (playIcon) playIcon.className = 'fas fa-pause';
            if (ctrlPlay) ctrlPlay.className = 'fas fa-pause';
            if (playOverlay) {
                playOverlay.style.opacity = '0';
                playOverlay.style.pointerEvents = 'none';
            }
        });

        video.addEventListener('pause', function() {
            var playIcon = document.getElementById('playIcon-' + id);
            var ctrlPlay = document.getElementById('ctrlPlay-' + id);
            if (playIcon) playIcon.className = 'fas fa-play';
            if (ctrlPlay) ctrlPlay.className = 'fas fa-play';
            if (playOverlay) {
                playOverlay.style.opacity = '1';
                playOverlay.style.pointerEvents = 'auto';
            }
        });
    });

    // Fungsi Seek video (klik progress bar)
    function seekVideo(event, id) {
        var video = document.getElementById('video-' + id);
        if (!video || !video.duration) return;
        
        var progressBar = event.currentTarget;
        var rect = progressBar.getBoundingClientRect();
        var x = event.clientX - rect.left;
        var width = rect.width;
        var percent = x / width;
        video.currentTime = percent * video.duration;
    }

    // Tampilkan overlay saat video tidak diputar
    document.querySelectorAll('video').forEach(function(video) {
        var id = video.id.replace('video-', '');
        var playOverlay = document.getElementById('playOverlay-' + id);
        
        if (video.paused && playOverlay) {
            playOverlay.style.opacity = '1';
            playOverlay.style.pointerEvents = 'auto';
        }
    });
</script>

</body>
</html>