<?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 laporan");

if(!$data){
    die(mysqli_error($conn));
}
?>

<!DOCTYPE html>
<html>

<head>

    <title>Validasi</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="mb-6">
            <h1 class="text-3xl font-bold text-gray-800">
                Validasi Laporan
            </h1>
            <p class="text-gray-500 mt-1">
                Kelola laporan kemiskinan yang masuk dari masyarakat.
            </p>
        </div>

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

            <div class="p-5 border-b bg-gray-50">
                <h2 class="font-semibold text-lg">
                    Data Laporan
                </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">Foto</th>
                            <th class="px-4 py-3 text-left">Status</th>
                            <th class="px-4 py-3 text-center">Aksi</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php

                $no = 1;

                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">
                                <?= $row['nama_warga'] ?>
                            </td>

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

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

                                <?php if(!empty($row['foto'])){ ?>

                                <img src="../assets/uploads/<?= htmlspecialchars($row['foto']) ?>"
                                    onerror="this.src='../assets/uploads/no-image.png'"
                                    class="w-20 h-20 object-cover rounded-xl shadow">

                                <?php } else { ?>

                                <span class="text-gray-400 italic">
                                    Tidak ada foto
                                </span>

                                <?php } ?>

                            </td>

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

                                <?php

                        if($row['status']=='pending'){

                            echo '<span class="px-3 py-1 rounded-full text-xs font-semibold bg-yellow-100 text-yellow-700">
                            Pending
                            </span>';
                        }

                        elseif($row['status']=='disetujui'){

                            echo '<span class="px-3 py-1 rounded-full text-xs font-semibold bg-green-100 text-green-700">
                            Disetujui
                            </span>';
                        }

                        elseif($row['status']=='ditolak'){

                            echo '<span class="px-3 py-1 rounded-full text-xs font-semibold bg-red-100 text-red-700">
                            Ditolak
                            </span>';
                        }

                        elseif($row['status']=='diproses'){

                            echo '<span class="px-3 py-1 rounded-full text-xs font-semibold bg-blue-100 text-blue-700">
                            Diproses
                            </span>';
                        }

                        elseif($row['status']=='selesai'){

                            echo '<span class="px-3 py-1 rounded-full text-xs font-semibold bg-purple-100 text-purple-700">
                            Selesai
                            </span>';
                        }

                        ?>

                            </td>

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

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

                                    <?php if($row['status']=='pending'){ ?>

                                    <a href="approve.php?id=<?= $row['id'] ?>"
                                        onclick="return confirm('Setujui laporan ini?')"
                                        class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white rounded-lg text-sm transition">

                                        <i class="fas fa-check mr-1"></i>
                                        Approve

                                    </a>

                                    <a href="reject.php?id=<?= $row['id'] ?>"
                                        onclick="return confirm('Tolak laporan ini?')"
                                        class="px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg text-sm transition">

                                        <i class="fas fa-times mr-1"></i>
                                        Reject

                                    </a>

                                    <?php } ?>

                                    <?php if($row['status']=='disetujui'){ ?>

                                    <a href="assign_bantuan.php?id=<?= $row['id'] ?>"
                                        class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg text-sm transition">

                                        <i class="fas fa-hand-holding-heart mr-1"></i>
                                        Assign Bantuan

                                    </a>

                                    <?php } ?>

                                </div>

                            </td>

                        </tr>

                        <?php } ?>

                    </tbody>

                </table>

            </div>

        </div>


    </div>


</body>

</html>