import { initMap } from './modules/map.js'; import { setupDrawControls } from './modules/draw.js'; import { renderForm } from './modules/form.js'; import { spbuService, jalanService, kavlingService } from './services/api.service.js'; let appMap; let drawControl; // Custom SVG Icons Generator export const createIcon = (svgPath, color) => { return L.divIcon({ className: 'custom-icon', html: `
${svgPath}
`, iconSize: [34, 34], iconAnchor: [17, 34], popupAnchor: [0, -34] }); }; export const iconSPBU = (is24h) => createIcon('', is24h ? '#10B981' : '#EF4444'); window.showToast = (msg, type='success') => { const t = document.createElement('div'); t.className = `toast ${type}`; t.innerHTML = msg; document.getElementById('toast-container').appendChild(t); setTimeout(() => { t.style.transform='translateX(100%)'; t.style.opacity=0; setTimeout(()=>t.remove(),300); }, 3000); }; document.addEventListener('DOMContentLoaded', async () => { appMap = initMap('map'); drawControl = setupDrawControls(appMap, handleGeometryCreated); await loadAllData(); }); const loadAllData = async () => { try { const spbu = await spbuService.getAll(); L.geoJSON(spbu, { pointToLayer: (f, latlng) => L.marker(latlng, { icon: iconSPBU(f.properties.buka_24_jam) }), onEachFeature: (f, l) => bindPopup(l, 'spbu', f.properties) }).addTo(appMap); const jalan = await jalanService.getAll(); L.geoJSON(jalan, { style: { color: '#F59E0B', weight: 4 }, onEachFeature: (f, l) => bindPopup(l, 'jalan', f.properties) }).addTo(appMap); const kavling = await kavlingService.getAll(); L.geoJSON(kavling, { style: { color: '#3B82F6', weight: 2, fillColor: '#3B82F6', fillOpacity: 0.3 }, onEachFeature: (f, l) => bindPopup(l, 'kavling', f.properties) }).addTo(appMap); } catch (e) { window.showToast("Gagal meload data: "+e.message, 'error'); } }; const bindPopup = (layer, type, props) => { const ext = props.buka_24_jam !== undefined ? `

Buka 24 Jam: ${props.buka_24_jam ? 'Ya' : 'Tidak'}

` : ''; layer.bindPopup(` `); }; window.deleteData = async (type, id) => { if(!confirm('Yakin ingin menghapus?')) return; try { if(type==='spbu') await spbuService.delete(id); if(type==='jalan') await jalanService.delete(id); if(type==='kavling') await kavlingService.delete(id); window.showToast('Data dihapus'); setTimeout(()=>location.reload(), 800); } catch(e) { window.showToast('Gagal hapus', 'error'); } }; const handleGeometryCreated = (type, geometry, layer) => { const tempLayer = L.geoJSON(geometry).addTo(appMap); renderForm(type, geometry, async (payload) => { try { if(type==='spbu') await spbuService.create(payload); if(type==='jalan') await jalanService.create(payload); if(type==='kavling') await kavlingService.create(payload); window.showToast('Berhasil disimpan'); setTimeout(()=>location.reload(), 800); } catch(e) { window.showToast('Gagal simpan', 'error'); } }, () => { appMap.removeLayer(tempLayer); }); };