<?php
require_once "conn.php";

$stmt = $conn->prepare("SELECT t1.pamong_id, IFNULL(t1.pamong_nama, t2.nama) AS pamong_nama, t1.gelar_depan, t1.gelar_belakang, IFNULL(t1.foto, t2.foto) AS foto, t3.nama, t1.id_pend
                        FROM tweb_desa_pamong AS t1
                        LEFT JOIN tweb_penduduk AS t2 ON t1.id_pend = t2.id
                        LEFT JOIN ref_jabatan AS t3 ON t1.jabatan_id = t3.id");

if ($stmt === false) {
    die('Query preparation failed: ' . $conn->error);
}

if ($stmt->execute() === false) {
    die('Query execution failed: ' . $stmt->error);
}

$stmt->bind_result($pamong_id, $pamong_nama, $gelar_depan, $gelar_belakang, $foto, $nama_jabatan, $id_pend);

$tweb_desa_pamong = array();

while ($stmt->fetch()) {
    $temp = array();
    $temp['pamong_id'] = $pamong_id;
    $temp['pamong_nama'] = $pamong_nama;
    $temp['gelar_depan'] = $gelar_depan;
    $temp['gelar_belakang'] = $gelar_belakang;
    $temp['foto'] = $foto;
    $temp['jabatan'] = $nama_jabatan;
    $temp['id_pend'] = $id_pend;

    array_push($tweb_desa_pamong, $temp);
}

echo json_encode($tweb_desa_pamong);
?>
