<?php

session_start();

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

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

// Ambil data pengumuman
$data = mysqli_query(
    $koneksi,
    "SELECT * FROM pengumuman ORDER BY tanggal DESC, id DESC"
);

?>

<!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>Kelola 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: 1200px;
        }

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

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

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

        .table {
            margin-bottom: 0;
        }

        .table th {
            background: #eaf1fb;
            color: #07357e;
            vertical-align: middle;
            text-align: center;
        }

        .table td {
            vertical-align: middle;
        }

        .judul {
            font-weight: 600;
            color: #07357e;
        }

        .isi-pengumuman {
            max-width: 450px;
            line-height: 1.5;
        }

        .btn-tambah {
            background: white;
            color: #07357e;
            border: none;
            font-weight: 600;
        }

        .btn-tambah:hover {
            background: #f1f5fb;
            color: #07357e;
        }

        .btn-edit {
            background: #ffc107;
            color: #000;
            border: none;
        }

        .btn-edit:hover {
            background: #e0a800;
            color: #000;
        }

        .btn-hapus {
            background: #dc3545;
            color: white;
            border: none;
        }

        .btn-hapus:hover {
            background: #bb2d3b;
            color: white;
        }

        .btn-dashboard {
            background: #07357e;
            color: white;
            border: none;
        }

        .btn-dashboard:hover {
            background: #05285f;
            color: white;
        }

        .empty-data {
            padding: 40px;
            color: #777;
        }
    </style>

</head>

<body>

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

        <div class="card shadow">

            <!-- HEADER -->

            <div class="card-header d-flex justify-content-between align-items-center">

                <div>

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

                    <small>
                        Kelola informasi pengumuman yang ditampilkan pada website
                        DISPUSIP Kabupaten Banyuwangi.
                    </small>

                </div>

                <a
                    href="tambah.php"
                    class="btn btn-tambah">
                    + Tambah Pengumuman
                </a>

            </div>


            <!-- BODY -->

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

                <!-- NOTIFIKASI -->

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

                    <?php if ($_GET['status'] == 'update'): ?>

                        <div class="alert alert-success alert-dismissible fade show">
                            Pengumuman berhasil diperbarui.
                            <button
                                type="button"
                                class="btn-close"
                                data-bs-dismiss="alert"></button>
                        </div>

                    <?php elseif ($_GET['status'] == 'tambah'): ?>

                        <div class="alert alert-success alert-dismissible fade show">
                            Pengumuman berhasil ditambahkan.
                            <button
                                type="button"
                                class="btn-close"
                                data-bs-dismiss="alert"></button>
                        </div>

                    <?php elseif ($_GET['status'] == 'hapus'): ?>

                        <div class="alert alert-success alert-dismissible fade show">
                            Pengumuman berhasil dihapus.
                            <button
                                type="button"
                                class="btn-close"
                                data-bs-dismiss="alert"></button>
                        </div>

                    <?php elseif ($_GET['status'] == 'gagal'): ?>

                        <div class="alert alert-danger alert-dismissible fade show">
                            Terjadi kesalahan. Data tidak dapat diproses.
                            <button
                                type="button"
                                class="btn-close"
                                data-bs-dismiss="alert"></button>
                        </div>

                    <?php endif; ?>

                <?php endif; ?>


                <!-- TABEL -->

                <div class="table-responsive">

                    <table class="table table-bordered table-hover">

                        <thead>

                            <tr>

                                <th width="60">
                                    No
                                </th>

                                <th width="250">
                                    Judul Pengumuman
                                </th>

                                <th width="140">
                                    Tanggal
                                </th>

                                <th>
                                    Isi Pengumuman
                                </th>

                                <th width="160">
                                    Aksi
                                </th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php

                            $no = 1;

                            if (mysqli_num_rows($data) > 0):

                                while ($row = mysqli_fetch_assoc($data)):

                            ?>

                                    <tr>

                                        <!-- NO -->

                                        <td class="text-center">
                                            <?= $no++; ?>
                                        </td>


                                        <!-- JUDUL -->

                                        <td>

                                            <div class="judul">

                                                <?= htmlspecialchars($row['judul']); ?>

                                            </div>

                                        </td>


                                        <!-- TANGGAL -->

                                        <td class="text-center">

                                            <?= date(
                                                'd-m-Y',
                                                strtotime($row['tanggal'])
                                            ); ?>

                                        </td>


                                        <!-- ISI -->

                                        <td>

                                            <div class="isi-pengumuman">

                                                <?php

                                                $isi = strip_tags($row['isi']);

                                                if (strlen($isi) > 120) {

                                                    echo htmlspecialchars(
                                                        substr($isi, 0, 120)
                                                    ) . "...";
                                                } else {

                                                    echo htmlspecialchars($isi);
                                                }

                                                ?>

                                            </div>

                                        </td>


                                        <!-- AKSI -->

                                        <td class="text-center">

                                            <a
                                                href="edit.php?id=<?= $row['id']; ?>"
                                                class="btn btn-edit btn-sm">
                                                Edit
                                            </a>

                                            <a
                                                href="hapus.php?id=<?= $row['id']; ?>"
                                                class="btn btn-hapus btn-sm"
                                                onclick="return confirm('Apakah Anda yakin ingin menghapus pengumuman ini?');">
                                                Hapus
                                            </a>

                                        </td>

                                    </tr>

                                <?php

                                endwhile;

                            else:

                                ?>

                                <tr>

                                    <td
                                        colspan="5"
                                        class="text-center empty-data">

                                        Belum ada data pengumuman.

                                        <br>

                                        <a
                                            href="tambah.php"
                                            class="btn btn-primary btn-sm mt-3">
                                            + Tambah Pengumuman
                                        </a>

                                    </td>

                                </tr>

                            <?php endif; ?>

                        </tbody>

                    </table>

                </div>


                <!-- KEMBALI KE DASHBOARD -->

                <div class="mt-4">

                    <a
                        href="../dashboard.php"
                        class="btn btn-dashboard">
                        ← Kembali ke Dashboard
                    </a>

                </div>

            </div>

        </div>

    </div>


    <script
        src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

</body>

</html>