function loadRILayer(data) {
layers.rumahIbadah.clearLayers();
layers.ibadahRadius.clearLayers();
data.forEach(r => {
const circle = L.circle([r.latitude, r.longitude], {
radius: r.radius_meter,
color: '#f59e0b', fillColor: '#fef3c7', fillOpacity: 0.18,
weight: 1.5, dashArray: '6,4'
});
layers.ibadahRadius.addLayer(circle);
const icon = L.divIcon({
className: '',
html: `
โช
`,
iconSize: [32, 32], iconAnchor: [16, 16]
});
const marker = L.marker([r.latitude, r.longitude], {
icon,
draggable: (window.currentUser && window.currentUser.role === 'admin')
}).bindPopup(riPopup(r));
marker._dataId = r.id;
marker.on('dragend', async function (ev) {
const { lat, lng } = ev.target.getLatLng();
circle.setLatLng([lat, lng]);
const res = await callAPI(`rumah_ibadah.php?id=${r.id}`, 'PUT', {
nama: r.nama, jenis: r.jenis, alamat: r.alamat,
no_wa: r.no_wa, latitude: lat, longitude: lng,
radius_meter: r.radius_meter
});
if (res.status === 'success') {
r.latitude = lat;
r.longitude = lng;
toast(`๐ ${r.nama} dipindahkan & koordinat diperbarui`, 'success');
await recalcAllProximity();
switchTab(currentTab);
} else {
toast('Gagal update lokasi: ' + res.message, 'error');
marker.setLatLng([r.latitude, r.longitude]);
}
});
layers.rumahIbadah.addLayer(marker);
});
}
function riPopup(r) {
const canEdit = window.currentUser && (window.currentUser.role === 'admin' || (window.currentUser.role === 'petugas_lapangan' && String(window.currentUser.rumah_ibadah_id) === String(r.id)));
const isAdmin = window.currentUser && window.currentUser.role === 'admin';
return `โช ${esc(r.nama)}
Jenis: ${r.jenis}
Radius: ${r.radius_meter} m
${r.alamat ? `Alamat: ${esc(r.alamat)}
` : ''}
${r.no_wa ? `WA: ${r.no_wa}
` : ''}
`;
}
function loadPMLayer(data) {
layers.pendudukMiskin.clearLayers();
data.forEach(p => {
const inR = p.status_proximity === 'dalam_radius';
const color = inR ? '#16a34a' : '#dc2626';
const hasBoth = p.bantuan_pemberdayaan > 0 && p.bantuan_konsumtif > 0;
const hasPemb = p.bantuan_pemberdayaan > 0;
const hasKons = p.bantuan_konsumtif > 0;
let dot = '';
if (hasBoth || hasPemb) dot = ``;
if (hasKons && !hasPemb) dot = ``;
const icon = L.divIcon({
className: '',
html: ``,
iconSize: [26, 26], iconAnchor: [13, 13]
});
const isAdmin = window.currentUser && window.currentUser.role === 'admin';
const isPetugas = window.currentUser && window.currentUser.role === 'petugas_lapangan';
const isResidentInRadiusOfPetugas = isPetugas && inR && String(p.rumah_ibadah_id) === String(window.currentUser.rumah_ibadah_id);
const canDrag = isAdmin || isResidentInRadiusOfPetugas;
const marker = L.marker([p.latitude, p.longitude], {
icon,
draggable: canDrag
}).bindPopup(pmPopup(p));
marker._dataId = p.id;
marker.on('dragend', async function (ev) {
const { lat, lng } = ev.target.getLatLng();
const res = await callAPI(`penduduk_miskin.php?id=${p.id}`, 'PUT', {
nama: p.nama, no_wa: p.no_wa, nik: p.nik,
alamat: p.alamat, jumlah_anggota: p.jumlah_anggota,
latitude: lat, longitude: lng
});
if (res.status === 'success') {
const prox = res.data?.proximity;
const statusMsg = prox?.status === 'dalam_radius' ? 'โ
Dalam radius' : 'โ Luar radius';
toast(`๐ ${p.nama} dipindahkan โ ${statusMsg}`, 'success');
switchTab('penduduk_miskin');
} else {
toast('Gagal update: ' + res.message, 'error');
marker.setLatLng([p.latitude, p.longitude]);
}
});
layers.pendudukMiskin.addLayer(marker);
});
}
function pmPopup(p) {
const inR = p.status_proximity === 'dalam_radius';
const isAdmin = window.currentUser && window.currentUser.role === 'admin';
const isPetugas = window.currentUser && window.currentUser.role === 'petugas_lapangan';
const isResidentInRadiusOfPetugas = isPetugas && inR && String(p.rumah_ibadah_id) === String(window.currentUser.rumah_ibadah_id);
const canEditOrLog = isAdmin || isResidentInRadiusOfPetugas;
return `๐ค ${esc(p.nama)}
${inR ? `โ
Dalam radius โ ${esc(p.nama_rumah_ibadah || '')}` : 'โ Di luar radius rumah ibadah'}
${p.no_wa ? `WA: ${p.no_wa}
` : ''}
${p.nik ? `NIK: ${p.nik}
` : ''}
${p.alamat ? `Alamat: ${esc(p.alamat)}
` : ''}
Keluarga: ${p.jumlah_anggota} jiwa
Bantuan: ๐${p.bantuan_pemberdayaan} ยท ๐${p.bantuan_konsumtif}
`;
}