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

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

// Cek dan buat tabel jika belum ada
$cek_tabel = mysqli_query($koneksi, "SHOW TABLES LIKE 'arsip_foto'");
if(mysqli_num_rows($cek_tabel) == 0){
    mysqli_query($koneksi, "
        CREATE TABLE arsip_foto (
            id INT AUTO_INCREMENT PRIMARY KEY,
            judul VARCHAR(200) NOT NULL,
            deskripsi TEXT,
            tahun DATE,
            foto TEXT NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        )
    ");
}

// Proses Hapus
if(isset($_GET['hapus'])){
    $id = mysqli_real_escape_string($koneksi, $_GET['hapus']);
    
    $query = mysqli_query($koneksi, "SELECT foto FROM arsip_foto WHERE id='$id'");
    $data = mysqli_fetch_assoc($query);
    
    if($data){
        $fotoArray = explode(',', $data['foto']);
        foreach($fotoArray as $foto){
            $foto = trim($foto);
            if(!empty($foto) && file_exists("../img/arsip/".$foto)){
                unlink("../img/arsip/".$foto);
            }
        }
    }
    
    mysqli_query($koneksi, "DELETE FROM arsip_foto WHERE id='$id'");
    echo "<script>alert('✅ Data berhasil dihapus!'); window.location='arsip_poto.php';</script>";
    exit();
}

// Proses Tambah / Edit
if(isset($_POST['submit'])){
    $judul = mysqli_real_escape_string($koneksi, $_POST['judul']);
    $deskripsi = mysqli_real_escape_string($koneksi, $_POST['deskripsi']);
    $tahun = mysqli_real_escape_string($koneksi, $_POST['tahun']);
    $id = isset($_POST['id']) ? $_POST['id'] : '';
    
    $gambarArray = [];
    $uploadDir = "../img/arsip/";
    
    if(!file_exists($uploadDir)){
        mkdir($uploadDir, 0777, true);
    }
    
    if(!empty($_FILES['foto']['name'][0])){
        foreach($_FILES['foto']['name'] as $key => $name){
            if($_FILES['foto']['error'][$key] == 0){
                $ext = pathinfo($name, PATHINFO_EXTENSION);
                $namaFile = time() . '_' . $key . '_' . uniqid() . '.' . $ext;
                $tmpFile = $_FILES['foto']['tmp_name'][$key];
                
                if($_FILES['foto']['size'][$key] > 5 * 1024 * 1024){
                    continue;
                }
                
                if(move_uploaded_file($tmpFile, $uploadDir . $namaFile)){
                    $gambarArray[] = $namaFile;
                }
            }
        }
    }
    
    $gambarSimpan = !empty($gambarArray) ? implode(',', $gambarArray) : '';
    
    if($id){
        if($gambarSimpan != ''){
            $query = mysqli_query($koneksi, "SELECT foto FROM arsip_foto WHERE id='$id'");
            $data = mysqli_fetch_assoc($query);
            if($data && !empty($data['foto'])){
                $fotoLama = explode(',', $data['foto']);
                foreach($fotoLama as $fl){
                    $fl = trim($fl);
                    if(!empty($fl) && file_exists($uploadDir . $fl)){
                        unlink($uploadDir . $fl);
                    }
                }
            }
            mysqli_query($koneksi, "UPDATE arsip_foto SET judul='$judul', deskripsi='$deskripsi', tahun='$tahun', foto='$gambarSimpan' WHERE id='$id'");
        } else {
            mysqli_query($koneksi, "UPDATE arsip_foto SET judul='$judul', deskripsi='$deskripsi', tahun='$tahun' WHERE id='$id'");
        }
        echo "<script>alert('✅ Data berhasil diupdate!'); window.location='arsip_poto.php';</script>";
    } else {
        if(empty($gambarSimpan)){
            echo "<script>alert('⚠️ Gagal! Minimal upload 1 gambar.'); window.history.back();</script>";
            exit();
        }
        mysqli_query($koneksi, "INSERT INTO arsip_foto (judul, deskripsi, tahun, foto) VALUES ('$judul', '$deskripsi', '$tahun', '$gambarSimpan')");
        echo "<script>alert('✅ Data berhasil ditambahkan!'); window.location='arsip_poto.php';</script>";
    }
    exit();
}

$editData = null;
if(isset($_GET['edit'])){
    $id = mysqli_real_escape_string($koneksi, $_GET['edit']);
    $query = mysqli_query($koneksi, "SELECT * FROM arsip_foto WHERE id='$id'");
    $editData = mysqli_fetch_assoc($query);
}

$query = mysqli_query($koneksi, "SELECT * FROM arsip_foto 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">
    <title>Arsip Foto Sejarah | Admin DPAD</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&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: linear-gradient(135deg, #f0f4f8 0%, #e8edf2 100%);
            min-height: 100vh;
            padding-top: 80px;
        }
        
        /* ========== HEADER PREMIUM ========== */
        .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;
            text-decoration: none;
        }

        .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 h1 span {
            color: #f5c542;
        }

        .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-icon.active {
            background: #f5c542;
            color: #0a3d32;
        }

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

        /* Container */
        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 40px 20px;
        }
        
        /* Statistik Bar */
        .stats-bar {
            display: flex;
            gap: 20px;
            margin-bottom: 30px;
            flex-wrap: wrap;
        }
        .stat-card {
            background: white;
            border-radius: 20px;
            padding: 20px 25px;
            display: flex;
            align-items: center;
            gap: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            flex: 1;
            min-width: 160px;
            transition: 0.3s;
            border-bottom: 3px solid transparent;
        }
        .stat-card:hover {
            transform: translateY(-5px);
            border-bottom-color: #f5c542;
        }
        .stat-card i {
            font-size: 38px;
            color: #1a5f4b;
        }
        .stat-card .info h4 {
            font-size: 0.75rem;
            color: #64748b;
        }
        .stat-card .info .number {
            font-size: 1.5rem;
            font-weight: 800;
            color: #0f3b2c;
        }
        
        /* Card Form Premium */
        .form-card {
            background: white;
            border-radius: 28px;
            padding: 35px;
            margin-bottom: 40px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.08);
            border: 1px solid rgba(26,95,75,0.1);
        }
        .form-card h3 {
            margin-bottom: 25px;
            color: #0f3b2c;
            font-size: 1.3rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .form-group {
            margin-bottom: 25px;
        }
        .form-group label {
            display: block;
            font-weight: 600;
            margin-bottom: 8px;
            color: #1e293b;
        }
        .form-group label i {
            margin-right: 8px;
            color: #1a5f4b;
        }
        .form-group input, .form-group textarea {
            width: 100%;
            padding: 14px 18px;
            border: 2px solid #e2e8f0;
            border-radius: 14px;
            font-family: 'Inter', sans-serif;
            font-size: 0.9rem;
            transition: 0.3s;
            background: #fafbfc;
        }
        .form-group input:focus, .form-group textarea:focus {
            outline: none;
            border-color: #1a5f4b;
            box-shadow: 0 0 0 3px rgba(26,95,75,0.1);
            background: white;
        }
        
        /* ========== DATE INPUT PREMIUM ========== */
        .date-input {
            cursor: pointer;
        }
        .date-input::-webkit-calendar-picker-indicator {
            cursor: pointer;
            filter: invert(0.4);
            padding: 5px;
        }
        .help-text {
            font-size: 0.7rem;
            color: #64748b;
            margin-top: 5px;
            display: block;
        }
        .required {
            color: #ef4444;
        }
        
        .btn-submit {
            background: linear-gradient(135deg, #1a5f4b, #0f3b2c);
            color: white;
            border: none;
            padding: 14px 32px;
            border-radius: 50px;
            cursor: pointer;
            font-weight: 600;
            transition: all 0.3s;
            font-size: 1rem;
            display: inline-flex;
            align-items: center;
            gap: 10px;
        }
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(26,95,75,0.3);
        }
        
        /* Gallery Grid */
        .section-title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
        }
        .section-title h3 {
            font-size: 1.3rem;
            color: #0f3b2c;
        }
        .section-title span {
            background: #1a5f4b;
            color: white;
            padding: 5px 12px;
            border-radius: 30px;
            font-size: 0.8rem;
        }
        
        .gallery-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 30px;
        }
        .gallery-card {
            background: white;
            border-radius: 24px;
            overflow: hidden;
            box-shadow: 0 10px 25px rgba(0,0,0,0.08);
            transition: all 0.4s;
        }
        .gallery-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 35px rgba(0,0,0,0.15);
        }
        .gallery-card .img-container {
            height: 230px;
            overflow: hidden;
            position: relative;
        }
        .gallery-card .img-container img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: 0.5s;
        }
        .gallery-card:hover .img-container img {
            transform: scale(1.08);
        }
        .badge-multi {
            position: absolute;
            top: 12px;
            right: 12px;
            background: rgba(0,0,0,0.65);
            backdrop-filter: blur(4px);
            color: white;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 0.7rem;
            display: flex;
            align-items: center;
            gap: 5px;
        }
        .badge-date {
            position: absolute;
            bottom: 12px;
            left: 12px;
            background: rgba(0,0,0,0.55);
            backdrop-filter: blur(4px);
            color: white;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 0.65rem;
            display: flex;
            align-items: center;
            gap: 5px;
        }
        .gallery-card .info {
            padding: 20px;
        }
        .gallery-card .info h4 {
            font-size: 1.1rem;
            color: #0f3b2c;
            margin-bottom: 8px;
        }
        .gallery-card .info .tahun {
            color: #1a5f4b;
            font-size: 0.75rem;
            margin-bottom: 8px;
            display: inline-flex;
            align-items: center;
            gap: 5px;
            background: #dcfce7;
            padding: 3px 12px;
            border-radius: 30px;
        }
        .gallery-card .info p {
            color: #64748b;
            font-size: 0.85rem;
            margin-bottom: 15px;
            line-height: 1.5;
        }
        .btn-group {
            display: flex;
            gap: 10px;
            margin-top: 10px;
        }
        .btn-edit, .btn-delete {
            padding: 8px 18px;
            border-radius: 30px;
            text-decoration: none;
            font-size: 0.75rem;
            font-weight: 600;
            transition: 0.3s;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }
        .btn-edit {
            background: #f5c542;
            color: #0f3b2c;
        }
        .btn-edit:hover {
            background: #eab308;
            transform: translateY(-2px);
        }
        .btn-delete {
            background: #ef4444;
            color: white;
        }
        .btn-delete:hover {
            background: #dc2626;
            transform: translateY(-2px);
        }
        
        /* Preview Gambar */
        .preview-grid {
            display: flex;
            flex-wrap: wrap;
            gap: 12px;
            margin-top: 15px;
        }
        .preview-item {
            position: relative;
            width: 85px;
            height: 85px;
            border-radius: 12px;
            overflow: hidden;
        }
        .preview-item img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .remove-preview {
            position: absolute;
            top: -8px;
            right: -8px;
            background: #ef4444;
            color: white;
            border: none;
            width: 22px;
            height: 22px;
            border-radius: 50%;
            cursor: pointer;
            font-size: 12px;
        }
        .file-upload {
            border: 2px dashed #cbd5e1;
            border-radius: 20px;
            padding: 35px;
            text-align: center;
            cursor: pointer;
            background: #f8fafc;
            transition: 0.3s;
        }
        .file-upload:hover {
            border-color: #1a5f4b;
            background: #f0fdf4;
        }
        .file-upload i {
            font-size: 48px;
            color: #1a5f4b;
            margin-bottom: 10px;
        }
        input[type="file"] {
            display: none;
        }
        
        .info-empty {
            text-align: center;
            padding: 80px;
            background: white;
            border-radius: 30px;
            grid-column: 1/-1;
        }
        
        @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;
            }
            
            .container {
                padding: 20px;
            }
            .form-card {
                padding: 20px;
            }
            .stats-bar {
                flex-direction: column;
            }
            .gallery-grid {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 480px) {
            .header-content {
                padding: 6px 10px;
            }
            .logo-text h1 {
                font-size: 0.7rem;
            }
            .logo-text p {
                font-size: 0.5rem;
            }
            .logo-img {
                width: 32px;
                height: 32px;
                padding: 4px;
            }
            .logo-img i {
                font-size: 16px;
            }
            .menu-icon {
                width: 30px;
                height: 30px;
                font-size: 11px;
            }
            .header-menu {
                gap: 3px;
            }
        }
    </style>
