<?php

session_start();

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

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

// Cek ID
if (!isset($_GET['id']) || empty($_GET['id'])) {
    header("Location: index.php");
    exit;
}

$id = (int) $_GET['id'];

// Ambil data pengumuman
$query = mysqli_query(
    $koneksi,
    "SELECT * FROM pengumuman WHERE id = $id"
);

$row = mysqli_fetch_assoc($query);

// Jika data tidak ditemukan
if (!$row) {
    header("Location: index.php");
    exit;
}

?>

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

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Edit Pengumuman - 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;
            font-family: Arial, sans-serif;
        }

        .container {
            max-width: 900px;
        }

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

        .card-header {
            background: #07357e;
            color: white;
            padding: 20px 30px;
        }

        .card-header h4 {
            font-weight: 600;
        }

        .card-body {
            padding: 30px;
        }

        .form-label {
            font-weight: 600;
            color: #333;
        }

        .form-control {
            border-radius: 8px;
            padding: 10px 12px;
        }

        .form-control:focus {
            border-color: #07357e;
            box-shadow: 0 0 0 0.2rem rgba(7, 53, 126, 0.15);
        }

        .btn-primary {
            background: #07357e;
            border-color: #07357e;
        }

        .btn-primary:hover {
            background: #05285f;
            border-color: #05285f;
        }
    </style>

</head>

<body>

    <div class="container mt-5 mb-5">

        <div class="card shadow">

            <!-- HEADER -->

            <div class="card-header">

                <h4 class="mb-1">
                    Edit Pengumuman
                </h4>

                <small>
                    Perbarui informasi pengumuman website DISPUSIP Kabupaten Banyuwangi.
                </small>

            </div>


            <!-- BODY -->

            <div class="card-body">

                <form
                    action="proses-edit.php"
                    method="POST">

                    <!-- ID -->

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


                    <!-- JUDUL -->

                    <div class="mb-4">

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

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

                    </div>


                    <!-- TANGGAL -->

                    <div class="mb-4">

                        <label class="form-label">
                            Tanggal Pengumuman
                        </label>

                        <input
                            type="date"
                            name="tanggal"
                            class="form-control"
                            value="<?= htmlspecialchars($row['tanggal']); ?>"
                            required>

                    </div>


                    <!-- ISI -->

                    <div class="mb-4">

                        <label class="form-label">
                            Isi Pengumuman
                        </label>

                        <textarea
                            name="isi"
                            class="form-control"
                            rows="8"
                            placeholder="Tuliskan isi pengumuman..."
                            required><?= htmlspecialchars($row['isi']); ?></textarea>

                    </div>


                    <!-- BUTTON -->

                    <div class="mt-4">

                        <button
                            type="submit"
                            class="btn btn-primary">
                            Simpan Perubahan
                        </button>

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

                    </div>

                </form>

            </div>

        </div>

    </div>

</body>

</html>