<?php
    include "../inc/koneksi.php";
    $tanggal = date("d/m/y");

    // Ambil Data Profil
    $sql_profil = "SELECT * FROM tb_profil WHERE id_profil='1'";
    $query_profil = mysqli_query($koneksi, $sql_profil);
    $data_profil = mysqli_fetch_array($query_profil,MYSQLI_BOTH);

    $kategori = $_POST['kategori'];
    $label = "SEMUA KATEGORI";

    // Build Query with TIMESTAMPDIFF for accurate age calculation
    $sql_tampil = "SELECT *, TIMESTAMPDIFF(YEAR, tgl_lh, CURDATE()) AS usia FROM tb_pdd";
    
    if($kategori == "Balita") {
        $sql_tampil .= " WHERE TIMESTAMPDIFF(YEAR, tgl_lh, CURDATE()) BETWEEN 0 AND 5";
        $label = "KATEGORI BALITA (0-5 TAHUN)";
    } elseif ($kategori == "Anak") {
        $sql_tampil .= " WHERE TIMESTAMPDIFF(YEAR, tgl_lh, CURDATE()) BETWEEN 6 AND 12";
        $label = "KATEGORI ANAK-ANAK (6-12 TAHUN)";
    } elseif ($kategori == "Remaja") {
        $sql_tampil .= " WHERE TIMESTAMPDIFF(YEAR, tgl_lh, CURDATE()) BETWEEN 13 AND 17";
        $label = "KATEGORI REMAJA (13-17 TAHUN)";
    } elseif ($kategori == "Dewasa") {
        $sql_tampil .= " WHERE TIMESTAMPDIFF(YEAR, tgl_lh, CURDATE()) BETWEEN 18 AND 59";
        $label = "KATEGORI DEWASA (18-59 TAHUN)";
    } elseif ($kategori == "Lansia") {
        $sql_tampil .= " WHERE TIMESTAMPDIFF(YEAR, tgl_lh, CURDATE()) >= 60";
        $label = "KATEGORI LANSIA (> 60 TAHUN)";
    }

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>LAPORAN DATA PENDUDUK BERDASARKAN UMUR</title>
</head>
<body>
    <center>
        <h2>PEMERINTAH KABUPATEN <?php echo strtoupper($data_profil['kabupaten']); ?></h2>
        <h3>KECAMATAN <?php echo strtoupper($data_profil['kecamatan']); ?> <br> DESA <?php echo strtoupper($data_profil['nama_desa']); ?></h3>
        <p><?php echo $data_profil['alamat']; ?></p>
        <p>________________________________________________________________________</p>
        <h3>LAPORAN DATA PENDUDUK</h3>
        <h4><?php echo $label; ?></h4>
        <p align="left">Dicetak Tanggal : <?php echo $tanggal; ?></p>
    </center>

    <table border="1" cellspacing="0" style="width: 100%">
        <thead>
            <tr>
                <th>No</th>
                <th>NIK</th>
                <th>Nama</th>
                <th>Tgl Lahir</th>
                <th>Usia</th>
                <th>JK</th>
                <th>Alamat</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $no=1;
            $query_tampil = mysqli_query($koneksi, $sql_tampil);
            while ($data = mysqli_fetch_array($query_tampil,MYSQLI_BOTH)) {
            ?>
            <tr>
                <td align="center"><?php echo $no++; ?></td>
                <td>'<?php echo $data['nik']; ?></td>
                <td><?php echo $data['nama']; ?></td>
                <td align="center"><?php echo date("d/m/Y", strtotime($data['tgl_lh'])); ?></td>
                <td align="center"><?php echo $data['usia']; ?> Thn</td>
                <td align="center"><?php echo $data['jekel']; ?></td>
                <td><?php echo $data['desa']; ?>, RT <?php echo $data['rt']; ?>/ RW <?php echo $data['rw']; ?></td>
            </tr>
            <?php } ?>
        </tbody>
    </table>

    <script>
        window.print();
    </script>
</body>
</html>
