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

$success_message = '';
$error_message = '';

if(isset($_POST['submit'])){

    $judul = mysqli_real_escape_string($koneksi, $_POST['judul']);
    $tanggal = mysqli_real_escape_string($koneksi, $_POST['tanggal']);
    $isi = mysqli_real_escape_string($koneksi, $_POST['isi']);

    // Proses upload multiple gambar
    $gambarArray = [];
    $uploadDir = "../img/";
    
    // Buat folder jika belum ada
    if(!file_exists($uploadDir)){
        mkdir($uploadDir, 0777, true);
    }
    
    // Looping upload file (bisa banyak)
    if(!empty($_FILES['gambar']['name'][0])){
        foreach($_FILES['gambar']['name'] as $key => $name){
            if($_FILES['gambar']['error'][$key] == 0){
                $ext = pathinfo($name, PATHINFO_EXTENSION);
                $namaFile = time() . '_' . $key . '_' . uniqid() . '.' . $ext;
                $tmpFile = $_FILES['gambar']['tmp_name'][$key];
                
                if($_FILES['gambar']['size'][$key] > 5 * 1024 * 1024){
                    continue;
                }
                
                $allowedTypes = ['image/jpeg', 'image/png', 'image/jpg', 'image/webp'];
                if(!in_array($_FILES['gambar']['type'][$key], $allowedTypes)){
                    continue;
                }
                
                if(move_uploaded_file($tmpFile, $uploadDir . $namaFile)){
                    $gambarArray[] = $namaFile;
                }
            }
        }
    }
    
    $gambarSimpan = !empty($gambarArray) ? implode(',', $gambarArray) : '';

    $query = "INSERT INTO berita (id, judul, tanggal, isi, gambar) 
              VALUES ('', '$judul', '$tanggal', '$isi', '$gambarSimpan')";
    
    if(mysqli_query($koneksi, $query)){
        $success_message = "✅ Berita berhasil ditambah dengan " . count($gambarArray) . " gambar!";
        // Reset form setelah sukses
        $judul = $tanggal = $isi = '';
        $_POST = array();
    } else {
        $error_message = "❌ Gagal menambah berita: " . mysqli_error($koneksi);
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
    <title>Tambah Berita | DPAD Solok Selatan</title>
    
    <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">
    <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">
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: #f0f7f2;
            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;
        }

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

        /* ========== CONTAINER ========== */
        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 0 40px 50px;
        }

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

        .breadcrumb {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 0.8rem;
            color: #94a3b8;
            flex-wrap: wrap;
            background: white;
            padding: 12px 20px;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.04);
        }

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

        /* ========== TOMBOL KEMBALI ========== */
        .btn-back-wrap {
            max-width: 1000px;
            margin: 15px auto 0;
            padding: 0 40px;
        }

        .btn-back {
            display: inline-flex;
            align-items: center;
            gap: 10px;
            background: white;
            color: #1a5f4b;
            padding: 10px 22px;
            border-radius: 12px;
            text-decoration: none;
            font-weight: 600;
            font-size: 0.9rem;
            transition: all 0.3s ease;
            box-shadow: 0 2px 10px rgba(0,0,0,0.04);
            border: 1px solid #e8edf2;
        }

        .btn-back:hover {
            background: #1a5f4b;
            color: white;
            transform: translateX(-5px);
            box-shadow: 0 5px 20px rgba(26,95,75,0.2);
            border-color: #1a5f4b;
        }

        /* ========== ALERT ========== */
        .alert {
            padding: 14px 18px;
            border-radius: 12px;
            margin-bottom: 20px;
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 12px;
            animation: slideDown 0.5s ease;
        }

        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .alert-success {
            background: #f0fdf4;
            border-left: 4px solid #22c55e;
            color: #065f46;
        }

        .alert-success i {
            color: #22c55e;
            font-size: 1.4rem;
        }

        .alert-error {
            background: #fef2f2;
            border-left: 4px solid #ef4444;
            color: #991b1b;
        }

        .alert-error i {
            color: #ef4444;
            font-size: 1.4rem;
        }

        .alert .alert-close {
            margin-left: auto;
            background: none;
            border: none;
            font-size: 1.2rem;
            cursor: pointer;
            color: inherit;
            opacity: 0.6;
            transition: 0.3s;
            padding: 0 4px;
        }

        .alert .alert-close:hover {
            opacity: 1;
        }

        /* ========== FORM CARD ========== */
        .form-card {
            background: white;
            border-radius: 24px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.06);
            overflow: hidden;
            border: 1px solid #e8edf2;
            margin-top: 20px;
        }

        .card-header {
            background: linear-gradient(135deg, #0a3d32, #1a5f4b);
            padding: 30px 35px;
            color: white;
            position: relative;
        }

        .card-header::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, #f5c542, #eab308, #f5c542);
        }

        .card-header .icon-box {
            width: 50px;
            height: 50px;
            background: rgba(255,255,255,0.12);
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: #f5c542;
        }

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

        .card-header h2 {
            font-size: 1.5rem;
            font-weight: 700;
            font-family: 'Playfair Display', serif;
        }

        .card-header p {
            margin-top: 6px;
            opacity: 0.85;
            font-size: 0.9rem;
            padding-left: 65px;
        }

        .card-body {
            padding: 35px;
        }

        /* ========== ALERT INFO ========== */
        .alert-info {
            background: #f0fdf4;
            border-left: 4px solid #1a5f4b;
            padding: 14px 18px;
            border-radius: 12px;
            margin-bottom: 22px;
            font-size: 0.85rem;
            color: #065f46;
            display: flex;
            align-items: center;
            gap: 12px;
        }

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

        .alert-info .required {
            color: #ef4444;
            font-weight: 700;
        }

        /* ========== INFO MULTIPLE ========== */
        .info-multiple {
            background: #eff6ff;
            border: 1px solid #bfdbfe;
            padding: 12px 16px;
            border-radius: 10px;
            margin-bottom: 20px;
            font-size: 0.8rem;
            color: #1e40af;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .info-multiple i {
            font-size: 1.2rem;
        }

        .info-multiple kbd {
            background: #fff;
            padding: 2px 10px;
            border-radius: 4px;
            font-size: 0.65rem;
            font-weight: 700;
            color: #1e293b;
            border: 1px solid #d1d9e6;
        }

        /* ========== FORM GROUP ========== */
        .form-group {
            margin-bottom: 24px;
        }

        .form-group label {
            display: flex;
            align-items: center;
            gap: 10px;
            font-weight: 600;
            color: #1e293b;
            margin-bottom: 8px;
            font-size: 0.9rem;
        }

        .form-group label i {
            color: #1a5f4b;
            width: 20px;
            font-size: 1rem;
        }

        .form-group label .required {
            color: #ef4444;
            margin-left: 2px;
        }

        .form-group .hint {
            font-size: 0.7rem;
            color: #94a3b8;
            margin-top: 6px;
        }

        input, textarea {
            width: 100%;
            padding: 14px 18px;
            border: 2px solid #e8edf2;
            border-radius: 12px;
            font-family: 'Inter', sans-serif;
            font-size: 0.95rem;
            transition: all 0.3s ease;
            background: #fafbfc;
            color: #1e293b;
        }

        input:focus, textarea:focus {
            outline: none;
            border-color: #1a5f4b;
            background: #fff;
            box-shadow: 0 0 0 4px rgba(26,95,75,0.08);
        }

        input::placeholder, textarea::placeholder {
            color: #b0b8c5;
        }

        textarea {
            resize: vertical;
            min-height: 150px;
        }

        /* ========== FILE UPLOAD ========== */
        .file-upload {
            border: 2px dashed #d9dfe8;
            border-radius: 12px;
            padding: 35px 20px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            background: #fafbfc;
        }

        .file-upload:hover {
            border-color: #1a5f4b;
            background: #f0fdf6;
        }

        .file-upload i {
            font-size: 3.5rem;
            color: #1a5f4b;
            display: block;
            margin-bottom: 10px;
        }

        .file-upload p {
            color: #64748b;
            font-size: 0.9rem;
        }

        .file-upload .file-count {
            margin-top: 12px;
            font-size: 0.85rem;
            color: #1a5f4b;
            font-weight: 600;
        }

        input[type="file"] {
            display: none;
        }

        /* ========== PREVIEW GALLERY ========== */
        .gallery-preview {
            margin-top: 20px;
            display: none;
        }

        .gallery-preview.active {
            display: block;
            animation: fadeUp 0.4s ease;
        }

        @keyframes fadeUp {
            from { opacity: 0; transform: translateY(15px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .gallery-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 18px;
            background: #f1f5f9;
            border-radius: 12px 12px 0 0;
            font-size: 0.85rem;
            font-weight: 600;
            color: #1e293b;
        }

        .gallery-header .count {
            color: #1a5f4b;
        }

        .clear-all {
            background: none;
            border: none;
            color: #ef4444;
            cursor: pointer;
            font-size: 0.7rem;
            padding: 4px 14px;
            border-radius: 20px;
            transition: 0.2s;
            display: flex;
            align-items: center;
            gap: 6px;
            font-weight: 600;
        }

        .clear-all:hover {
            background: #fee2e2;
        }

        .preview-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            gap: 16px;
            padding: 18px;
            background: #f8fafc;
            border-radius: 0 0 12px 12px;
            max-height: 400px;
            overflow-y: auto;
            border: 2px solid #e8edf2;
            border-top: none;
        }

        .preview-grid::-webkit-scrollbar {
            width: 6px;
        }

        .preview-grid::-webkit-scrollbar-track {
            background: #f1f5f9;
            border-radius: 10px;
        }

        .preview-grid::-webkit-scrollbar-thumb {
            background: #cbd5e1;
            border-radius: 10px;
        }

        .preview-item {
            position: relative;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
            transition: all 0.3s ease;
            background: #fff;
            border: 1px solid #e8edf2;
        }

        .preview-item:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 25px rgba(0,0,0,0.12);
        }

        .preview-item img {
            width: 100%;
            height: 120px;
            object-fit: cover;
        }

        .preview-item .info {
            padding: 8px 10px;
            background: #fff;
            text-align: center;
        }

        .preview-item .info .name {
            font-size: 0.65rem;
            color: #1e293b;
            font-weight: 500;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .preview-item .info .size {
            font-size: 0.55rem;
            color: #94a3b8;
        }

        .remove-img {
            position: absolute;
            top: 6px;
            right: 6px;
            background: rgba(239,68,68,0.9);
            color: #fff;
            border: none;
            width: 26px;
            height: 26px;
            border-radius: 50%;
            cursor: pointer;
            font-size: 11px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: 0.2s;
            z-index: 10;
        }

        .remove-img:hover {
            background: #dc2626;
            transform: scale(1.1);
        }

        .preview-item .index {
            position: absolute;
            bottom: 36px;
            left: 6px;
            background: rgba(0,0,0,0.6);
            color: #fff;
            font-size: 9px;
            padding: 2px 10px;
            border-radius: 12px;
            font-weight: 600;
        }

        /* ========== TOMBOL SUBMIT ========== */
        .btn-submit {
            width: 100%;
            background: linear-gradient(135deg, #0a3d32, #1a5f4b);
            color: white;
            border: none;
            padding: 16px 24px;
            border-radius: 12px;
            font-size: 1rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
        }

        .btn-submit:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 30px rgba(10,61,50,0.3);
        }

        .btn-submit i {
            font-size: 1.2rem;
        }

        /* ========== BUTTON TAMBAH LAGI ========== */
        .btn-group-actions {
            display: flex;
            gap: 12px;
            margin-top: 15px;
            flex-wrap: wrap;
        }

        .btn-tambah-lagi {
            flex: 1;
            background: #e8edf2;
            color: #1a5f4b;
            border: none;
            padding: 14px 20px;
            border-radius: 12px;
            font-size: 0.95rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            text-decoration: none;
        }

        .btn-tambah-lagi:hover {
            background: #1a5f4b;
            color: white;
        }

        .btn-lihat-berita {
            flex: 1;
            background: #f5c542;
            color: #0a3d32;
            border: none;
            padding: 14px 20px;
            border-radius: 12px;
            font-size: 0.95rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            text-decoration: none;
        }

        .btn-lihat-berita:hover {
            background: #eab308;
            transform: translateY(-2px);
        }

        /* ========== FOOTER ========== */
        .footer-premium {
            background: linear-gradient(135deg, #0a3d32, #0f3b2c);
            color: rgba(255,255,255,0.7);
            text-align: center;
            padding: 30px 20px;
            margin-top: 40px;
        }

        .footer-premium p:last-child {
            font-size: 0.7rem;
            margin-top: 5px;
            opacity: 0.6;
        }

        /* ========== RESPONSIVE ========== */
        @media (max-width: 992px) {
            .container {
                padding: 0 30px 40px;
            }
            .breadcrumb-wrap {
                padding: 0 30px;
            }
            .btn-back-wrap {
                padding: 0 30px;
            }
            .card-body {
                padding: 28px;
            }
        }

        @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: 0 15px 30px;
            }
            .breadcrumb-wrap {
                padding: 0 15px;
            }
            .btn-back-wrap {
                padding: 0 15px;
            }

            .breadcrumb {
                font-size: 0.7rem;
                padding: 10px 15px;
            }

            .btn-back {
                padding: 8px 16px;
                font-size: 0.8rem;
            }

            .card-header {
                padding: 20px 24px;
            }

            .card-header .header-title h2 {
                font-size: 1.2rem;
            }

            .card-header .icon-box {
                width: 40px;
                height: 40px;
                font-size: 18px;
            }

            .card-header p {
                padding-left: 0;
                font-size: 0.8rem;
            }

            .card-body {
                padding: 20px;
            }

            .preview-grid {
                grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
                padding: 12px;
            }

            .preview-item img {
                height: 80px;
            }

            .file-upload {
                padding: 24px 12px;
            }

            .file-upload i {
                font-size: 2.5rem;
            }

            .info-multiple {
                flex-wrap: wrap;
                font-size: 0.7rem;
            }

            .alert-info {
                font-size: 0.8rem;
                flex-wrap: wrap;
            }

            .btn-submit {
                padding: 14px 20px;
                font-size: 0.9rem;
            }

            .btn-group-actions {
                flex-direction: column;
            }
        }

        @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;
            }
            .menu-icon {
                width: 30px;
                height: 30px;
                font-size: 11px;
            }
            .header-menu {
                gap: 3px;
            }

            .container {
                padding: 0 10px 25px;
            }
            .breadcrumb-wrap {
                padding: 0 10px;
            }
            .btn-back-wrap {
                padding: 0 10px;
            }

            .card-header {
                padding: 16px 18px;
            }

            .card-header .header-title h2 {
                font-size: 1rem;
            }

            .card-header .icon-box {
                width: 35px;
                height: 35px;
                font-size: 16px;
            }

            .card-body {
                padding: 16px;
            }

            input, textarea {
                padding: 10px 14px;
                font-size: 0.85rem;
            }

            .preview-grid {
                grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
                gap: 10px;
            }

            .preview-item img {
                height: 65px;
            }

            .gallery-header {
                flex-wrap: wrap;
                gap: 6px;
                padding: 10px 12px;
                font-size: 0.7rem;
            }

            .btn-submit {
                padding: 12px 16px;
                font-size: 0.85rem;
            }

            .btn-back {
                padding: 6px 12px;
                font-size: 0.75rem;
            }
            .btn-back span {
                display: none;
            }
        }
    </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="index.php" class="menu-icon">
                    <i class="fas fa-home"></i>
                </a>
                <span class="tooltip">Dashboard</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">
        
                
            </div>
        </div>
    </div>
