<?php

session_start();

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

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

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

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

include '../config/koneksi.php';

$data = mysqli_query($conn,
"SELECT * FROM rumah_ibadah");

?>

<!DOCTYPE html>
<html>

<head>

    <title>Rumah Ibadah</title>

    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" />

    <?php include 'layout/style.php'; ?>

</head>

<body>

    <?php include 'layout/navbar.php'; ?>

    <?php include 'layout/sidebar.php'; ?>

    <div class="content">


        <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4 mb-6">

            <div>

                <h1 class="text-3xl font-bold text-gray-800">
                    Data Rumah Ibadah
                </h1>

                <p class="text-gray-500 mt-2">
                    Kelola data rumah ibadah yang berpartisipasi dalam program bantuan sosial.
                </p>

            </div>

            <a href="tambah_rumah_ibadah.php"
                class="inline-flex items-center gap-2 px-5 py-3 bg-blue-600 hover:bg-blue-700 text-white rounded-xl shadow transition">

                <i class="fas fa-plus"></i>

                Tambah Rumah Ibadah

            </a>

        </div>

        <?php

$total_rumah_ibadah = mysqli_num_rows($data);

mysqli_data_seek($data,0);

?>

        <div class="grid md:grid-cols-3 gap-5 mb-6">

            <div class="bg-white rounded-2xl shadow p-5">

                <div class="flex items-center justify-between">

                    <div>

                        <p class="text-gray-500 text-sm">
                            Total Rumah Ibadah
                        </p>

                        <h2 class="text-3xl font-bold text-blue-600">
                            <?= $total_rumah_ibadah ?>
                        </h2>

                    </div>

                    <i class="fas fa-place-of-worship text-4xl text-blue-500"></i>

                </div>

            </div>

        </div>

        <div class="bg-white rounded-2xl shadow overflow-hidden">

            <div class="p-5 border-b">

                <h2 class="font-semibold text-lg">
                    Daftar Rumah Ibadah
                </h2>

            </div>

            <div class="overflow-x-auto">

                <table class="min-w-full">

                    <thead class="bg-gray-100">

                        <tr>

                            <th class="px-4 py-3 text-left">No</th>
                            <th class="px-4 py-3 text-left">Nama</th>
                            <th class="px-4 py-3 text-left">Alamat</th>
                            <th class="px-4 py-3 text-left">Latitude</th>
                            <th class="px-4 py-3 text-left">Longitude</th>
                            <th class="px-4 py-3 text-center">Aksi</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php

                $no = 1;

                mysqli_data_seek($data,0);

                if(mysqli_num_rows($data) > 0){

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

                ?>

                        <tr class="border-b hover:bg-gray-50 transition">

                            <td class="px-4 py-3">
                                <?= $no++ ?>
                            </td>

                            <td class="px-4 py-3 font-medium">
                                <?= htmlspecialchars($row['nama']) ?>
                            </td>

                            <td class="px-4 py-3">
                                <?= htmlspecialchars($row['alamat']) ?>
                            </td>

                            <td class="px-4 py-3">
                                <?= htmlspecialchars($row['latitude']) ?>
                            </td>

                            <td class="px-4 py-3">
                                <?= htmlspecialchars($row['longitude']) ?>
                            </td>

                            <td class="px-4 py-3 text-center">

                                <div class="flex flex-wrap justify-center gap-2">

                                    <a href="https://www.google.com/maps?q=<?= $row['latitude'] ?>,<?= $row['longitude'] ?>"
                                        target="_blank"
                                        class="px-3 py-2 bg-green-600 hover:bg-green-700 text-white rounded-lg transition">

                                        <i class="fas fa-map-marker-alt"></i>

                                    </a>

                                    <a href="hapus_rumah_ibadah.php?id=<?= $row['id'] ?>"
                                        onclick="return confirm('Yakin hapus data?')"
                                        class="px-3 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg transition">

                                        <i class="fas fa-trash"></i>

                                    </a>

                                </div>

                            </td>

                        </tr>

                        <?php } } else { ?>

                        <tr>

                            <td colspan="6" class="py-10 text-center text-gray-500">

                                <i class="fas fa-place-of-worship text-4xl mb-3 block"></i>

                                Belum ada data rumah ibadah

                            </td>

                        </tr>

                        <?php } ?>

                    </tbody>

                </table>

            </div>

        </div>


    </div>


</body>

</html>