// Handle selection change select.addEventListener('change', function() { if (this.value) { const selectedOption = this.options[this.selectedIndex]; const username = selectedOption.dataset.username; const fullName = selectedOption.dataset.fullName; info.innerHTML = ` Username: ${username}`; } else { info.innerHTML = ''; } }); } function showStatus(message, type) { const statusEl = document.getElementById('statusMessage'); statusEl.textContent = message; statusEl.className = `status-message status-${type}`; statusEl.style.display = 'block'; if (type === 'success') { setTimeout(() => { statusEl.style.display = 'none'; }, 3000); } } function setLoading(loading) { const loginBtn = document.getElementById('loginBtn'); const loginText = loginBtn.querySelector('.login-text'); const loadingText = loginBtn.querySelector('.loading'); const form = document.getElementById('loginForm'); if (loading) { loginText.style.display = 'none'; loadingText.style.display = 'inline'; loginBtn.disabled = true; form.style.pointerEvents = 'none'; } else { loginText.style.display = 'inline'; loadingText.style.display = 'none'; loginBtn.disabled = false; form.style.pointerEvents = 'auto'; } } // Handle form submission document.getElementById('loginForm').addEventListener('submit', async function(e) { e.preventDefault(); const extension = document.getElementById('extensionSelect').value; const password = document.getElementById('password').value; if (!extension || !password) { showStatus('Please select an extension and enter password', 'error'); return; } setLoading(true); try { // Authenticate with server const response = await fetch('authenticate.php', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ extension: extension, password: password }) }); const data = await response.json(); if (data.success) { showStatus('Authentication successful! Redirecting...', 'success'); // Redirect to PHP phone interface setTimeout(() => { window.location.href = data.redirect || 'phone_index.php'; }, 1500); } else { showStatus(data.error || 'Authentication failed', 'error'); setLoading(false); } } catch (error) { console.error('Authentication error:', error); showStatus('Connection error. Please try again.', 'error'); setLoading(false); } }); // Load extensions when page loads document.addEventListener('DOMContentLoaded', function() { loadExtensions(); });