</div>
<!-- ========== CONTAINER ========== -->
<div class="container">
    
    <!-- ========== ALERT SUCCESS / ERROR ========== -->
    <?php if(!empty($success_message)): ?>
        <div class="alert alert-success" id="alertSuccess">
            <i class="fas fa-check-circle"></i>
            <span><?php echo $success_message; ?></span>
            <button class="alert-close" onclick="document.getElementById('alertSuccess').remove()">
                <i class="fas fa-times"></i>
            </button>
        </div>
    <?php endif; ?>

    <?php if(!empty($error_message)): ?>
        <div class="alert alert-error" id="alertError">
            <i class="fas fa-exclamation-circle"></i>
            <span><?php echo $error_message; ?></span>
            <button class="alert-close" onclick="document.getElementById('alertError').remove()">
                <i class="fas fa-times"></i>
            </button>
        </div>
    <?php endif; ?>

    <!-- ========== FORM CARD ========== -->
    <div class="form-card">
        <div class="card-header">
            <div class="header-title">
                <div class="icon-box"><i class="fas fa-newspaper"></i></div>
                <div>
                    <h2>Tambah Berita Baru</h2>
                    <p>Lengkapi form di bawah untuk menambahkan berita / pengumuman terbaru</p>
                </div>
            </div>
        </div>
