Files
2026-06-09 19:02:22 +07:00

142 lines
5.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// ============================================================
// api_dashboard.php Data untuk Dashboard Walikota
// Endpoint statistik ringkasan, laporan terbaru, data heatmap
// ============================================================
require_once 'config.php';
$method = $_SERVER['REQUEST_METHOD'];
if ($method !== 'GET') {
http_response_code(405);
echo json_encode(['error' => 'Method tidak diizinkan']);
exit();
}
$action = isset($_GET['action']) ? $_GET['action'] : 'stats';
$db = getDB();
// ── STATISTIK RINGKASAN ────────────────────────────────────
if ($action === 'stats') {
$stats = [];
// Total per kategori kemiskinan
$result = $db->query("SELECT kategori, COUNT(*) as total,
SUM(CASE WHEN status_bantuan = 'Sudah' THEN 1 ELSE 0 END) as sudah_dibantu,
SUM(CASE WHEN status_bantuan != 'Sudah' OR status_bantuan IS NULL THEN 1 ELSE 0 END) as belum_dibantu
FROM data_kemiskinan GROUP BY kategori");
$kategori = [];
$total_warga = 0;
$total_sudah = 0;
$total_belum = 0;
while ($row = $result->fetch_assoc()) {
$row['total'] = (int)$row['total'];
$row['sudah_dibantu'] = (int)$row['sudah_dibantu'];
$row['belum_dibantu'] = (int)$row['belum_dibantu'];
$kategori[] = $row;
$total_warga += $row['total'];
$total_sudah += $row['sudah_dibantu'];
$total_belum += $row['belum_dibantu'];
}
$stats['kategori'] = $kategori;
$stats['total_warga'] = $total_warga;
$stats['total_sudah'] = $total_sudah;
$stats['total_belum'] = $total_belum;
$stats['persen_sudah'] = $total_warga > 0 ? round(($total_sudah / $total_warga) * 100, 1) : 0;
// Total rumah ibadah
$result = $db->query("SELECT jenis, COUNT(*) as total FROM data_masjid GROUP BY jenis");
$ibadah = [];
$total_ibadah = 0;
while ($row = $result->fetch_assoc()) {
$row['total'] = (int)$row['total'];
$ibadah[] = $row;
$total_ibadah += $row['total'];
}
$stats['rumah_ibadah'] = $ibadah;
$stats['total_ibadah'] = $total_ibadah;
// Total bantuan tercatat
$result = $db->query("SELECT COUNT(*) as total FROM riwayat_bantuan");
$row = $result->fetch_assoc();
$stats['total_bantuan'] = (int)$row['total'];
// Total laporan masyarakat per status
$result = $db->query("SELECT status, COUNT(*) as total FROM laporan_masyarakat GROUP BY status");
$laporan_status = [];
while ($row = $result->fetch_assoc()) {
$row['total'] = (int)$row['total'];
$laporan_status[] = $row;
}
$stats['laporan_status'] = $laporan_status;
// Penyakit terbanyak (aggregate)
$result = $db->query("SELECT riwayat_penyakit, COUNT(*) as total
FROM data_kemiskinan
WHERE riwayat_penyakit IS NOT NULL AND riwayat_penyakit != ''
GROUP BY riwayat_penyakit
ORDER BY total DESC LIMIT 10");
$penyakit = [];
while ($row = $result->fetch_assoc()) {
$row['total'] = (int)$row['total'];
$penyakit[] = $row;
}
$stats['penyakit_terbanyak'] = $penyakit;
echo json_encode($stats);
$db->close();
exit();
}
// ── LAPORAN TERBARU ────────────────────────────────────────
if ($action === 'laporan_terbaru') {
$limit = isset($_GET['limit']) ? min(50, max(1, (int)$_GET['limit'])) : 20;
$result = $db->query("SELECT id, nama_pelapor, nik, no_wa, deskripsi, latitude, longitude, status, alasan_tolak, created_at
FROM laporan_masyarakat ORDER BY created_at DESC LIMIT $limit");
$rows = [];
while ($row = $result->fetch_assoc()) {
$row['latitude'] = (float)$row['latitude'];
$row['longitude'] = (float)$row['longitude'];
$rows[] = $row;
}
echo json_encode($rows);
$db->close();
exit();
}
// ── DATA KEMISKINAN UNTUK TABEL ────────────────────────────
if ($action === 'kemiskinan') {
$result = $db->query("SELECT id, nama_kk, nik, kategori, status_bantuan, jumlah_kk, alamat, pendidikan, riwayat_penyakit, latitude, longitude
FROM data_kemiskinan ORDER BY id DESC");
$rows = [];
while ($row = $result->fetch_assoc()) {
$row['jumlah_kk'] = (int)$row['jumlah_kk'];
$row['latitude'] = (float)$row['latitude'];
$row['longitude'] = (float)$row['longitude'];
$rows[] = $row;
}
echo json_encode($rows);
$db->close();
exit();
}
// ── HEATMAP DATA ───────────────────────────────────────────
if ($action === 'heatmap') {
$result = $db->query("SELECT latitude, longitude, kategori FROM data_kemiskinan");
$points = [];
while ($row = $result->fetch_assoc()) {
$intensity = 0.5;
if ($row['kategori'] === 'Sangat Miskin') $intensity = 1.0;
else if ($row['kategori'] === 'Miskin') $intensity = 0.7;
$points[] = [(float)$row['latitude'], (float)$row['longitude'], $intensity];
}
echo json_encode($points);
$db->close();
exit();
}
http_response_code(400);
echo json_encode(['error' => 'Action tidak dikenali. Gunakan: stats, laporan_terbaru, kemiskinan, heatmap']);
?>