<?php

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}


// ========================================
// CEK LOGIN ADMIN
// ========================================

if (!isset($_SESSION['user_id'])) {

    header("Location: login.php");

    exit;
}


// ========================================
// CEK ROLE ADMIN
// ========================================

if (
    !isset($_SESSION['role']) ||
    $_SESSION['role'] !== 'admin'
) {

    header("Location: ../index.php");

    exit;
}


// ========================================
// KONEKSI DATABASE
// ========================================

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


// ========================================
// AMBIL DATA SAMBUTAN
// ========================================

$query = "
    SELECT *
    FROM sambutan
    ORDER BY id DESC
    LIMIT 1
";

$result = mysqli_query(
    $koneksi,
    $query
);


// ========================================
// CEK DATA SAMBUTAN
// ========================================

if (
    !$result ||
    mysqli_num_rows($result) === 0
) {

    die("
        <div style='
            font-family: Arial;
            padding: 30px;
        '>

            <h2>Data Sambutan Belum Ada</h2>

            <p>
                Silakan tambahkan data sambutan
                terlebih dahulu ke tabel sambutan.
            </p>

            <a href='dashboard.php'>
                Kembali ke Dashboard
            </a>

        </div>
    ");
}


// ========================================
// AMBIL DATA
// ========================================

$sambutan = mysqli_fetch_assoc(
    $result
);

?>

<!DOCTYPE html>

<html lang="id">

<head>
    <link rel="icon"
        type="image/png"
        href="/website-dispussip/assets/img/logo.PNG">
    <meta charset="UTF-8">

    <meta charset="UTF-8">

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

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


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


    <style>
        body {

            background: #f5f7fb;

        }


        .container-edit {

            max-width: 900px;

            margin: 50px auto;

        }


        .card {

            border: none;

            border-radius: 15px;

            box-shadow:
                0 10px 30px rgba(0, 0, 0, 0.08);

        }


        .card-header {

            background: #0d6efd;

            color: white;

            padding: 20px;

            border-radius:
                15px 15px 0 0;

        }


        textarea {

            min-height: 300px;

        }


        .foto-preview {

            max-width: 250px;

            max-height: 300px;

            object-fit: cover;

            border-radius: 10px;

            margin-top: 10px;

        }
    </style>


</head>

<body>

    <div class="container-edit">


        <div class="card">


            <!-- HEADER -->

            <div class="card-header">

                <h3 class="mb-0">

                    Edit Sambutan Kepala Dinas

                </h3>

            </div>


            <!-- BODY -->

            <div class="card-body p-4">


                <?php if (
                    isset($_GET['success'])
                ): ?>

                    <div class="alert alert-success">

                        Sambutan berhasil diperbarui.

                    </div>

                <?php endif; ?>


                <form
                    action="update-sambutan.php"
                    method="POST"
                    enctype="multipart/form-data">


                    <!-- ID SAMBUTAN -->

                    <input
                        type="hidden"
                        name="id"
                        value="<?= htmlspecialchars(
                                    $sambutan['id']
                                ); ?>">


                    <!-- JUDUL -->

                    <div class="mb-4">

                        <label class="form-label">

                            <strong>
                                Judul Sambutan
                            </strong>

                        </label>

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

                    </div>


                    <!-- ISI -->

                    <div class="mb-4">

                        <label class="form-label">

                            <strong>
                                Isi Sambutan
                            </strong>

                        </label>

                        <textarea
                            name="isi"
                            class="form-control"
                            required><?= htmlspecialchars(
                                            $sambutan['isi']
                                        ); ?></textarea>

                        <small class="text-muted">

                            Gunakan baris baru untuk
                            memisahkan paragraf.

                        </small>

                    </div>


                    <!-- FOTO -->

                    <div class="mb-4">

                        <label class="form-label">

                            <strong>
                                Foto Kepala Dinas
                            </strong>

                        </label>


                        <input
                            type="file"
                            name="foto"
                            class="form-control"
                            accept="image/jpeg,image/png,image/jpg">


                        <small class="text-muted">

                            Pilih foto baru jika ingin
                            mengganti foto Kepala Dinas.

                        </small>


                        <?php if (
                            !empty($sambutan['foto'])
                        ): ?>


                            <div class="mt-3">

                                <p class="mb-1">

                                    Foto Saat Ini:

                                </p>


                                <img
                                    src="../uploads/kepala-dinas/<?= htmlspecialchars(
                                                                        $sambutan['foto']
                                                                    ); ?>"
                                    alt="Foto Kepala Dinas"
                                    class="foto-preview">

                            </div>


                        <?php endif; ?>


                    </div>


                    <!-- TOMBOL -->

                    <div class="d-flex gap-2">


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

                            Simpan

                        </button>


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

                            Kembali

                        </a>


                        <a
                            href="../index.php"
                            target="_blank"
                            class="btn btn-success">

                            🌐 Lihat Website

                        </a>


                    </div>


                </form>


            </div>

        </div>


    </div>

</body>

</html>