<?php

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

include "../includes/header.php";
include "../includes/navbar.php";

?>
<div style="padding-top:120px;">
    <div class="container py-5">

        <!-- JUDUL HALAMAN -->
        <div class="text-center mb-5">

            <h1 class="section-title">
                Berita
            </h1>

            <p class="text-muted">
                Berita dan informasi terbaru Dinas Perpustakaan
                dan Kearsipan Kabupaten Banyuwangi.
            </p>

        </div>
        <style>
            .btn-primary {
                background-color: #07357e !important;
                border-color: #07357e !important;
            }
        </style>

        <!-- DAFTAR BERITA -->
        <div class="row g-4">

            <?php

            $query = mysqli_query(
                $koneksi,
                "SELECT * FROM berita ORDER BY tanggal DESC"
            );

            if (mysqli_num_rows($query) > 0) {

                while ($data = mysqli_fetch_assoc($query)) {

            ?>

                    <div class="col-lg-4 col-md-6">

                        <div class="card shadow-sm h-100">

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

                                <img
                                    src="../uploads/berita/<?= htmlspecialchars($data['gambar']); ?>"
                                    class="card-img-top"
                                    style="height:220px; object-fit:cover;"
                                    alt="<?= htmlspecialchars($data['judul']); ?>">

                            <?php } ?>

                            <div class="card-body d-flex flex-column">

                                <h5 class="card-title">

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

                                </h5>

                                <small class="text-muted">

                                    <?= date('d F Y', strtotime($data['tanggal'])); ?>

                                </small>

                                <p class="mt-3">

                                    <?= substr(strip_tags($data['isi']), 0, 150); ?>...

                                </p>

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

                                    <a
                                        href="detail-berita.php?id=<?= $data['id']; ?>"
                                        class="btn btn-primary">

                                        Selengkapnya

                                    </a>

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

                                        <a
                                            href="<?= htmlspecialchars($data['link_instagram']); ?>"
                                            target="_blank"
                                            rel="noopener noreferrer"
                                            class="btn btn-danger">

                                            Instagram

                                        </a>

                                    <?php endif; ?>

                                </div>

                            </div>

                        </div>

                    </div>

                <?php

                }
            } else {

                ?>

                <div class="col-12">

                    <div class="alert alert-info text-center">

                        Belum ada berita yang dipublikasikan.

                    </div>

                </div>

            <?php

            }

            ?>

        </div>

    </div>

    <?php

    include "../includes/footer.php";

    ?>