/* ========================================================= skpl-map.js โ€” Peta SKPL (operator & pimpinan) Warga miskin (POI) + fasilitas publik + choropleth + heatmap (choropleth/heatmap/legenda/pencarian via attachSkplLayers). Role ditentukan global SKPL_ROLE ('operator' | 'pimpinan'). ========================================================= */ (function () { const API = APP_BASE + '/api'; const ROLE = (typeof SKPL_ROLE !== 'undefined') ? SKPL_ROLE : 'pimpinan'; let map, wargaLayer, fasilitasLayer; const FAS_ICON = { sekolah: '๐Ÿซ', puskesmas: '๐Ÿฅ', pasar: '๐Ÿช', kantor_kelurahan: '๐Ÿ›๏ธ', rumah_ibadah: '๐Ÿ•Œ', lainnya: '๐Ÿ“' }; function divIcon(emoji, bg, size = 30) { return L.divIcon({ className: 'div-icon-wrap', html: `
${emoji}
`, iconSize: [size, size], iconAnchor: [size / 2, size], popupAnchor: [0, -size], }); } const wargaIcon = divIcon('๐Ÿ‘ค', '#EF4444'); function wargaPopup(p) { const vb = { terverifikasi: '#10B981', perlu_tinjauan: '#F59E0B', belum: '#9CA3AF' }[p.status_verifikasi] || '#9CA3AF'; let h = `
${p.nama_kk}
`; if (p.nik_kk) h += `
NIK: ${p.nik_kk}
`; if (p.alamat) h += `
${p.alamat}
`; h += `
Penghasilan: Rp ${Number(p.penghasilan||0).toLocaleString('id-ID')}
`; h += `
Jiwa: ${p.jumlah_jiwa||'-'} ยท Tanggungan: ${p.jumlah_tanggungan||0}
`; h += `
Air: ${p.sumber_air||'-'} ยท Dinding: ${p.material_dinding||'-'}
`; h += `
Bantuan: ${p.bantuan_aktif||'Belum ada'}
`; h += `
Status: ${p.status_verifikasi||'belum'}${p.jumlah_foto?' ยท ๐Ÿ“ท '+p.jumlah_foto:''}
`; if (ROLE === 'operator') { h += `
`; } return h + `
`; } async function loadWarga() { const j = await (await fetch(`${API}/warga_miskin.php`)).json(); const data = j.status === 'success' ? j.data : { features: [] }; wargaLayer = L.geoJSON(data, { pointToLayer: (f, ll) => L.marker(ll, { icon: wargaIcon }), onEachFeature: (f, l) => l.bindPopup(wargaPopup(f.properties)), }).addTo(map); } async function loadFasilitas() { const j = await (await fetch(`${API}/fasilitas.php`)).json(); const data = j.status === 'success' ? j.data : { features: [] }; fasilitasLayer = L.geoJSON(data, { pointToLayer: (f, ll) => L.marker(ll, { icon: divIcon(FAS_ICON[f.properties.jenis] || '๐Ÿ“', '#2563EB', 26) }), onEachFeature: (f, l) => l.bindPopup(`${f.properties.nama}
${f.properties.jenis.replace('_',' ')}`), }); } window.SKPLMAP_del = async function (id) { if (!confirm('Hapus data warga ini?')) return; const r = await (await fetch(`${API}/warga_miskin.php?id=${id}`, { method: 'DELETE' })).json(); if (r.status === 'success') { map.removeLayer(wargaLayer); loadWarga(); } else alert(r.message || 'Gagal'); }; document.addEventListener('DOMContentLoaded', async () => { map = L.map('skpl-map').setView([-0.0500, 109.3450], 13); L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', { subdomains: 'abcd', maxZoom: 19, attribution: 'ยฉ OpenStreetMap, ยฉ CARTO' }).addTo(map); await loadWarga(); await loadFasilitas(); if (window.attachSkplLayers) await window.attachSkplLayers(map); // tambahkan toggle warga & fasilitas L.control.layers(null, { '๐Ÿ‘ค Warga Miskin': wargaLayer, '๐Ÿข Fasilitas Publik': fasilitasLayer }, { collapsed: true, position: 'topright' }).addTo(map); }); })();