const appUrl = window.CMBNET_APP_URL || ((path) => `/${String(path || '').replace(/^\/+/, '')}`); const tenant = window.CMBNET_TENANT || ''; const apiBase = appUrl(`${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 importHouseNumbers(input) { const file = input.files[0]; if (!file) return; const formData = new FormData(); formData.append('file', file); fetch(`${apiBase}/hausnummern/import`, { method: 'POST', body: formData }).then(r => r.json()).then(data => { if (data.success) { alert(`${data.count} Hausnummern importiert`); location.reload(); } else { alert(data.error); } }).finally(() => { input.value = ''; }); } 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 = ` ${h.created_at} ${h.from_state || '-'} ${h.to_state} ${h.group_id || '-'} `; tbody.appendChild(tr); }); document.getElementById('streetHistoryModal').showModal(); }); } function revertStreet(histId) { if (!confirm('Zustand wirklich wiederherstellen?')) return; fetch(`${apiBase}/strassen/${currentStreetId}/history/${histId}/revert`, { method: 'POST' }).then(r => r.json()).then(data => { if (data.success) { alert('Wiederhergestellt'); location.reload(); } else { alert(data.error); } }); } function normalizeForFilter(text) { if (!text) return ''; let s = String(text).toLowerCase(); s = s.replace(/ä/g, 'ae') .replace(/ö/g, 'oe') .replace(/ü/g, 'ue') .replace(/ß/g, 'ss'); s = s.replace(/[-_]/g, ' '); s = s.replace(/\s+/g, ' ').trim(); return s; } function applyStreetFilters() { const nameInput = document.getElementById('streetNameFilter'); const stateInput = document.getElementById('streetStateFilter'); const groupInput = document.getElementById('streetGroupFilter'); const nameQuery = normalizeForFilter(nameInput ? nameInput.value : ''); const stateQuery = stateInput ? stateInput.value : ''; const groupQuery = groupInput ? groupInput.value : ''; document.querySelectorAll('#strassenTable tbody tr').forEach(row => { if (!row.dataset.id) return; const nameCell = row.cells[1]; const nameText = normalizeForFilter(nameCell ? nameCell.innerText : ''); const stateSelect = row.cells[2] ? row.cells[2].querySelector('select') : null; const groupSelect = row.cells[3] ? row.cells[3].querySelector('select') : null; const stateVal = stateSelect ? stateSelect.value : ''; const groupVal = groupSelect ? groupSelect.value : ''; let visible = true; if (nameQuery && nameText.indexOf(nameQuery) === -1) { visible = false; } if (visible && stateQuery && stateVal !== stateQuery) { visible = false; } if (visible && groupQuery && groupVal !== groupQuery) { visible = false; } row.style.display = visible ? '' : 'none'; }); } document.addEventListener('DOMContentLoaded', applyStreetFilters); function closeStreetHistoryModal() { document.getElementById('streetHistoryModal').close(); }