<?php

session_start();

require_once "../../config/koneksi.php";

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

$id = $_GET['id'];

$query = mysqli_query($koneksi, "SELECT * FROM galeri WHERE id='$id'");
$data = mysqli_fetch_assoc($query);

?>

<!DOCTYPE html>
<html lang="id">

<head>

    <meta charset="UTF-8">

    <title>Edit Galeri - Admin DISPUSIP</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

    <style>
        body {
            background: #f4f7fb;
        }

        .card {
            border: none;
            border-radius: 15px;
            overflow: hidden;
        }

        .card-header {
            background: #0d6efd;
            color: #fff;
            padding: 20px 30px;
        }

        .card-header h4 {
            margin: 0;
        }

        .card-header p {
            margin: 8px 0 0;
        }

        .card-body {
            padding: 30px;
        }

        .form-label {
            font-weight: 600;
        }

        img {
            border-radius: 10px;
            border: 1px solid #ddd;
            object-fit: cover;
        }
    </style>

</head>

<body>

    <div class="container py-5">

        <div class="card shadow">

            <div class="card-header">

                <h4> Edit Galeri</h4>

                <p>Ubah data galeri website DISPUSIP Kabupaten Banyuwangi.</p>

            </div>

            <div class="card-body">

                <form action="proses-edit.php" method="POST" enctype="multipart/form-data">

                    <input
                        type="hidden"
                        name="id"
                        value="<?= $data['id']; ?>">

                    <input
                        type="hidden"
                        name="gambar_lama"
                        value="<?= $data['gambar']; ?>">

                    <div class="mb-3">

                        <label class="form-label">
                            Judul Galeri
                        </label>

                        <input
                            type="text"
                            name="judul"
                            class="form-control"
                            value="<?= htmlspecialchars($data['judul']); ?>"
                            required>

                    </div>

                    <div class="mb-3">

                        <label class="form-label">
                            Foto Saat Ini
                        </label>

                        <br>

                        <?php if (!empty($data['gambar'])): ?>

                            <img
                                src="../../uploads/galeri/<?= htmlspecialchars($data['gambar']); ?>"
                                width="180"
                                height="130">

                        <?php else: ?>

                            <p class="text-muted">
                                Belum ada foto.
                            </p>

                        <?php endif; ?>

                    </div>

                    <div class="mb-3">

                        <label class="form-label">
                            Ganti Foto
                        </label>

                        <input
                            type="file"
                            name="gambar"
                            class="form-control"
                            accept=".jpg,.jpeg,.png,.webp">

                        <small class="text-muted">
                            Kosongkan jika tidak ingin mengganti foto.
                        </small>

                    </div>

                    <div class="mb-3">

                        <label class="form-label">
                            Link Instagram
                        </label>

                        <input
                            type="url"
                            name="link"
                            class="form-control"
                            value="<?= htmlspecialchars($data['link']); ?>"
                            placeholder="https://www.instagram.com/...">

                    </div>

                    <div class="mb-3">

                        <label class="form-label">
                            Deskripsi
                        </label>

                        <textarea
                            name="deskripsi"
                            class="form-control"
                            rows="5"><?= htmlspecialchars($data['deskripsi']); ?></textarea>

                    </div>

                    <button
                        type="submit"
                        class="btn btn-primary">

                        Update

                    </button>

                    <a
                        href="index.php"
                        class="btn btn-secondary">

                        ← Kembali

                    </a>

                </form>

            </div>

        </div>

    </div>

</body>

</html>