<?php
include("home_top.php");

?>

<div class="container mt-5">
    <h2 class="text-center">Cari Data Penduduk</h2>
    <form method="GET" action="">
        <div class="input-group mb-3">
            <input type="text" class="form-control" placeholder="Masukkan kata kunci..." name="keyword">
            <button class="btn btn-primary" type="submit">Cari</button>
        </div>
        <p class="text-danger">Diharapkan catat id penduduk untuk mengajukan permohonan</p>
    </form>

    <?php
    if(isset($_GET['keyword'])) {
        $keyword = $_GET['keyword'];

        // Koneksi ke database
        $koneksi = mysqli_connect("localhost", "root", "", "pogar");

        // Periksa koneksi
        if (mysqli_connect_errno()) {
            echo "Koneksi database gagal: " . mysqli_connect_error();
            exit();
        }

        // Query SQL untuk mencari data
        $query = "SELECT * FROM penduduk WHERE nik LIKE '%$keyword%' OR nama_lengkap LIKE '%$keyword%' OR pekerjaan LIKE '%$keyword%'";
        $result = mysqli_query($koneksi, $query);

        // Cek apakah ada hasil pencarian
        if(mysqli_num_rows($result) > 0) {
            echo "<h3>Hasil Pencarian:</h3>";
            echo "<div class='table-responsive'>";
            echo "<table class='table table-striped'>";
            echo "<thead>
                    <tr>
                        <th>ID</th>
                        <th>Nik</th>
                        <th>Nama Lengkap</th>
                        <th>Kepala Keluarga</th>
                        <th>Pekerjaan</th>
                        
                    </tr>
                  </thead>";
            echo "<tbody>";

            // Tampilkan data hasil pencarian
            while ($row = mysqli_fetch_assoc($result)) {
                echo "<tr>";
                echo "<td>" . $row['id_penduduk'] . "</td>";
                echo "<td>" . $row['nik'] . "</td>";
                echo "<td>" . $row['nama_lengkap'] . "</td>";
                echo "<td>" . $row['kepala_keluarga'] . "</td>";
                echo "<td>" . $row['pekerjaan'] . "</td>";
                echo "<td><button";
                echo "</tr>";
            }

            echo "</tbody>";
            echo "</table>";
            echo "</div>";
        } else {
            echo "<p>Data tidak ditemukan.silahkan memasukan identitas penduduk melalui menu Register Penduduk</p>";
           
        }

        // Tutup koneksi database
        mysqli_close($koneksi);
    }
    ?>
</div>


<?php
include("home_bottom.php");
?>