(function(){
var PREF_KEYS = {
calendar: 'profile_show_calendar',
inbox: 'profile_show_inbox',
salary: 'profile_show_salary'
};
function setStatus(id, text, ok){
var el = document.getElementById(id);
if(!el) return;
el.textContent = text;
el.style.color = ok ? '#059669' : '#dc2626';
setTimeout(function(){ if(el.textContent === text) el.textContent = ''; }, 2500);
}
function applyProfileMenuPreferences(){
var calendar = localStorage.getItem(PREF_KEYS.calendar);
var inbox = localStorage.getItem(PREF_KEYS.inbox);
var salary = localStorage.getItem(PREF_KEYS.salary);
document.querySelectorAll('.nav-item[data-page="kalender"]').forEach(function(el){ el.style.display = calendar === '0' ? 'none' : ''; });
document.querySelectorAll('.nav-item[data-page="inkorg"]').forEach(function(el){ el.style.display = inbox === '0' ? 'none' : ''; });
document.querySelectorAll('.nav-item[data-page="my-salary"]').forEach(function(el){ el.style.display = salary === '0' ? 'none' : ''; });
}
function initProfilePage(){
var nameInput = document.getElementById('profileName');
var emailInput = document.getElementById('profileEmail');
var phoneInput = document.getElementById('profilePhone');
var languageInput = document.getElementById('profileLanguage');
var showCalendar = document.getElementById('profileShowCalendar');
var showInbox = document.getElementById('profileShowInbox');
var showSalary = document.getElementById('profileShowSalary');
var signatureInput = document.getElementById('profileSignature');
if(nameInput) nameInput.value = localStorage.getItem('profile_name') || (typeof gUserName !== 'undefined' ? gUserName : '');
if(emailInput) emailInput.value = localStorage.getItem('profile_email') || (typeof gUserEmail !== 'undefined' ? gUserEmail : '');
if(phoneInput) phoneInput.value = localStorage.getItem('profile_phone') || '';
if(languageInput) languageInput.value = localStorage.getItem('profile_language') || 'sv';
if(showCalendar) showCalendar.checked = localStorage.getItem(PREF_KEYS.calendar) !== '0';
if(showInbox) showInbox.checked = localStorage.getItem(PREF_KEYS.inbox) !== '0';
if(showSalary) showSalary.checked = localStorage.getItem(PREF_KEYS.salary) !== '0';
if(signatureInput) signatureInput.value = localStorage.getItem('mailSignature') || (typeof gMailSignature !== 'undefined' ? gMailSignature : '');
applyProfileMenuPreferences();
}
function saveProfileSettings(){
localStorage.setItem('profile_name', document.getElementById('profileName').value || '');
localStorage.setItem('profile_email', document.getElementById('profileEmail').value || '');
localStorage.setItem('profile_phone', document.getElementById('profilePhone').value || '');
localStorage.setItem('profile_language', document.getElementById('profileLanguage').value || 'sv');
if(typeof gUserName !== 'undefined') gUserName = document.getElementById('profileName').value || gUserName;
if(typeof gUserEmail !== 'undefined') gUserEmail = document.getElementById('profileEmail').value || gUserEmail;
try { sessionStorage.setItem('gUserName', gUserName || ''); sessionStorage.setItem('gUserEmail', gUserEmail || ''); } catch(e){}
var nameStrong = document.querySelector('.sidebar-user-info strong');
if(nameStrong) nameStrong.textContent = gUserEmail || gUserName || 'Logged in';
setStatus('profileSaveStatus', 'Profile saved', true);
}
function saveProfilePassword(){
var next = document.getElementById('profileNewPassword').value || '';
var confirm = document.getElementById('profileConfirmPassword').value || '';
if(!next || next !== confirm){
setStatus('profilePasswordStatus', 'Passwords do not match', false);
return;
}
document.getElementById('profileCurrentPassword').value = '';
document.getElementById('profileNewPassword').value = '';
document.getElementById('profileConfirmPassword').value = '';
setStatus('profilePasswordStatus', 'Password updated locally', true);
}
function saveProfilePreferences(){
localStorage.setItem(PREF_KEYS.calendar, document.getElementById('profileShowCalendar').checked ? '1' : '0');
localStorage.setItem(PREF_KEYS.inbox, document.getElementById('profileShowInbox').checked ? '1' : '0');
localStorage.setItem(PREF_KEYS.salary, document.getElementById('profileShowSalary').checked ? '1' : '0');
applyProfileMenuPreferences();
setStatus('profilePreferencesStatus', 'Preferences saved', true);
}
function saveProfileSignature(){
var sig = document.getElementById('profileSignature').value || '';
localStorage.setItem('mailSignature', sig);
if(typeof gMailSignature !== 'undefined') gMailSignature = sig;
setStatus('profileSignatureStatus', 'Signature saved', true);
}
window.applyProfileMenuPreferences = applyProfileMenuPreferences;
window.initProfilePage = initProfilePage;
window.saveProfileSettings = saveProfileSettings;
window.saveProfilePassword = saveProfilePassword;
window.saveProfilePreferences = saveProfilePreferences;
window.saveProfileSignature = saveProfileSignature;
window.addEventListener('DOMContentLoaded', function(){
initProfilePage();
applyProfileMenuPreferences();
});
})();