<?php

session_start();

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


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

    header("Location: ../login.php");
    exit;
}


$query = mysqli_query(
    $koneksi,
    "SELECT * FROM galeri ORDER BY 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">

    <title>Kelola Galeri</title>


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


    <style>
        body {
            background: #f4f6f9;
        }


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


        .table img {

            width: 90px;
            height: 70px;
            object-fit: cover;
            border-radius: 8px;
            border: 1px solid #ddd;

        }


        .table th {

            background: #0d6efd;
            color: white;
            text-align: center;

        }


        .table td {

            vertical-align: middle;

        }


        .btn {

            border-radius: 8px;

        }
    </style>


</head>


<body>


    <div class="container py-4">


        <div class="card shadow">



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


                <h4 class="mb-0">

                    Kelola Galeri

                </h4>



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

                    + Tambah Foto

                </a>



            </div>





            <div class="card-body">


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


                    <thead>


                        <tr>

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

                            <th>
                                Foto
                            </th>

                            <th>
                                Judul
                            </th>

                            <th>
                                Link Instagram
                            </th>

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


                        </tr>


                    </thead>




                    <tbody>


                        <?php

                        $no = 1;


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


                        ?>


                            <tr>


                                <td class="text-center">

                                    <?= $no++; ?>

                                </td>



                                <td class="text-center">


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


                                        <img src="../../uploads/galeri/<?= $data['gambar']; ?>">


                                    <?php } else { ?>


                                        Tidak ada foto


                                    <?php } ?>


                                </td>




                                <td>

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

                                </td>




                                <td>


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


                                        <a href="<?= $data['link']; ?>"
                                            target="_blank"
                                            class="btn btn-info btn-sm text-white">

                                            Lihat Instagram

                                        </a>


                                    <?php } else { ?>


                                        -


                                    <?php } ?>


                                </td>




                                <td class="text-center">


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

                                        Edit

                                    </a>




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

                                        Hapus

                                    </a>



                                </td>


                            </tr>


                        <?php } ?>


                    </tbody>


                </table>


            </div>


        </div>




        <div class="mt-3">


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

                ← Kembali ke Dashboard

            </a>


        </div>


    </div>


</body>


</html>