const tenant = window.location.pathname.split('/')[1]; const apiBase = `/${tenant}/admin/api`; let currentStreetId = null; let editMode = false; function updateStrasse(streetId, data) { fetch(`${apiBase}/strassen/${streetId}`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }).then(r => r.json()).then(data => { if (data.success) { location.reload(); } }); } function deleteStrasse(streetId) { if (!confirm('Straße wirklich löschen?')) return; fetch(`${apiBase}/strassen/${streetId}`, { method: 'DELETE' }).then(r => r.json()).then(data => { if (data.success) location.reload(); }); } function openCreateStrasseModal() { editMode = false; document.getElementById('strasseModalTitle').innerText = 'Neue Straße'; document.getElementById('strasseId').value = ''; document.getElementById('strasseName').value = ''; document.getElementById('strasseHnMin').value = ''; document.getElementById('strasseHnMax').value = ''; document.getElementById('strasseBbox').value = ''; document.getElementById('strasseState').value = 'unbearbeitet'; document.getElementById('strasseState').disabled = true; document.getElementById('strasseGroup').value = ''; document.getElementById('strasseGroup').disabled = true; document.getElementById('strasseModal').showModal(); } function editStrasse(streetId) { editMode = true; const row = document.querySelector(`tr[data-id="${streetId}"]`); document.getElementById('strasseModalTitle').innerText = 'Straße bearbeiten'; document.getElementById('strasseId').value = streetId; document.getElementById('strasseName').value = row.cells[1].innerText; document.getElementById('strasseHnMin').value = row.cells[4].innerText === '-' ? '' : row.cells[4].innerText; document.getElementById('strasseHnMax').value = row.cells[5].innerText === '-' ? '' : row.cells[5].innerText; document.getElementById('strasseBbox').value = row.dataset.bbox || ''; document.getElementById('strasseState').value = row.cells[2].querySelector('select').value; document.getElementById('strasseState').disabled = false; document.getElementById('strasseGroup').value = row.cells[3].querySelector('select').value; document.getElementById('strasseGroup').disabled = false; document.getElementById('strasseModal').showModal(); } function closeStrasseModal() { document.getElementById('strasseModal').close(); } function saveStrasse() { const id = document.getElementById('strasseId').value; const data = { name: document.getElementById('strasseName').value, hn_min: document.getElementById('strasseHnMin').value || null, hn_max: document.getElementById('strasseHnMax').value || null, bbox: document.getElementById('strasseBbox').value || null, state: document.getElementById('strasseState').value, group_id: document.getElementById('strasseGroup').value || null }; if (!editMode) { fetch(`${apiBase}/strassen`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }).then(r => r.json()).then(data => { if (data.success) { alert('Straße angelegt'); location.reload(); } }); } else { fetch(`${apiBase}/strassen/${id}`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }).then(r => r.json()).then(data => { if (data.success) { alert('Gespeichert'); location.reload(); } }); } closeStrasseModal(); } function importCSV(input) { const file = input.files[0]; if (!file) return; const formData = new FormData(); formData.append('file', file); fetch(`${apiBase}/strassen/import`, { method: 'POST', body: formData }).then(r => r.json()).then(data => { if (data.success) { alert(`${data.count} Straßen importiert`); location.reload(); } else { alert(data.error); } }); } function showStreetHistory(streetId) { currentStreetId = streetId; fetch(`${apiBase}/strassen/${streetId}/history`) .then(r => r.json()) .then(data => { const tbody = document.getElementById('streetHistoryBody'); tbody.innerHTML = ''; data.forEach(h => { const tr = document.createElement('tr'); tr.innerHTML = `