let selectedProfileId = localStorage.getItem('selectedProfileId') || 'default'; let editingProfile = null; // Profile functions async function loadProfiles() { try { const response = await apiRequest('/profiles'); if (!response || !Object.keys(response).length) { throw new Error('No profiles available'); } const profiles = Object.values(response); const select = document.getElementById('profileSelect'); // Actualizar el selector select.innerHTML = profiles.map(profile => ` `).join(''); // Intentar seleccionar el perfil guardado o el predeterminado const savedProfile = profiles.find(p => p.id === selectedProfileId); if (savedProfile) { await selectProfile(savedProfile.id); } else { // Si no se encuentra el perfil guardado, usar el primero disponible selectedProfileId = profiles[0].id; select.value = selectedProfileId; await selectProfile(selectedProfileId); } localStorage.setItem('selectedProfileId', selectedProfileId); } catch (error) { console.error('Error loading profiles:', error); showError('Error loading profiles. Using default profile.'); await loadDefaultProfile(); } } async function loadDefaultProfile() { try { currentProfile = await apiRequest('/profiles/default'); selectedProfileId = 'default'; localStorage.setItem('selectedProfileId', 'default'); const select = document.getElementById('profileSelect'); select.innerHTML = ``; select.value = 'default'; // Actualizar la visualización del perfil updateProfileDisplay(); } catch (error) { console.error('Error loading default profile:', error); showError('Failed to load default profile'); } } async function selectProfile(profileId) { try { const response = await apiRequest(`/profiles/${profileId}`); if (!response || response.error) { throw new Error(response?.error || 'Profile not found'); } currentProfile = response; selectedProfileId = profileId; localStorage.setItem('selectedProfileId', profileId); // Actualizar la visualización del perfil updateProfileDisplay(); } catch (error) { console.error('Failed to load profile:', error); showError(`Failed to load profile: ${error.message}`); // Intentar cargar el perfil por defecto si falla if (profileId !== 'default') { await loadDefaultProfile(); } } } function updateProfileDisplay() { const profileConfig = document.getElementById('profileConfig'); if (!profileConfig || !currentProfile) return; profileConfig.innerHTML = `