// Global Nutrition Modal Handlers window.openNutritionModal = function(btn) { const enUrl = btn.getAttribute('data-nutri-en') || ''; const frUrl = btn.getAttribute('data-nutri-fr') || ''; const imgEn = document.getElementById('nutri-img-en'); const imgFr = document.getElementById('nutri-img-fr'); const btnFr = document.getElementById('nutri-btn-fr'); if(enUrl && enUrl.length > 5 && !enUrl.includes('null')) imgEn.src = enUrl; else imgEn.src = ""; if(frUrl && frUrl.length > 5 && !frUrl.includes('null')) imgFr.src = frUrl; else imgFr.src = ""; if(!frUrl || frUrl.length <= 5 || frUrl.includes('null')) { btnFr.classList.add('hidden'); } else { btnFr.classList.remove('hidden'); } window.switchNutritionLang('en'); document.body.style.overflow = 'hidden'; document.getElementById('merchant-nutrition-modal').classList.remove('hidden'); }; window.closeNutritionModal = function() { document.getElementById('merchant-nutrition-modal').classList.add('hidden'); document.body.style.overflow = ''; }; window.switchNutritionLang = function(lang) { const btnEn = document.getElementById('nutri-btn-en'); const btnFr = document.getElementById('nutri-btn-fr'); const imgEn = document.getElementById('nutri-img-en'); const imgFr = document.getElementById('nutri-img-fr'); if(lang === 'en') { btnEn.className = "flex-1 py-1.5 text-xs font-bold rounded-lg bg-white shadow-sm text-peppa-dark transition-all"; btnFr.className = "flex-1 py-1.5 text-xs font-bold rounded-lg text-gray-500 hover:text-peppa-dark transition-all"; imgEn.classList.remove('hidden'); imgFr.classList.add('hidden'); } else { btnFr.className = "flex-1 py-1.5 text-xs font-bold rounded-lg bg-white shadow-sm text-peppa-dark transition-all"; btnEn.className = "flex-1 py-1.5 text-xs font-bold rounded-lg text-gray-500 hover:text-peppa-dark transition-all"; imgFr.classList.remove('hidden'); imgEn.classList.add('hidden'); } }; // PO Mode Toggle Logic window.poMode = 'auto'; window.togglePoMode = function(mode) { window.poMode = mode; const autoBtn = document.getElementById('po-btn-auto'); const manualBtn = document.getElementById('po-btn-manual'); const autoContainer = document.getElementById('po-auto-container'); const manualInput = document.getElementById('po-input-manual'); // Generate initials safely let nameStr = ""; if (window.merchantBusinessName) { nameStr = window.merchantBusinessName; } else if (window.currentMerchantUser?.displayName) { nameStr = window.currentMerchantUser.displayName; } else if (window.currentMerchantUser?.email) { nameStr = window.currentMerchantUser.email.split('@')[0]; } const initials = nameStr ? nameStr.split(' ').map(n => n[0]).join('').toUpperCase().substring(0, 2) : "PO"; const now = new Date(); const dateSequence = String(now.getMonth() + 1).padStart(2, '0') + String(now.getDate()).padStart(2, '0') + String(now.getFullYear()).substring(2); const globalPo = window.lastGeneratedPo || `${initials}-${dateSequence}`; if (mode === 'auto') { if(autoBtn) autoBtn.className = "flex-1 py-1.5 text-xs font-bold rounded-lg bg-white shadow-sm text-peppa-dark transition-all"; if(manualBtn) manualBtn.className = "flex-1 py-1.5 text-xs font-bold rounded-lg text-gray-500 hover:text-peppa-dark transition-all"; if(autoContainer) { autoContainer.classList.remove('hidden'); autoContainer.innerText = globalPo; // FIX: Ensure visual field is populated } if(manualInput) manualInput.classList.add('hidden'); const hiddenInput = document.getElementById('hidden-po-input'); if(hiddenInput) hiddenInput.value = globalPo; } else { if(manualBtn) manualBtn.className = "flex-1 py-1.5 text-xs font-bold rounded-lg bg-white shadow-sm text-peppa-dark transition-all"; if(autoBtn) autoBtn.className = "flex-1 py-1.5 text-xs font-bold rounded-lg text-gray-500 hover:text-peppa-dark transition-all"; if(autoContainer) autoContainer.classList.add('hidden'); if(manualInput) { manualInput.classList.remove('hidden'); const hiddenInput = document.getElementById('hidden-po-input'); if(hiddenInput) hiddenInput.value = manualInput.value; } } }; document.addEventListener('DOMContentLoaded', () => { const manualInput = document.getElementById('po-input-manual'); if(manualInput) { manualInput.addEventListener('input', (e) => { if (window.poMode === 'manual') document.getElementById('hidden-po-input').value = e.target.value; }); } }); // Handle the form submission window.handleFirebaseLogin = function() { const email = document.getElementById('fb-login-email').value; const pass = document.getElementById('fb-login-password').value; const btn = document.getElementById('fb-login-btn'); const errBox = document.getElementById('login-error-msg'); btn.disabled = true; btn.innerText = "Authenticating..."; errBox.classList.add('hidden'); window.auth.signInWithEmailAndPassword(email, pass) .then((userCredential) => { // Success! onAuthStateChanged will handle the UI switch. btn.disabled = false; btn.innerHTML = 'Authenticate '; }) .catch((error) => { errBox.innerText = "❌ Incorrect email or password. Please try again."; errBox.classList.remove('hidden'); btn.disabled = false; btn.innerHTML = 'Authenticate '; }); }; });