</head>
<body>

<!-- ========== HEADER PREMIUM ========== -->
<div class="header" id="mainHeader">
    <div class="header-content">
        <a href="../dashboard.php" 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 <span>Solok Selatan</span></h1>
                <p>Dinas Arsip Daerah</p>
            </div>
        </a>

        <div class="header-menu">
            <div class="menu-item">
                <a href="index.php" class="menu-icon">
                    <i class="fas fa-home"></i>
                </a>
                <span class="tooltip">Dashboard</span>
            </div>
            <div class="menu-item">
                <a href="arsip_poto.php" class="menu-icon active">
                    <i class="fas fa-images"></i>
                </a>
                <span class="tooltip">Arsip Foto</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="index.php" class="menu-icon">
                    <i class="fas fa-sign-out-alt"></i>
                </a>
                <span class="tooltip">Logout</span>
            </div>
        </div>
    </div>
</div>

<div class="container">
    
    <!-- STATISTIK BAR -->
    <div class="stats-bar">
        <div class="stat-card">
            <i class="fas fa-images"></i>
            <div class="info">
                <h4>Total Arsip</h4>
                <div class="number"><?php echo mysqli_num_rows($query); ?></div>
            </div>
        </div>
        <div class="stat-card">
            <i class="fas fa-calendar-alt"></i>
            <div class="info">
                <h4>Upload Terakhir</h4>
                <div class="number"><?php echo date('d/m/Y'); ?></div>
            </div>
        </div>
        <div class="stat-card">
            <i class="fas fa-user-shield"></i>
            <div class="info">
                <h4>Admin Aktif</h4>
                <div class="number"><?php echo $_SESSION['nama_admin'] ?? 'Admin'; ?></div>
            </div>
        </div>
    </div>
    
    <!-- FORM TAMBAH/EDIT -->
    <div class="form-card" data-aos="fade-up">
        <h3>
            <?php if($editData): ?>
                <i class="fas fa-edit"></i> Edit Arsip Foto
            <?php else: ?>
                <i class="fas fa-plus-circle"></i> Tambah Arsip Foto Baru
            <?php endif; ?>
        </h3>
        <form method="POST" enctype="multipart/form-data" id="formArsip">
            <?php if($editData): ?>
                <input type="hidden" name="id" value="<?php echo $editData['id']; ?>">
            <?php endif; ?>
            
            <div class="form-group">
                <label><i class="fas fa-heading"></i> Judul Foto</label>
                <input type="text" name="judul" required value="<?php echo $editData['judul'] ?? ''; ?>" placeholder="Contoh: Peresmian Gedung Perpustakaan">
            </div>
            
            <div class="form-group">
                <label><i class="fas fa-align-left"></i> Deskripsi</label>
                <textarea name="deskripsi" rows="3" placeholder="Ceritakan tentang foto ini..."><?php echo $editData['deskripsi'] ?? ''; ?></textarea>
            </div>
            
            <!-- ========== TANGGAL PUBLIKASI DENGAN INPUT TYPE DATE ========== -->
            <div class="form-group">
                <label>
                    <i class="fas fa-calendar-alt"></i> 
                    Tanggal Publikasi <span class="required">*</span>
                </label>
                <input type="date" name="tahun" class="date-input" value="<?php echo $editData['tahun'] ?? ''; ?>" required>
                <small class="help-text">
                    <i class="fas fa-info-circle"></i> Pilih tanggal publikasi foto
                </small>
            </div>
            
            <div class="form-group">
                <label><i class="fas fa-images"></i> Upload Foto (bisa banyak sekaligus)</label>
                <div class="file-upload" onclick="document.getElementById('foto').click()">
                    <i class="fas fa-cloud-upload-alt"></i>
                    <p>Klik untuk upload gambar</p>
                    <p style="font-size: 12px; margin-top: 5px;">Format: JPG, PNG, JPEG (Max 5MB per gambar)</p>
                    <p style="font-size: 11px; color: #f5c542; margin-top: 8px;">💡 Tekan CTRL/CMD untuk pilih banyak gambar</p>
                </div>
                <input type="file" name="foto[]" id="foto" accept="image/*" multiple onchange="previewImages()">
                <div class="preview-grid" id="previewGrid"></div>
                <?php if($editData && !empty($editData['foto'])): 
                    $fotoLamaArray = explode(',', $editData['foto']);
                    $jmlFotoLama = count($fotoLamaArray);
                ?>
                    <p style="margin-top: 10px; font-size: 12px; color: #64748b;">
                        <i class="fas fa-info-circle"></i> Saat ini ada <?php echo $jmlFotoLama; ?> foto. Upload gambar baru akan mengganti semua foto lama.
                    </p>
                <?php endif; ?>
            </div>
            
            <button type="submit" name="submit" class="btn-submit">
                <i class="fas fa-save"></i> <?php echo $editData ? 'Update Arsip' : 'Simpan Arsip'; ?>
            </button>
            
            <?php if($editData): ?>
                <a href="arsip_poto.php" style="margin-left: 15px; text-decoration: none; color: #64748b;">
                    <i class="fas fa-times"></i> Batal
                </a>
            <?php endif; ?>
        </form>
    </div>
    
    <!-- DAFTAR ARSIP FOTO -->
    <div class="section-title">
        <h3><i class="fas fa-folder-open"></i> Daftar Arsip Foto</h3>
        <span><?php echo mysqli_num_rows($query); ?> Arsip</span>
    </div>
    
    <div class="gallery-grid">
        <?php if(mysqli_num_rows($query) > 0): ?>
            <?php while($row = mysqli_fetch_array($query)): 
                $fotoArray = explode(',', $row['foto']);
                $fotoArray = array_map('trim', $fotoArray);
                $fotoArray = array_filter($fotoArray);
                $fotoPertama = reset($fotoArray);
                $jumlahFoto = count($fotoArray);
                $tanggalUpload = date('d/m/Y', strtotime($row['created_at']));
                $tanggalPublikasi = !empty($row['tahun']) ? date('d/m/Y', strtotime($row['tahun'])) : 'Tidak diketahui';
            ?>
            <div class="gallery-card" data-aos="fade-up" data-aos-delay="50">
                <div class="img-container">
                    <?php if($fotoPertama && file_exists("../img/arsip/".$fotoPertama)): ?>
                        <img src="../img/arsip/<?php echo $fotoPertama; ?>" alt="<?php echo htmlspecialchars($row['judul']); ?>">
                    <?php else: ?>
                        <img src="https://placehold.co/400x230/1a5f4b/white?text=No+Image" alt="No Image">
                    <?php endif; ?>
                    <?php if($jumlahFoto > 1): ?>
                        <div class="badge-multi">
                            <i class="fas fa-images"></i> <?php echo $jumlahFoto; ?> foto
                        </div>
                    <?php endif; ?>
                    <div class="badge-date">
                        <i class="fas fa-clock"></i> <?php echo $tanggalUpload; ?>
                    </div>
                </div>
                <div class="info">
                    <h4><?php echo htmlspecialchars($row['judul']); ?></h4>
                    <div class="tahun">
                        <i class="fas fa-calendar"></i> <?php echo $tanggalPublikasi; ?>
                    </div>
                    <p><?php echo substr(htmlspecialchars($row['deskripsi']), 0, 100); ?>...</p>
                    <div class="btn-group">
                        <a href="?edit=<?php echo $row['id']; ?>" class="btn-edit">
                            <i class="fas fa-edit"></i> Edit
                        </a>
                        <a href="?hapus=<?php echo $row['id']; ?>" class="btn-delete" onclick="return confirm('Yakin ingin menghapus arsip ini?')">
                            <i class="fas fa-trash"></i> Hapus
                        </a>
                    </div>
                </div>
            </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="info-empty" data-aos="fade-up">
                <i class="fas fa-images" style="font-size: 70px; color: #cbd5e1;"></i>
                <h3 style="margin-top: 15px;">Belum Ada Arsip Foto</h3>
                <p style="margin-top: 10px;">Silakan tambah foto pertama menggunakan form di atas!</p>
            </div>
        <?php endif; ?>
    </div>
</div>

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

    // Header Sticky
    const header = document.getElementById('mainHeader');
    window.addEventListener('scroll', function() {
        if (window.scrollY > 50) {
            header.classList.add('scrolled');
        } else {
            header.classList.remove('scrolled');
        }
    });
    
    let selectedFiles = [];
    
    function previewImages() {
        const input = document.getElementById('foto');
        const previewGrid = document.getElementById('previewGrid');
        selectedFiles = Array.from(input.files);
        
        if(selectedFiles.length > 0){
            let html = '';
            selectedFiles.forEach((file, index) => {
                const url = URL.createObjectURL(file);
                html += `
                    <div class="preview-item">
                        <img src="${url}" alt="Preview">
                        <button type="button" class="remove-preview" onclick="removeImage(${index})">×</button>
                    </div>
                `;
            });
            previewGrid.innerHTML = html;
        } else {
            previewGrid.innerHTML = '';
        }
    }
    
    function removeImage(index){
        selectedFiles.splice(index, 1);
        const input = document.getElementById('foto');
        const dt = new DataTransfer();
        selectedFiles.forEach(file => dt.items.add(file));
        input.files = dt.files;
        previewImages();
    }
</script>
</body>
</html>