<?php

session_start();

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

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

// Ambil semua berita
$query = "
    SELECT *
    FROM berita
    ORDER BY id DESC
";

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

?>

<!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 Berita - DISPUSIP Banyuwangi</title>

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

    <style>
        body {
            background: #f5f7fb;
        }

        .topbar {
            background: white;
            padding: 20px 25px;
            border-radius: 12px;
            margin-bottom: 25px;
        }

        .berita-card {
            background: white;
            border: none;
            border-radius: 12px;
            overflow: hidden;
            height: 100%;
            box-shadow: 0 3px 15px rgba(0, 0, 0, 0.06);
            transition: 0.2s;
        }

        .berita-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.10);
        }

        .berita-img {
            width: 100%;
            height: 190px;
            object-fit: cover;
        }

        .no-image {
            width: 100%;
            height: 190px;
            background: #e9eef5;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #777;
        }

        .berita-body {
            padding: 20px;
        }

        .berita-title {
            font-size: 18px;
            font-weight: 600;
            color: #222;
        }

        .berita-date {
            font-size: 13px;
            color: #777;
        }

        .btn-tambah {
            border-radius: 8px;
        }
    </style>

</head>

<body>

    <div class="container py-4">

        <!-- HEADER -->

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

            <div>

                <h3 class="mb-1">
                    Kelola Berita
                </h3>

                <p class="text-muted mb-0">
                    Kelola berita yang ditampilkan pada website DISPUSIP Banyuwangi.
                </p>

            </div>

            <a
                href="tambah.php"
                class="btn btn-primary btn-tambah">

                + Tambah Berita

            </a>

        </div>


        <!-- TOMBOL KEMBALI -->

        <div class="mb-4">

            <a
                href="../dashboard.php"
                class="text-decoration-none">

                ← Kembali ke Dashboard

            </a>

        </div>


        <!-- DAFTAR BERITA -->

        <div class="row g-4">

            <?php if ($result && mysqli_num_rows($result) > 0): ?>

                <?php while ($berita = mysqli_fetch_assoc($result)): ?>

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

                        <div class="berita-card">

                            <!-- FOTO -->

                            <?php if (
                                !empty($berita['gambar']) &&
                                file_exists("../../uploads/berita/" . $berita['gambar'])
                            ): ?>

                                <img
                                    src="../../uploads/berita/<?= htmlspecialchars($berita['gambar']); ?>"
                                    class="berita-img"
                                    alt="<?= htmlspecialchars($berita['judul']); ?>">

                            <?php else: ?>

                                <div class="no-image">

                                    Tidak ada foto

                                </div>

                            <?php endif; ?>


                            <!-- ISI -->

                            <div class="berita-body">

                                <div class="berita-date mb-2">

                                    <?= htmlspecialchars(
                                        $berita['tanggal'] ?? ''
                                    ); ?>

                                </div>


                                <div class="berita-title mb-3">

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

                                </div>


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

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

                                        Edit

                                    </a>


                                    <a
                                        href="hapus.php?id=<?= $berita['id']; ?>"
                                        class="btn btn-sm btn-danger"
                                        onclick="return confirm('Yakin ingin menghapus berita ini?');">

                                        Hapus

                                    </a>

                                </div>

                            </div>

                        </div>

                    </div>

                <?php endwhile; ?>

            <?php else: ?>

                <div class="col-12">

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

                        Belum ada berita yang ditambahkan.

                        <br><br>

                        <a
                            href="tambah.php"
                            class="btn btn-primary">

                            Tambah Berita Pertama

                        </a>

                    </div>

                </div>

            <?php endif; ?>

        </div>

    </div>

</body>

</html>