const tenant = window.location.pathname.split('/')[1]; const apiBase = `/${tenant}/admin/api`; const GROUP_STATES = ['offen', 'unterwegs', 'online', 'pausierend', 'fertig']; let currentGroupId = null; function hasPosition(lat, lon) { return lat !== null && lat !== undefined && lon !== null && lon !== undefined; } function formatPosition(lat, lon) { return hasPosition(lat, lon) ? `${lat}, ${lon}` : 'unbekannt'; } function showError(data) { alert(data && data.error ? data.error : 'Aktion fehlgeschlagen'); } function makeButton(label, className, handler) { const button = document.createElement('button'); button.type = 'button'; button.textContent = label; if (className) button.className = className; button.addEventListener('click', handler); return button; } function updateGruppe(groupId, data) { fetch(`${apiBase}/gruppen/${groupId}`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }).then(r => r.json()).then(result => { if (!result.success) showError(result); }).catch(() => alert('Server nicht erreichbar')); } function deleteGruppe(groupId) { if (!confirm('Gruppe wirklich löschen?')) return; fetch(`${apiBase}/gruppen/${groupId}`, { method: 'DELETE' }).then(r => r.json()).then(data => { if (data.success) { location.reload(); } else { showError(data); } }).catch(() => alert('Server nicht erreichbar')); } // === Neue Gruppe === function openCreateGruppeModal() { document.getElementById('gruppeAlias').value = ''; document.getElementById('gruppeAccount').value = ''; document.getElementById('createGruppeModal').showModal(); } function closeCreateGruppeModal() { document.getElementById('createGruppeModal').close(); document.getElementById('gruppeAlias').value = ''; document.getElementById('gruppeAccount').value = ''; } function buildStateSelect(state) { const select = document.createElement('select'); GROUP_STATES.forEach(value => { const option = document.createElement('option'); option.value = value; option.textContent = value; option.selected = value === state; select.appendChild(option); }); return select; } function buildAccountSelect(accountId) { const select = document.createElement('select'); const source = document.getElementById('gruppeAccount'); Array.from(source.options).forEach(option => { const copy = document.createElement('option'); copy.value = option.value; copy.textContent = option.textContent; copy.selected = String(option.value) === String(accountId ?? ''); select.appendChild(copy); }); return select; } function addGruppeRow(gruppe) { const tbody = document.querySelector('#gruppenTable tbody'); const row = document.createElement('tr'); row.dataset.id = gruppe.id; const idCell = document.createElement('td'); idCell.textContent = gruppe.id; row.appendChild(idCell); const aliasCell = document.createElement('td'); const aliasInput = document.createElement('input'); aliasInput.type = 'text'; aliasInput.value = gruppe.aliasname || ''; aliasInput.addEventListener('change', () => updateGruppe(gruppe.id, {aliasname: aliasInput.value})); aliasCell.appendChild(aliasInput); row.appendChild(aliasCell); const stateCell = document.createElement('td'); const stateSelect = buildStateSelect(gruppe.state); stateSelect.addEventListener('change', () => updateGruppe(gruppe.id, {state: stateSelect.value})); stateCell.appendChild(stateSelect); row.appendChild(stateCell); const accountCell = document.createElement('td'); const accountSelect = buildAccountSelect(gruppe.account_id); accountSelect.addEventListener('change', () => updateGruppe(gruppe.id, {account_id: accountSelect.value})); accountCell.appendChild(accountSelect); row.appendChild(accountCell); const positionCell = document.createElement('td'); positionCell.className = 'group-position'; positionCell.textContent = formatPosition(gruppe.last_lat, gruppe.last_lon); row.appendChild(positionCell); const actionsCell = document.createElement('td'); actionsCell.appendChild(makeButton('Historie', '', () => showHistory(gruppe.id))); const clearPositionButton = makeButton('Position löschen', 'secondary delete-position', () => deleteGroupPosition(gruppe.id)); clearPositionButton.disabled = !hasPosition(gruppe.last_lat, gruppe.last_lon); actionsCell.appendChild(clearPositionButton); actionsCell.appendChild(makeButton('Löschen', 'secondary', () => deleteGruppe(gruppe.id))); row.appendChild(actionsCell); tbody.appendChild(row); row.scrollIntoView({block: 'nearest'}); } function createGruppe() { const aliasname = document.getElementById('gruppeAlias').value; const accountId = document.getElementById('gruppeAccount').value; fetch(`${apiBase}/gruppen`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({aliasname, account_id: accountId}) }).then(r => r.json()).then(data => { if (!data.success) { showError(data); return; } addGruppeRow({ id: data.id, aliasname: aliasname, account_id: accountId || null, state: 'offen', last_lat: null, last_lon: null }); closeCreateGruppeModal(); }).catch(() => alert('Server nicht erreichbar')); } // === Historie / Position === function setPositionCell(groupId, lat, lon) { const row = document.querySelector(`#gruppenTable tr[data-id="${groupId}"]`); if (!row) return; const positionCell = row.querySelector('.group-position'); if (positionCell) positionCell.textContent = formatPosition(lat, lon); const clearButton = row.querySelector('.delete-position'); if (clearButton) clearButton.disabled = !hasPosition(lat, lon); } function showHistory(groupId) { currentGroupId = groupId; fetch(`${apiBase}/gruppen/${groupId}/history`) .then(r => r.json()) .then(data => { const tbody = document.getElementById('historyBody'); tbody.innerHTML = ''; data.forEach(entry => { const row = document.createElement('tr'); [entry.created_at, entry.from_state || '-', entry.to_state, formatPosition(entry.last_lat, entry.last_lon)].forEach(value => { const cell = document.createElement('td'); cell.textContent = value; row.appendChild(cell); }); const actionCell = document.createElement('td'); actionCell.appendChild(makeButton('Zustand wiederherstellen', '', () => revertGroup(entry.id))); if (hasPosition(entry.last_lat, entry.last_lon)) { actionCell.appendChild(makeButton('Position setzen', 'secondary', () => setPositionFromHistory(entry.id, entry.last_lat, entry.last_lon))); } row.appendChild(actionCell); tbody.appendChild(row); }); document.getElementById('historyModal').showModal(); }) .catch(() => alert('Historie konnte nicht geladen werden')); } function setPositionFromHistory(histId, lat, lon) { if (!confirm('Position aus diesem Historieneintrag übernehmen?')) return; fetch(`${apiBase}/gruppen/${currentGroupId}/position/history/${histId}`, { method: 'POST' }).then(r => r.json()).then(data => { if (!data.success) { showError(data); return; } setPositionCell(currentGroupId, lat, lon); closeHistoryModal(); }).catch(() => alert('Server nicht erreichbar')); } function deleteGroupPosition(groupId) { if (!confirm('Position dieser Gruppe wirklich löschen?')) return; fetch(`${apiBase}/gruppen/${groupId}/position`, { method: 'DELETE' }).then(r => r.json()).then(data => { if (!data.success) { showError(data); return; } setPositionCell(groupId, null, null); }).catch(() => alert('Server nicht erreichbar')); } function revertGroup(histId) { if (!confirm('Zustand wirklich wiederherstellen?')) return; fetch(`${apiBase}/gruppen/${currentGroupId}/history/${histId}/revert`, { method: 'POST' }).then(r => r.json()).then(data => { if (data.success) { location.reload(); } else { showError(data); } }).catch(() => alert('Server nicht erreichbar')); } function closeHistoryModal() { document.getElementById('historyModal').close(); }