`,
iconSize: [38, 38],
iconAnchor: [19, 38],
popupAnchor: [0, -40]
});
}
const spbuGreenIcon = makeSpbuIcon(true);
const spbuRedIcon = makeSpbuIcon(false);
// Fetch SPBU
function loadSpbu() {
spbuLayer.clearLayers();
fetch('../spbu/read.php')
.then(res => res.json())
.then(data => {
if (data.status === 'success' && data.data) {
data.data.forEach(item => {
addSpbuMarker(item);
});
}
if (window.refreshActivePanel) window.refreshActivePanel();
});
}
function addSpbuMarker(item) {
const isAdmin = !!(window.currentUser && window.currentUser.role === 'admin');
const icon = makeSpbuIcon(item.is_24_jam);
const marker = L.marker([item.lat, item.lng], {
icon: icon,
draggable: isAdmin
});
marker.spbuData = item; // Simpan data di objek marker
// Hitung popupContent sekali saat marker dibuat
const d = item;
let actionButtons = '';
if (isAdmin) {
actionButtons = `
`;
}
const popupContent = `
SPBU ${d.nama}
No. WA: ${d.no_wa}
Buka 24 Jam: ${d.is_24_jam ? 'Ya' : 'Tidak'}
${actionButtons}
`;
marker.bindPopup(popupContent);
// Event drag untuk update koordinat
marker.on('dragend', function(e) {
const newPos = marker.getLatLng();
updateSpbu(item.id, item.nama, item.no_wa, item.is_24_jam, newPos.lat, newPos.lng);
});
spbuLayer.addLayer(marker);
}
// Map Click -> Form Add
map.on('click', function(e) {
// Jangan muncul form jika sedang dalam draw mode (jalan/parsil)
if (window.currentDrawMode) return;
// Hanya proses jika mode tambah SPBU aktif
if (window.currentAddMode === 'spbu') {
const bodyHTML = `