<?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';

$id = intval($_GET['id']);

$laporan = mysqli_fetch_assoc(
mysqli_query($conn,
"SELECT * FROM laporan
WHERE id='$id'")
);

if(!$laporan){
    die("Data laporan tidak ditemukan");
}

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

$cs = mysqli_query($conn,
"SELECT * FROM cs_rumah_ibadah");

$cek_bantuan = mysqli_query(
$conn,
"SELECT * FROM bantuan
WHERE laporan_id='$id'"
);

if(mysqli_num_rows($cek_bantuan)>0){

    echo "
    <script>
    alert('Laporan sudah pernah diassign');
    window.location='validasi.php';
    </script>
    ";

    exit;
}
?>

<!DOCTYPE html>
<html>

<head>

    <title>Assign Bantuan</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">
                Assign Bantuan
            </h1>

            <p class="text-gray-500 mt-2">
                Tentukan rumah ibadah, CS penyalur, dan jenis bantuan untuk laporan yang telah disetujui.
            </p>

        </div>

        <div class="grid lg:grid-cols-3 gap-6">

            <!-- Informasi Warga -->

            <div class="lg:col-span-1">

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

                    <div class="p-5 border-b bg-blue-50">

                        <h2 class="font-semibold text-lg flex items-center gap-2">

                            <i class="fas fa-user text-blue-600"></i>

                            Data Warga

                        </h2>

                    </div>

                    <div class="p-5">

                        <div class="mb-4">

                            <label class="text-gray-500 text-sm">
                                Nama Warga
                            </label>

                            <p class="font-semibold text-lg">
                                <?= htmlspecialchars($laporan['nama_warga']) ?>
                            </p>

                        </div>

                        <div class="mb-4">

                            <label class="text-gray-500 text-sm">
                                Alamat
                            </label>

                            <p>
                                <?= htmlspecialchars($laporan['alamat']) ?>
                            </p>

                        </div>

                        <div class="mb-4">

                            <label class="text-gray-500 text-sm">
                                Kondisi
                            </label>

                            <p>
                                <?= htmlspecialchars($laporan['kondisi']) ?>
                            </p>

                        </div>

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

                        <img src="../assets/uploads/<?= htmlspecialchars($laporan['foto']) ?>"
                            class="w-full h-56 object-cover rounded-xl shadow">

                        <?php } ?>

                    </div>

                </div>

            </div>

            <!-- Form Assign -->

            <div class="lg:col-span-2">

                <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 flex items-center gap-2">

                            <i class="fas fa-hand-holding-heart text-green-600"></i>

                            Form Penugasan Bantuan

                        </h2>

                    </div>

                    <form action="proses_assign.php" method="POST" class="p-6 space-y-5">

                        <input type="hidden" name="laporan_id" value="<?= $laporan['id'] ?>">

                        <div>

                            <label class="block text-sm font-medium text-gray-700 mb-2">
                                Nama Warga
                            </label>

                            <input type="text" readonly value="<?= htmlspecialchars($laporan['nama_warga']) ?>"
                                class="w-full border rounded-xl p-3 bg-gray-100">

                        </div>

                        <div>

                            <label class="block text-sm font-medium text-gray-700 mb-2">
                                Rumah Ibadah
                            </label>

                            <select name="rumah_ibadah_id" required
                                class="w-full border rounded-xl p-3 focus:ring-2 focus:ring-blue-500 focus:outline-none">

                                <option value="">
                                    -- Pilih Rumah Ibadah --
                                </option>

                                <?php while($r = mysqli_fetch_assoc($rumah_ibadah)){ ?>

                                <option value="<?= $r['id'] ?>">

                                    <?= htmlspecialchars($r['nama']) ?>

                                </option>

                                <?php } ?>

                            </select>

                        </div>

                        <div>

                            <label class="block text-sm font-medium text-gray-700 mb-2">
                                CS Penyalur
                            </label>

                            <select name="cs_id" required
                                class="w-full border rounded-xl p-3 focus:ring-2 focus:ring-blue-500 focus:outline-none">

                                <option value="">
                                    -- Pilih CS --
                                </option>

                                <?php while($c = mysqli_fetch_assoc($cs)){ ?>

                                <option value="<?= $c['id'] ?>">

                                    <?= htmlspecialchars($c['nama']) ?>

                                </option>

                                <?php } ?>

                            </select>

                        </div>

                        <div>


                            <label class="block text-sm font-medium text-gray-700 mb-2">
                                Jenis Bantuan
                            </label>

                            <select name="jenis_bantuan" required
                                class="w-full border rounded-xl p-3 focus:ring-2 focus:ring-blue-500 focus:outline-none">

                                <option value="">
                                    -- Pilih Jenis Bantuan --
                                </option>

                                <option value="Sembako">
                                    🍚 Sembako
                                </option>

                                <option value="Uang Tunai">
                                    💵 Uang Tunai
                                </option>

                                <option value="Pendidikan">
                                    🎓 Pendidikan
                                </option>

                                <option value="Kesehatan">
                                    🏥 Kesehatan
                                </option>

                                <option value="Peralatan Sekolah">
                                    🎒 Peralatan Sekolah
                                </option>

                                <option value="Perbaikan Rumah">
                                    🏠 Perbaikan Rumah
                                </option>

                                <option value="Pakaian Layak">
                                    👕 Pakaian Layak
                                </option>

                                <option value="Makanan Bergizi">
                                    🍱 Makanan Bergizi
                                </option>

                                <option value="Modal Usaha">
                                    💼 Modal Usaha
                                </option>

                                <option value="Lainnya">
                                    📦 Lainnya
                                </option>

                            </select>

                        </div>


                        <div class="flex flex-wrap gap-3 pt-3">

                            <button type="submit"
                                class="px-6 py-3 bg-green-600 hover:bg-green-700 text-white rounded-xl shadow transition">

                                <i class="fas fa-check-circle mr-2"></i>

                                Assign Bantuan

                            </button>

                            <a href="validasi.php"
                                class="px-6 py-3 bg-gray-500 hover:bg-gray-600 text-white rounded-xl shadow transition">

                                <i class="fas fa-arrow-left mr-2"></i>

                                Kembali

                            </a>

                        </div>

                    </form>

                </div>

            </div>

        </div>


    </div>


</body>

</html>