<div class="breadcrumb-wrap">
    <div class="breadcrumb">
        <a href=".php"><i class="fas fa-home"></i></a>
        <span class="sep">/</span>
        <span class="active">Tambah Berita</span>
    </div>
</div>
        <div class="card-body">
            
            <!-- Alert Info -->
            <div class="alert-info">
                <i class="fas fa-info-circle"></i> 
                Field dengan tanda <span class="required">*</span> wajib diisi.
            </div>

            <!-- Info Multiple Upload -->
            <div class="info-multiple">
                <i class="fas fa-images"></i>
                <span>
                    <strong>Bisa upload banyak gambar!</strong> Tekan <kbd>CTRL</kbd> (Windows) / <kbd>CMD</kbd> (Mac) sambil klik untuk pilih beberapa gambar sekaligus.
                </span>
            </div>

            <!-- ========== FORM ========== -->
            <form method="POST" enctype="multipart/form-data" id="formBerita">
                
                <!-- Judul Berita -->
                <div class="form-group">
                    <label>
                        <i class="fas fa-heading"></i> 
                        Judul Berita <span class="required">*</span>
                    </label>
                    <input type="text" name="judul" id="judul" placeholder="Masukkan judul berita..." value="<?php echo isset($_POST['judul']) ? htmlspecialchars($_POST['judul']) : ''; ?>" required>
                </div>

                <!-- Tanggal -->
                <div class="form-group">
                    <label>
                        <i class="fas fa-calendar-alt"></i> 
                        Tanggal Publikasi <span class="required">*</span>
                    </label>
                    <input type="date" name="tanggal" id="tanggal" value="<?php echo isset($_POST['tanggal']) ? $_POST['tanggal'] : date('Y-m-d'); ?>" required>
                </div>

                <!-- Isi Berita -->
                <div class="form-group">
                    <label>
                        <i class="fas fa-align-left"></i> 
                        Isi Berita <span class="required">*</span>
                    </label>
                    <textarea name="isi" id="isi" rows="6" placeholder="Tulis isi berita di sini..."><?php echo isset($_POST['isi']) ? htmlspecialchars($_POST['isi']) : ''; ?></textarea>
                </div>

                <!-- Upload Multiple Gambar -->
                <div class="form-group">
                    <label>
                        <i class="fas fa-images"></i> 
                        Galeri Gambar (Bisa pilih banyak) <span class="required">*</span>
                    </label>
                    <div class="file-upload" onclick="document.getElementById('gambar').click()">
                        <i class="fas fa-cloud-upload-alt"></i>
                        <p>Klik untuk upload gambar (bisa pilih banyak sekaligus)</p>
                        <p style="font-size: 12px; color: #94a3b8;">Format: JPG, PNG, JPEG (Max 5MB per gambar)</p>
                        <div class="file-count" id="fileCount">Belum ada file dipilih</div>
                    </div>
                    <input type="file" name="gambar[]" id="gambar" accept="image/*" multiple onchange="previewMultipleImages()">
                </div>

                <!-- Preview Gallery -->
                <div class="gallery-preview" id="galleryPreview">
                    <div class="gallery-header">
                        <span><i class="fas fa-images"></i> Preview Gambar (<span id="previewCount">0</span> gambar)</span>
                        <button type="button" class="clear-all" onclick="clearAllImages()">
                            <i class="fas fa-trash-alt"></i> Hapus Semua
                        </button>
                    </div>
                    <div class="preview-grid" id="previewGrid"></div>
                </div>

                <!-- Tombol Submit -->
                <button type="submit" name="submit" class="btn-submit">
                    <i class="fas fa-save"></i>
                    Simpan Berita
                </button>

                <?php if(!empty($success_message)): ?>
                <!-- Tombol Aksi Setelah Sukses -->
                <div class="btn-group-actions">
                    <button type="button" class="btn-tambah-lagi" onclick="resetForm()">
                        <i class="fas fa-plus"></i> Tambah Lagi
                    </button>
                    <a href="../berita.php" class="btn-lihat-berita">
                        <i class="fas fa-newspaper"></i> Lihat Daftar Berita
                    </a>
                </div>
                <?php endif; ?>

            </form>
        </div>
    </div>
