// JavaScript für Medi-WOL Web-Oberfläche class PCManager { constructor() { this.allPCs = []; // Alle PCs im Speicher this.filteredPCs = []; // Gefilterte PCs this.searchTerm = ''; // Aktueller Suchbegriff this.init(); } init() { this.loadPCs(); this.setupEventListeners(); } setupEventListeners() { // Formular für neuen PC document.getElementById('addPCForm').addEventListener('submit', (e) => { e.preventDefault(); this.addPC(); }); // Edit PC Modal speichern Button document.getElementById('saveEditBtn').addEventListener('click', () => { this.saveEditPC(); }); // MAC-Adresse Formatierung für Edit-Formular document.getElementById('editMACAddress')?.addEventListener('input', (e) => { this.formatMACAddress(e.target); }); // Suchfeld Event Listener document.getElementById('searchInput').addEventListener('input', (e) => { this.searchPCs(e.target.value); }); // Suchfeld löschen Button document.getElementById('clearSearchBtn').addEventListener('click', () => { this.clearSearch(); }); } async loadPCs() { try { const response = await fetch('/api/pcs'); const data = await response.json(); if (data.success) { this.allPCs = data.pcs; this.filteredPCs = [...this.allPCs]; this.displayPCs(); } else { this.showNotification('Fehler', data.message, 'danger'); } } catch (error) { this.showNotification('Fehler', 'Fehler beim Laden der PCs', 'danger'); } } searchPCs(searchTerm) { this.searchTerm = searchTerm.toLowerCase().trim(); if (this.searchTerm === '') { // Keine Suche - alle PCs anzeigen this.filteredPCs = [...this.allPCs]; this.hideClearSearchButton(); } else { // PCs nach Namen filtern this.filteredPCs = this.allPCs.filter(pc => pc.name.toLowerCase().includes(this.searchTerm) ); this.showClearSearchButton(); } this.displayPCs(); } clearSearch() { document.getElementById('searchInput').value = ''; this.searchPCs(''); this.hideClearSearchButton(); } showClearSearchButton() { document.getElementById('clearSearchBtn').style.display = 'block'; } hideClearSearchButton() { document.getElementById('clearSearchBtn').style.display = 'none'; } displayPCs() { const tableBody = document.getElementById('pcTableBody'); const noPCs = document.getElementById('noPCs'); const pcList = document.getElementById('pcList'); const noSearchResults = document.getElementById('noSearchResults'); const searchTerm = document.getElementById('searchTerm'); const resultCount = document.getElementById('resultCount'); // Ergebnis-Zähler aktualisieren resultCount.textContent = `${this.filteredPCs.length} PC${this.filteredPCs.length !== 1 ? 's' : ''} gefunden`; if (this.allPCs.length === 0) { // Keine PCs vorhanden tableBody.innerHTML = ''; noPCs.style.display = 'block'; pcList.style.display = 'none'; noSearchResults.style.display = 'none'; return; } if (this.filteredPCs.length === 0 && this.searchTerm !== '') { // Keine Suchergebnisse tableBody.innerHTML = ''; noPCs.style.display = 'none'; pcList.style.display = 'none'; noSearchResults.style.display = 'block'; searchTerm.textContent = this.searchTerm; return; } // PCs anzeigen noPCs.style.display = 'none'; noSearchResults.style.display = 'none'; pcList.style.display = 'block'; tableBody.innerHTML = this.filteredPCs.map(pc => ` ${this.escapeHtml(pc.name)} ${this.escapeHtml(pc.mac)} ${new Date(pc.created_at).toLocaleDateString('de-DE')}
`).join(''); } async addPC() { const name = document.getElementById('pcName').value.trim(); const mac = document.getElementById('macAddress').value.trim(); if (!name || !mac) { this.showNotification('Warnung', 'Bitte füllen Sie alle Felder aus', 'warning'); return; } try { const response = await fetch('/api/pcs', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name, mac }) }); const data = await response.json(); if (data.success) { this.showNotification('Erfolg', 'PC erfolgreich hinzugefügt', 'success'); document.getElementById('addPCForm').reset(); await this.loadPCs(); // PCs neu laden } else { this.showNotification('Fehler', data.message, 'danger'); } } catch (error) { this.showNotification('Fehler', 'Fehler beim Hinzufügen des PCs', 'danger'); } } editPC(id, name, mac) { // Modal mit PC-Daten füllen document.getElementById('editPCId').value = id; document.getElementById('editPCName').value = name; document.getElementById('editMACAddress').value = mac; // Modal öffnen const editModal = new bootstrap.Modal(document.getElementById('editPCModal')); editModal.show(); } async saveEditPC() { const id = document.getElementById('editPCId').value; const name = document.getElementById('editPCName').value.trim(); const mac = document.getElementById('editMACAddress').value.trim(); if (!name || !mac) { this.showNotification('Warnung', 'Bitte füllen Sie alle Felder aus', 'warning'); return; } try { const response = await fetch(`/api/pcs/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name, mac }) }); const data = await response.json(); if (data.success) { this.showNotification('Erfolg', 'PC erfolgreich aktualisiert', 'success'); // Modal schließen const editModal = bootstrap.Modal.getInstance(document.getElementById('editPCModal')); editModal.hide(); // PCs neu laden await this.loadPCs(); } else { this.showNotification('Fehler', data.message, 'danger'); } } catch (error) { this.showNotification('Fehler', 'Fehler beim Aktualisieren des PCs', 'danger'); } } async deletePC(id) { if (!confirm('Sind Sie sicher, dass Sie diesen PC löschen möchten?')) { return; } try { const response = await fetch(`/api/pcs/${id}`, { method: 'DELETE' }); const data = await response.json(); if (data.success) { this.showNotification('Erfolg', 'PC erfolgreich gelöscht', 'success'); await this.loadPCs(); // PCs neu laden } else { this.showNotification('Fehler', data.message, 'danger'); } } catch (error) { this.showNotification('Fehler', 'Fehler beim Löschen des PCs', 'danger'); } } async wakePC(id) { try { const response = await fetch(`/api/pcs/${id}/wake`, { method: 'POST' }); const data = await response.json(); if (data.success) { this.showNotification('Erfolg', data.message, 'success'); } else { this.showNotification('Fehler', data.message, 'danger'); } } catch (error) { this.showNotification('Fehler', 'Fehler beim Senden des Wake-on-LAN Pakets', 'danger'); } } showNotification(title, message, type = 'info') { const toast = document.getElementById('notificationToast'); const toastTitle = document.getElementById('toastTitle'); const toastMessage = document.getElementById('toastMessage'); // Toast-Typ setzen toast.className = `toast ${type === 'success' ? 'bg-success' : type === 'danger' ? 'bg-danger' : type === 'warning' ? 'bg-warning' : 'bg-info'} text-white`; toastTitle.textContent = title; toastMessage.textContent = message; // Toast anzeigen const bsToast = new bootstrap.Toast(toast); bsToast.show(); } escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } formatMACAddress(input) { let value = input.value.replace(/[^0-9A-Fa-f]/g, ''); if (value.length > 12) { value = value.substring(0, 12); } // MAC-Adresse mit Doppelpunkten formatieren if (value.length >= 2) { value = value.match(/.{1,2}/g).join(':'); } input.value = value.toUpperCase(); } } // PC Manager initialisieren, wenn die Seite geladen ist document.addEventListener('DOMContentLoaded', () => { window.pcManager = new PCManager(); }); // MAC-Adresse Formatierung für das Hauptformular document.getElementById('macAddress')?.addEventListener('input', (e) => { let value = e.target.value.replace(/[^0-9A-Fa-f]/g, ''); if (value.length > 12) { value = value.substring(0, 12); } // MAC-Adresse mit Doppelpunkten formatieren if (value.length >= 2) { value = value.match(/.{1,2}/g).join(':'); } e.target.value = value.toUpperCase(); });