// --- Fitur Jalan --- const jalanColors = { 'Nasional': '#ff0000', // Merah 'Provinsi': '#0000ff', // Biru 'Kabupaten': '#00ff00' // Hijau }; function loadJalan() { jalanLayer.clearLayers(); fetch('api/jalan/read.php') .then(res => res.json()) .then(data => { if (data.status === 'success' && data.data) { data.data.forEach(item => { addJalanToMap(item); }); } if (window.refreshActivePanel) window.refreshActivePanel(); }); } function addJalanToMap(item) { // geom adalah GeoJSON {type: "LineString", coordinates: [[lng, lat], ...]} const latlngs = item.geom.coordinates.map(coord => [coord[1], coord[0]]); const polyline = L.polyline(latlngs, { color: jalanColors[item.status] || '#3388ff', weight: 5 }); polyline.jalanData = item; // Tampilkan label nama jalan sejajar dengan garis (diagonal) polyline.setText(item.nama, { center: true, offset: 0, attributes: { fill: '#000000', 'font-weight': 'bold', 'font-size': '14px', 'dy': '7' } }); // Hitung popupContent sekali saat jalan dibuat const d = item; const popupContent = `

Jalan ${d.nama}

Status: ${d.status}

Panjang: ${d.panjang.toFixed(2)} m

`; polyline.bindPopup(popupContent); jalanLayer.addLayer(polyline); } window.openEditJalanModal = function(id) { let d = null; jalanLayer.eachLayer(function(layer) { if (layer.jalanData && layer.jalanData.id == id) d = layer.jalanData; }); if (!d) return; const bodyHTML = `
`; map.closePopup(); openModal("Edit Jalan", bodyHTML, function() { window.saveEditJalan(d.id, 'editJalanNama', 'editJalanStatus'); }); }; window.saveEditJalan = function(id, namaId, statusId) { const nama = document.getElementById(namaId).value; const status = document.getElementById(statusId).value; fetch('api/jalan/update.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id, nama, status }) }) .then(res => res.json()) .then(data => { if(data.status === 'success') { map.closePopup(); loadJalan(); } else { alert(data.message); } }); }; window.deleteJalan = function(id) { openConfirmModal("Yakin hapus jalan ini?", function() { fetch('api/jalan/delete.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id }) }) .then(res => res.json()) .then(data => { if(data.status === 'success') { map.closePopup(); loadJalan(); } else { alert(data.message); } }); }); }; window.saveNewJalan = function(geoJsonStr, panjang, namaId, statusId) { const nama = document.getElementById(namaId).value; if (!nama) { alert("Nama jalan harus diisi!"); return false; } const status = document.getElementById(statusId).value; const geom = JSON.parse(geoJsonStr); fetch('api/jalan/create.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ nama, status, panjang, geom }) }) .then(res => res.json()) .then(data => { if(data.status === 'success') { closeModal(); loadJalan(); } else { alert(data.message); } }); return true; }; // Initial Load loadJalan();