</div>

<!-- ========== FOOTER ========== -->
<footer class="footer-premium">
    <p>&copy; <?php echo date('Y'); ?> Dinas Perpustakaan dan Arsip Daerah Kabupaten Solok Selatan</p>
    <p>Jl. Lubuk Gadang - Simpang Tigo Rimbo Tangah, Sangir, Solok Selatan</p>
</footer>

<script>
    // 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 = [];
    
    // Preview multiple images
    function previewMultipleImages(){
        const input = document.getElementById('gambar');
        const galleryPreview = document.getElementById('galleryPreview');
        const previewGrid = document.getElementById('previewGrid');
        const fileCountSpan = document.getElementById('fileCount');
        const previewCountSpan = document.getElementById('previewCount');
        
        selectedFiles = Array.from(input.files);
        const fileCount = selectedFiles.length;
        fileCountSpan.textContent = fileCount + ' file dipilih';
        previewCountSpan.textContent = fileCount;
        
        if(fileCount > 0){
            galleryPreview.classList.add('active');
            let html = '';
            selectedFiles.forEach((file, index) => {
                const imageUrl = URL.createObjectURL(file);
                const fileName = file.name.length > 20 ? file.name.substring(0, 17) + '...' : file.name;
                html += `
                    <div class="preview-item" data-index="${index}">
                        <img src="${imageUrl}" alt="Preview ${index + 1}">
                        <button type="button" class="remove-img" onclick="removeImage(${index})">
                            <i class="fas fa-times"></i>
                        </button>
                        <div class="index">${index + 1}</div>
                        <div class="info">
                            <div class="name">${fileName}</div>
                            <div class="size">${(file.size / 1024).toFixed(1)} KB</div>
                        </div>
                    </div>
                `;
            });
            previewGrid.innerHTML = html;
        } else {
            galleryPreview.classList.remove('active');
            previewGrid.innerHTML = '';
        }
    }
    
    // Remove specific image
    function removeImage(indexToRemove){
        const input = document.getElementById('gambar');
        const dt = new DataTransfer();
        const remainingFiles = selectedFiles.filter((_, index) => index !== indexToRemove);
        remainingFiles.forEach(file => { dt.items.add(file); });
        input.files = dt.files;
        selectedFiles = remainingFiles;
        
        const fileCount = selectedFiles.length;
        document.getElementById('fileCount').textContent = fileCount + ' file dipilih';
        document.getElementById('previewCount').textContent = fileCount;
        
        if(fileCount > 0){
            const previewGrid = document.getElementById('previewGrid');
            let html = '';
            selectedFiles.forEach((file, index) => {
                const imageUrl = URL.createObjectURL(file);
                const fileName = file.name.length > 20 ? file.name.substring(0, 17) + '...' : file.name;
                html += `
                    <div class="preview-item" data-index="${index}">
                        <img src="${imageUrl}" alt="Preview ${index + 1}">
                        <button type="button" class="remove-img" onclick="removeImage(${index})">
                            <i class="fas fa-times"></i>
                        </button>
                        <div class="index">${index + 1}</div>
                        <div class="info">
                            <div class="name">${fileName}</div>
                            <div class="size">${(file.size / 1024).toFixed(1)} KB</div>
                        </div>
                    </div>
                `;
            });
            previewGrid.innerHTML = html;
        } else {
            document.getElementById('galleryPreview').classList.remove('active');
        }
    }
    
    // Clear all images
    function clearAllImages(){
        const input = document.getElementById('gambar');
        input.value = '';
        selectedFiles = [];
        document.getElementById('fileCount').textContent = 'Belum ada file dipilih';
        document.getElementById('previewCount').textContent = '0';
        document.getElementById('galleryPreview').classList.remove('active');
        document.getElementById('previewGrid').innerHTML = '';
    }
    
    // Reset form (untuk tambah lagi)
    function resetForm() {
        document.getElementById('judul').value = '';
        document.getElementById('isi').value = '';
        document.getElementById('tanggal').value = '<?php echo date('Y-m-d'); ?>';
        clearAllImages();
        document.getElementById('judul').focus();
        // Hapus alert
        const alertSuccess = document.getElementById('alertSuccess');
        if(alertSuccess) alertSuccess.remove();
        window.scrollTo({ top: 0, behavior: 'smooth' });
    }
    
    // Set tanggal default ke hari ini
    const today = new Date().toISOString().split('T')[0];
    if(!document.getElementById('tanggal').value){
        document.getElementById('tanggal').value = today;
    }
    
    // Validasi sebelum submit
    document.getElementById('formBerita').addEventListener('submit', function(e) {
        const judul = document.getElementById('judul').value.trim();
        const isi = document.getElementById('isi').value.trim();
        const files = document.getElementById('gambar').files;
        
        if(judul === ''){
            e.preventDefault();
            alert('⚠️ Judul berita tidak boleh kosong!');
            document.getElementById('judul').focus();
            return false;
        }
        
        if(isi === ''){
            e.preventDefault();
            alert('⚠️ Isi berita tidak boleh kosong!');
            document.getElementById('isi').focus();
            return false;
        }
        
        if(files.length === 0){
            e.preventDefault();
            alert('⚠️ Minimal pilih 1 gambar untuk berita! Kamu bisa pilih banyak gambar sekaligus dengan tekan CTRL/CMD.');
            return false;
        }
        
        return true;
    });

    // Auto close alert after 5 seconds
    setTimeout(function() {
        const alertSuccess = document.getElementById('alertSuccess');
        if(alertSuccess) {
            alertSuccess.style.transition = 'opacity 0.5s';
            alertSuccess.style.opacity = '0';
            setTimeout(function() {
                alertSuccess.remove();
            }, 500);
        }
    }, 5000);
</script>

</body>
</html>