// Settings page functionality // ========== Legacy functions (kept for backward compatibility) ========== function testPrinter() { const resultEl = document.getElementById('printerTestResult'); const selectedHidden = document.getElementById('selectedPrintersJson'); resultEl.textContent = 'Testing...'; resultEl.style.color = '#666'; const body = JSON.stringify({ selectedPrintersJson: selectedHidden ? selectedHidden.value : '[]' }); fetch('/settings/test-printer', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body }) .then(response => response.json()) .then(data => { if (!data.error) { resultEl.textContent = '✓ ' + data.message; resultEl.style.color = '#28a745'; } else { resultEl.textContent = '✗ ' + (data.message || 'Test failed'); resultEl.style.color = '#dc3545'; } }) .catch(error => { console.error('Test print error:', error); resultEl.textContent = '✗ Network error'; resultEl.style.color = '#dc3545'; }); } async function uploadLogo() { const fileInput = document.getElementById('logoUpload'); const file = fileInput.files[0]; if (!file) { alert('Please select a file first'); return; } const validTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']; if (!validTypes.includes(file.type)) { alert('Please select a valid image file (PNG, JPG, or GIF)'); return; } if (file.size > 5 * 1024 * 1024) { alert('File size must be less than 5MB'); return; } const formData = new FormData(); formData.append('file', file); try { const response = await fetch('/settings/upload-logo', { method: 'POST', body: formData }); const data = await response.json(); if (!data.error) { alert('Logo uploaded successfully!'); location.reload(); } else { alert('Upload failed: ' + (data.message || 'Unknown error')); } } catch (error) { console.error('Logo upload error:', error); alert('Upload failed: Network error'); } } // ========== New Printer Management Functions ========== let currentPrinterId = null; // Load and display printers async function loadPrinters() { const container = document.getElementById('printer-cards-container'); if (!container) return; try { const response = await fetch('/api/printers/list'); const data = await response.json(); if (data.error || !data.printers || data.printers.length === 0) { container.innerHTML = `
No printers configured yet.
Click "Add Printer" to configure your first printer.
Detecting printers...
'; try { const response = await fetch('/api/printers/detect'); const data = await response.json(); if (data.error || !data.printers || data.printers.length === 0) { listEl.innerHTML = 'No printers detected.
'; return; } const items = data.printers.map(p => { const typeLabel = { 'system': 'System', 'com': 'COM' }[p.type] || p.type; return `Failed to detect printers.
'; } } // Select a detected printer function selectDetectedPrinter(type, interface) { document.getElementById('printer_type_select').value = type; document.getElementById('printer_interface').value = interface; updateInterfaceHint(); } // Update interface hint based on connection type function updateInterfaceHint() { const type = document.getElementById('printer_type_select').value; const hintEl = document.getElementById('interface_hint'); const hints = { 'network': 'Enter IP:Port for network printers (e.g., 192.168.1.100:9100)', 'com': 'Enter COM port (e.g., COM1, COM3)', 'usb': 'Enter USB device path (e.g., /dev/usb/lp0)', 'system': 'Enter the exact printer name from Windows' }; hintEl.textContent = hints[type] || 'Enter connection address'; } // Auto-update paper width when format changes function updatePaperWidthFromFormat() { const format = document.getElementById('paper_format').value; const widthInput = document.getElementById('paper_width'); const widthMap = { '58mm': 32, '80mm': 48, 'letter': 80 }; if (widthMap[format]) { widthInput.value = widthMap[format]; } } // Modal tab switching function switchPrinterModalTab(tabName) { // Update tab buttons document.querySelectorAll('.printer-modal-tab-btn').forEach(btn => { btn.classList.remove('active'); if (btn.getAttribute('data-tab') === tabName) { btn.classList.add('active'); } }); // Update tab content document.querySelectorAll('.printer-modal-tab-content').forEach(content => { content.classList.remove('active'); }); document.getElementById(tabName + '-tab-content').classList.add('active'); } // Close printer modal function closePrinterModal() { hideModal('printerConfigModal'); currentPrinterId = null; } // Show modal function showModal(modalId) { document.getElementById(modalId).classList.add('visible'); } // Hide modal function hideModal(modalId) { document.getElementById(modalId).classList.remove('visible'); } // ========== Sound Notification Functions ========== // Upload sound file async function uploadSound(soundType) { const fileInputId = soundType === 'newOrder' ? 'newOrderSoundUpload' : 'canceledOrderSoundUpload'; const fileInput = document.getElementById(fileInputId); const file = fileInput.files[0]; if (!file) { alert('Please select a file first'); return; } const validTypes = ['audio/mpeg', 'audio/mp3', 'audio/wav', 'audio/ogg']; if (!validTypes.includes(file.type)) { alert('Please select a valid audio file (MP3, WAV, or OGG)'); return; } if (file.size > 10 * 1024 * 1024) { alert('File size must be less than 10MB'); return; } const formData = new FormData(); formData.append('file', file); formData.append('soundType', soundType); try { const response = await fetch('/settings/upload-sound', { method: 'POST', body: formData }); const data = await response.json(); if (!data.error) { alert('Sound uploaded successfully! Please save settings to apply changes.'); location.reload(); } else { alert('Upload failed: ' + (data.message || 'Unknown error')); } } catch (error) { console.error('Sound upload error:', error); alert('Upload failed: Network error'); } } // Test sound playback async function testSound(soundType) { try { // Get current settings const response = await fetch('/api/notification-settings'); const data = await response.json(); if (data.error) { alert('Failed to load sound settings'); return; } const soundPath = soundType === 'newOrder' ? (data.newOrderSoundPath || '/public/sounds/new-order-notification.mp3') : (data.canceledOrderSoundPath || '/public/sounds/canceled-order-notification.mp3'); const volumeInput = document.getElementById('soundVolume'); const volume = volumeInput ? parseInt(volumeInput.value, 10) / 100 : 0.8; const audio = new Audio(soundPath); audio.volume = volume; audio.play().catch(error => { console.error('Failed to play sound:', error); alert('Failed to play sound. Make sure the file exists and is a valid audio file.'); }); } catch (error) { console.error('Test sound error:', error); alert('Failed to test sound: ' + error.message); } } // Update volume display function updateVolumeDisplay() { const volumeInput = document.getElementById('soundVolume'); const volumeValue = document.getElementById('volumeValue'); if (volumeInput && volumeValue) { volumeValue.textContent = volumeInput.value; } } // ========== Event Listeners ========== document.addEventListener('DOMContentLoaded', function() { // Load printers on settings page if (document.getElementById('printer-cards-container')) { loadPrinters(); } // Printer modal tab switching document.querySelectorAll('.printer-modal-tab-btn').forEach(btn => { btn.addEventListener('click', function() { switchPrinterModalTab(this.getAttribute('data-tab')); }); }); // Connection type change handler const typeSelect = document.getElementById('printer_type_select'); if (typeSelect) { typeSelect.addEventListener('change', updateInterfaceHint); } // Paper format change handler const formatSelect = document.getElementById('paper_format'); if (formatSelect) { formatSelect.addEventListener('change', updatePaperWidthFromFormat); } // Volume slider handler const volumeInput = document.getElementById('soundVolume'); if (volumeInput) { volumeInput.addEventListener('input', updateVolumeDisplay); } // Update interface placeholder based on interface type (legacy support) const interfaceSelect = document.getElementById('printerInterface'); const pathInput = document.getElementById('printerPath'); if (interfaceSelect && pathInput) { interfaceSelect.addEventListener('change', function() { const placeholders = { 'usb': '/dev/usb/lp0 (Linux) or COM1 (Windows)', 'network': '192.168.1.100:9100', 'serial': 'COM1 (Windows) or /dev/ttyS0 (Linux)' }; pathInput.placeholder = placeholders[this.value] || 'Enter printer path'; }); } });