<?php
include '../koneksi.php';
header('Content-Type: application/json');

try {
    // 1. Fetch all fasilitas_publik
    $stmtFp = $pdo->query("SELECT * FROM fasilitas_publik ORDER BY id DESC");
    $fasilitasPublik = $stmtFp->fetchAll();

    // 2. Fetch all penduduk_miskin (dengan nama FP jika ada join)
    $stmtPm = $pdo->query("
        SELECT p.*, f.nama as fp_nama 
        FROM penduduk_miskin p 
        LEFT JOIN fasilitas_publik f ON p.fasilitas_publik_id = f.id 
        ORDER BY p.id DESC
    ");
    $pendudukMiskin = $stmtPm->fetchAll();

    echo json_encode([
        'status' => 'success',
        'fasilitasPublik' => $fasilitasPublik,
        'pendudukMiskin' => $pendudukMiskin
    ]);
} catch (PDOException $e) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Database error: ' . $e->getMessage()
    ]);
}
?>
