<?php

    if(isset($_GET['kode'])){
        $sql_cek = "SELECT * FROM tb_kk WHERE id_kk='".$_GET['kode']."'";
        $query_cek = mysqli_query($koneksi, $sql_cek);
		$data_cek = mysqli_fetch_array($query_cek,MYSQLI_BOTH);
		
		$karkel=$data_cek['id_kk'];
    }
?>


<div class="card card-primary">
	<div class="card-header">
		<h3 class="card-title">
			<i class="fa fa-users"></i> Riwayat Anggota KK Lengkap</h3>
	</div>
	
    <div class="card-body">
        <input type='hidden' class="form-control" id="id_kk" name="id_kk" value="<?php echo $data_cek['id_kk']; ?>"
            readonly/>

        <div class="form-group row">
            <label class="col-sm-2 col-form-label">No KK | KPl Keluarga</label>
            <div class="col-sm-4">
                <input type="text" class="form-control" id="no_kk" name="no_kk" value="<?php echo $data_cek['no_kk']; ?>"
                    readonly/>
            </div>
            <div class="col-sm-4">
                <input type="text" class="form-control" id="kepala" name="kepala" value="<?php echo $data_cek['kepala']; ?>"
                    readonly/>
            </div>
        </div>

        <div class="form-group row">
            <label class="col-sm-2 col-form-label">Alamat</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" value="<?php echo $data_cek['desa']; ?>, RT <?php echo $data_cek['rt']; ?> RW <?php echo $data_cek['rw']; ?> (<?php echo $data_cek['kec']; ?> - <?php echo $data_cek['kab']; ?> - <?php echo $data_cek['prov']; ?>)"
                    readonly/>
            </div>
        </div>

        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>NIK</th>
                            <th>Nama</th>
                            <th>Jekel</th>
                            <th>Hub Keluarga</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>

                        <?php
            $no = 1;
            // Query tanpa filter status='Ada' untuk melihat sejarah lengkap
            $sql = $koneksi->query("SELECT p.nik, p.nama, p.jekel, p.status, a.hubungan, a.id_anggota 
            from tb_pdd p inner join tb_anggota a on p.id_pend=a.id_pend where id_kk=$karkel");
            while ($data= $sql->fetch_assoc()) {
        ?>

                        <tr>
                            <td>
                                <?php echo $no++; ?>
                            </td>
                            <td>
                                <?php echo $data['nik']; ?>
                            </td>
                            <td>
                                <?php echo $data['nama']; ?>
                            </td>
                            <td>
                                <?php echo $data['jekel']; ?>
                            </td>
                            <td>
                                <?php echo $data['hubungan']; ?>
                            </td>
                            <td>
                                <?php 
                                    if($data['status']=='Ada'){
                                        echo "<span class='badge badge-success'>Ada</span>";
                                    }else if($data['status']=='Meninggal'){
                                        echo "<span class='badge badge-danger'>Meninggal</span>";
                                    }else{
                                        echo "<span class='badge badge-warning'>Pindah</span>";
                                    }
                                ?>
                            </td>
                        </tr>

                        <?php
            }
        ?>
                    </tbody>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
    <div class="card-footer">
        <a href="?page=anggota&kode=<?php echo $data_cek['id_kk']; ?>" title="Kembali" class="btn btn-warning">Kembali</a>
    </div>

</div>
