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

try {
    $totalFP  = $pdo->query("SELECT COUNT(*) FROM fasilitas_publik")->fetchColumn();
    $totalPM  = $pdo->query("SELECT COUNT(*) FROM penduduk_miskin")->fetchColumn();
    $ditangani= $pdo->query("SELECT COUNT(*) FROM penduduk_miskin WHERE status_jangkauan='Dalam Jangkauan'")->fetchColumn();
    $belum    = $totalPM - $ditangani;
    $persen   = $totalPM > 0 ? round(($ditangani / $totalPM) * 100, 1) : 0;

    echo json_encode([
        'status'         => 'success',
        'total_ri'       => (int)$totalFP,
        'total_pm'       => (int)$totalPM,
        'ditangani'      => (int)$ditangani,
        'belum_ditangani'=> (int)$belum,
        'persentase'     => $persen,
    ]);
} catch (PDOException $e) {
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>
