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

/* =========================
   CEK LOGIN ADMIN
========================= */
if(!isset($_SESSION['admin'])){
    header("Location: login.php");
    exit;
}

/* =========================
   CEK ID BERITA
========================= */
if(!isset($_GET['id'])){
    echo "
    <script>
        alert('ID berita tidak ditemukan!');
        window.location='dashboard_admin.php';
    </script>
    ";
    exit;
}

$id = $_GET['id'];

/* =========================
   AMBIL DATA BERITA
========================= */
$query = mysqli_query($koneksi,"
    SELECT * FROM berita
    WHERE id='$id'
");

if(mysqli_num_rows($query) == 0){
    echo "
    <script>
        alert('Data berita tidak ditemukan!');
        window.location='dashboard_admin.php';
    </script>
    ";
    exit;
}

$data = mysqli_fetch_assoc($query);

/* =========================
   UPDATE BERITA
========================= */
$pesan = '';
$pesan_icon = '';
$pesan_text = '';

if(isset($_POST['update'])){
    $judul   = mysqli_real_escape_string($koneksi,$_POST['judul']);
    $tanggal = $_POST['tanggal'];
    $isi     = mysqli_real_escape_string($koneksi,$_POST['isi']);

    // upload gambar baru
    if($_FILES['gambar']['name'] != ''){
        $gambar = time().'_'.$_FILES['gambar']['name'];
        $tmp    = $_FILES['gambar']['tmp_name'];
        
        // Hapus gambar lama jika ada
        if($data['gambar'] != '' && file_exists("img/".$data['gambar'])){
            unlink("img/".$data['gambar']);
        }
        
        if(move_uploaded_file($tmp,"img/".$gambar)){
            $update = mysqli_query($koneksi,"
                UPDATE berita SET
                judul='$judul',
                tanggal='$tanggal',
                isi='$isi',
                gambar='$gambar'
                WHERE id='$id'
            ");
        } else {
            $update = false;
        }
    }else{
        $update = mysqli_query($koneksi,"
            UPDATE berita SET
            judul='$judul',
            tanggal='$tanggal',
            isi='$isi'
            WHERE id='$id'
        ");
    }

    if($update){
        $pesan = 'success';
        $pesan_icon = 'fa-check-circle';
        $pesan_text = 'Berita berhasil diupdate!';
        // Refresh data
        $query = mysqli_query($koneksi,"
            SELECT * FROM berita
            WHERE id='$id'
        ");
        $data = mysqli_fetch_assoc($query);
    }else{
        $pesan = 'error';
        $pesan_icon = 'fa-exclamation-circle';
        $pesan_text = 'Gagal mengupdate berita!';
    }
}
?>

<!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>Edit Berita | DPAD Solok Selatan</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">

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

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

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

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

        /* ========== TOMBOL KEMBALI ========== */
        .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;
            margin-bottom: 20px;
        }

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

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

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

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

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

        .notification.success {
            background: #f0fdf4;
            border: 1px solid #86efac;
            color: #065f46;
        }

        .notification.success i {
            color: #22c55e;
            font-size: 1.3rem;
        }

        .notification.error {
            background: #fef2f2;
            border: 1px solid #fca5a5;
            color: #991b1b;
        }

        .notification.error i {
            color: #ef4444;
            font-size: 1.3rem;
        }

        .notification .close-btn {
            margin-left: auto;
            background: none;
            border: none;
            font-size: 1rem;
            cursor: pointer;
            color: inherit;
            opacity: 0.6;
            transition: 0.3s;
        }

        .notification .close-btn:hover {
            opacity: 1;
        }

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

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

        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: 25px 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: 2.5rem;
            color: #1a5f4b;
            display: block;
            margin-bottom: 8px;
        }

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

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

        /* ========== PREVIEW GAMBAR ========== */
        .preview-image {
            margin-top: 15px;
            padding: 15px 20px;
            background: #f8fafc;
            border-radius: 12px;
            border: 1px solid #e8edf2;
            display: flex;
            align-items: center;
            gap: 20px;
            flex-wrap: wrap;
        }

        .preview-image img {
            width: 100px;
            height: 100px;
            object-fit: cover;
            border-radius: 10px;
            border: 2px solid #e8edf2;
        }

        .preview-image .info {
            flex: 1;
        }

        .preview-image .info p {
            font-size: 0.8rem;
            color: #64748b;
        }

        .preview-image .info .file-name {
            font-weight: 600;
            color: #1e293b;
        }

        .preview-image .btn-remove {
            background: #fee2e2;
            color: #dc2626;
            border: none;
            padding: 6px 16px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 0.75rem;
            font-weight: 600;
            transition: 0.3s;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .preview-image .btn-remove:hover {
            background: #dc2626;
            color: white;
        }

        /* ========== 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;
            margin-top: 5px;
        }

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

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

        /* ========== 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;
            }
            .breadcrumb-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;
                margin: 15px auto 40px;
            }
            .breadcrumb-wrap {
                padding: 0 15px;
            }

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

            .btn-back {
                padding: 8px 14px;
                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;
            }

            input, textarea {
                padding: 12px 14px;
                font-size: 0.9rem;
            }

            .preview-image {
                flex-direction: column;
                text-align: center;
                padding: 15px;
            }

            .preview-image img {
                width: 80px;
                height: 80px;
            }

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

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

            .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 12px;
                font-size: 0.85rem;
            }

            .btn-back {
                padding: 6px 12px;
                font-size: 0.75rem;
            }
            .btn-back span {
                display: none;
            }

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

            .preview-image img {
                width: 60px;
                height: 60px;
            }
        }
        
    </style>
</head>
<body>

<!-- ========== HEADER ========== -->
<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="admin/index.php" class="menu-icon">
                    <i class="fas fa-home"></i>
                </a>
                <span class="tooltip">Dashboard</span>
            </div>
            <div class="menu-item">
                <a href="edit_berita.php" class="menu-icon active">
                    <i class="fas fa-newspaper"></i>
                </a>
                <span class="tooltip">Daftar</span>
            </div>
        </div>
    </div>
</div>
<!-- ========== CONTAINER ========== -->
<div class="container">
    <!-- ========== NOTIFICATION ========== -->
    <?php if($pesan == 'success'): ?>
    <div class="notification success" id="notification">
        <i class="fas <?= $pesan_icon; ?>"></i>
        <div><?= $pesan_text; ?></div>
        <button class="close-btn" onclick="closeNotification()">
            <i class="fas fa-times"></i>
        </button>
    </div>
    <?php elseif($pesan == 'error'): ?>
    <div class="notification error" id="notification">
        <i class="fas <?= $pesan_icon; ?>"></i>
        <div><?= $pesan_text; ?></div>
        <button class="close-btn" onclick="closeNotification()">
            <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-pen-fancy"></i></div>
                <div>
                    <h2>Edit Berita</h2>
                    <p>Perbarui informasi berita yang sudah ada</p>
                </div>
            </div>
        </div>
<div class="breadcrumb-wrap">
    <div class="breadcrumb">
        <a href="admin/index.php"><i class="fas fa-home"></i></a>
        <span class="sep">/</span>
        <a href="edit_berita.php">Berita</a>
        <span class="sep">/</span>
        <span class="active">Edit 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>

            <!-- ========== FORM ========== -->
            <form method="POST" enctype="multipart/form-data">

                <!-- 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" 
                        value="<?= htmlspecialchars($data['judul']); ?>"
                        placeholder="Masukkan judul berita..."
                        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" 
                        value="<?= $data['tanggal']; ?>"
                        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" 
                        rows="8" 
                        placeholder="Tulis isi berita di sini..."
                        required
                    ><?= htmlspecialchars($data['isi']); ?></textarea>
                </div>

                <!-- Upload Gambar -->
                <div class="form-group">
                    <label>
                        <i class="fas fa-images"></i> 
                        Ganti Gambar
                    </label>
                    <div class="file-upload" onclick="document.getElementById('gambar').click()">
                        <i class="fas fa-cloud-upload-alt"></i>
                        <p>Klik untuk upload gambar baru</p>
                        <p style="font-size: 12px; color: #94a3b8;">Format: JPG, PNG, JPEG (Max 5MB)</p>
                    </div>
                    <input type="file" name="gambar" id="gambar" accept="image/*" onchange="previewImage(this)">
                </div>

                <!-- Preview Gambar -->
                <?php if($data['gambar'] != '' && file_exists("img/".$data['gambar'])): ?>
                <div class="preview-image" id="previewContainer">
                    <img src="img/<?= $data['gambar']; ?>" alt="Gambar Berita">
                    <div class="info">
                        <p><span class="file-name"><?= $data['gambar']; ?></span></p>
                        <p style="font-size:0.7rem; color:#94a3b8;">Gambar saat ini</p>
                    </div>
                    <button type="button" class="btn-remove" onclick="removeImage()">
                        <i class="fas fa-trash-alt"></i> Hapus
                    </button>
                </div>
                <?php endif; ?>

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

            </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');
        }
    });

    // Close Notification
    function closeNotification() {
        const notification = document.getElementById('notification');
        if (notification) {
            notification.style.opacity = '0';
            notification.style.transform = 'translateY(-20px)';
            setTimeout(() => {
                notification.remove();
            }, 300);
        }
    }

    // Auto close notification after 4 seconds
    setTimeout(function() {
        const notification = document.getElementById('notification');
        if (notification) {
            closeNotification();
        }
    }, 4000);

    // Preview Image
    function previewImage(input) {
        const previewContainer = document.getElementById('previewContainer');
        if (input.files && input.files[0]) {
            const reader = new FileReader();
            reader.onload = function(e) {
                if (!previewContainer) {
                    const container = document.createElement('div');
                    container.id = 'previewContainer';
                    container.className = 'preview-image';
                    container.innerHTML = `
                        <img src="${e.target.result}" alt="Preview">
                        <div class="info">
                            <p><span class="file-name">${input.files[0].name}</span></p>
                            <p style="font-size:0.7rem; color:#94a3b8;">${(input.files[0].size / 1024).toFixed(1)} KB</p>
                        </div>
                        <button type="button" class="btn-remove" onclick="removeImage()">
                            <i class="fas fa-trash-alt"></i> Hapus
                        </button>
                    `;
                    document.querySelector('.file-upload').after(container);
                } else {
                    previewContainer.innerHTML = `
                        <img src="${e.target.result}" alt="Preview">
                        <div class="info">
                            <p><span class="file-name">${input.files[0].name}</span></p>
                            <p style="font-size:0.7rem; color:#94a3b8;">${(input.files[0].size / 1024).toFixed(1)} KB</p>
                        </div>
                        <button type="button" class="btn-remove" onclick="removeImage()">
                            <i class="fas fa-trash-alt"></i> Hapus
                        </button>
                    `;
                }
            }
            reader.readAsDataURL(input.files[0]);
        }
    }

    // Remove Image
    function removeImage() {
        const previewContainer = document.getElementById('previewContainer');
        const fileInput = document.getElementById('gambar');
        if (previewContainer) {
            previewContainer.remove();
        }
        fileInput.value = '';
    }
</script>

</body>
</html>