function startNewsletterFlowOnce() { if (sessionStorage.getItem('newsletterFlowShown') === '1') return; try { const flow = gigya.flow('verpasse_keine_news'); console.log('[CIAM] Starte Newsletter-Flow auf Zielseite'); flow.execute(); } catch (e) { console.error('[CIAM] Fehler beim Start des Flows:', e); } finally { sessionStorage.setItem('newsletterFlowShown', '1'); } } function show(el) { if (el) el.style.display = ''; } function hide(el) { if (el) el.style.display = 'none'; } let __viewState = { current: null }; let __handlersAdded = false; function tryHideScreenSets() { try { if (gigya?.accounts?.hideScreenSet) { gigya.accounts.hideScreenSet({ screenSet: 'THW-RegistrationLogin' }); gigya.accounts.hideScreenSet({ screenSet: 'THW-ProfileUpdate' }); } } catch (e) { /* no-op */ } } function renderLogin() { if (__viewState.current === 'login') return; // idempotent __viewState.current = 'login'; const login = document.getElementById('login-container'); // wird nicht mehr genutzt const profile = document.getElementById('profile-root'); tryHideScreenSets(); if (login) login.innerHTML = ''; if (profile) profile.style.display = 'none'; document.body.classList.add('bg-login'); document.body.classList.remove('bg-profile'); gigya.accounts.showScreenSet({ screenSet: 'THW-RegistrationLogin', onLoad: function () { console.log('[CIAM] Login-ScreenSet (Dialog) geladen'); }, onAfterSubmit: function (response) { if (response?.response?.errorCode === 0) { console.log('[CIAM] onAfterSubmit OK – onLogin Event übernimmt Umschalten'); } } }); } function renderProfile() { if (__viewState.current === 'profile') return; // idempotent __viewState.current = 'profile'; const login = document.getElementById('login-container'); const profile = document.getElementById('profile-root'); tryHideScreenSets(); if (login) login.innerHTML = ''; // Sicherheit hide(login); show(profile); updateProfile?.(); document.body.classList.add('bg-profile'); document.body.classList.remove('bg-login'); } var onGigyaServiceReady = function () { // Explizites Login (z. B. auf login-thw.html) window.openLogin = function () { gigya.accounts.showScreenSet({ screenSet: 'THW-RegistrationLogin', onLoad: function () { console.log('[CIAM] Login-ScreenSet geladen'); }, onAfterSubmit: function (response) { if (response?.response?.errorCode === 0) { updateProfile?.(); } } }); }; if (!__handlersAdded) { gigya.accounts.addEventHandlers({ onLogin: function () { console.log('[CIAM] onLogin → Profil anzeigen'); renderProfile(); }, onLogout: function () { console.log('[CIAM] onLogout → Loginmaske anzeigen'); renderLogin(); } }); __handlersAdded = true; } const isProfilePage = /index-thw\.html(\?|#|$)/.test(location.pathname) || location.pathname.endsWith('/index-thw.html') || document.title.toLowerCase().includes('profil'); if (isProfilePage) { gigya.accounts.getAccountInfo({ callback: function (res) { if (res.errorCode === 0 && res.UID) { console.log('[CIAM] Session vorhanden → Profil'); startNewsletterFlowOnce?.(); renderProfile(); } else { console.log('[CIAM] Keine Session → Login'); renderLogin(); } } }); } window.updateProfile = function () { gigya.accounts.getAccountInfo({ callback: function (res) { if (res.errorCode !== 0) return; const profile = res.profile || {}; const data = res.data || {}; const zip = profile.zip; const countryValue = profile.country; const countryMap = { Germany: 'Deutschland' }; const country = countryMap[countryValue] || countryValue; const mail = profile.email; const firstName = profile.firstName; const lastName = profile.lastName; const profileImageURL = profile.photoURL; const favPlayer = data.thw?.favoritePlayer; const fanIdRaw = (data && data.b1 && (data.b1.CardCode ?? data.b1.cardCode)) ?? data?.thw?.fanId ?? null; const fanId = typeof fanIdRaw === 'string' ? fanIdRaw.trim() : (fanIdRaw ?? '').toString() const city = res.profile.city; const address = profile.address; const fullAddress = [zip, city, country].filter(Boolean).join(' '); const $ = (id) => document.getElementById(id); $('profile-name') && ($('profile-name').textContent = (`${firstName || ''} ${lastName || ''}`).trim() || '-'); $('mail') && ($('mail').textContent = mail || '-'); $('address') && ($('address').textContent = fullAddress || '-'); $('fav-player') && ($('fav-player').textContent = favPlayer || '-'); $('profile-id') && ($('profile-id').textContent = fanId || '-'); const profilePictureElement = $('profile-picture'); if (profilePictureElement) { profilePictureElement.innerHTML = ''; if (profileImageURL) { const img = document.createElement('img'); img.src = profileImageURL; img.alt = 'Profilbild'; img.className = 'profile-picture-img'; profilePictureElement.appendChild(img); } else { const initials = `${(firstName?.[0] || '')}${(lastName?.[0] || '')}`.toUpperCase(); profilePictureElement.textContent = initials || '-'; } } } }); }; window.openProfileUpdate = function () { gigya.accounts.showScreenSet({ screenSet: 'THW-ProfileUpdate', onAfterSubmit: function () { setTimeout(updateProfile, 600); } }); }; window.openCommunicationUpdate = function () { gigya.accounts.showScreenSet({ screenSet: 'THW-ProfileUpdate', startScreen: 'gigya-communication-screen', onAfterSubmit: function () { setTimeout(updateProfile, 600); } }); }; window.openTOS = function () { gigya.accounts.showScreenSet({ screenSet: 'THW-ProfileUpdate', startScreen: 'gigya-privacy-screen', onAfterSubmit: function () { setTimeout(updateProfile, 600); } }); }; window.logoutUser = function () { gigya.accounts.logout({ callback: function () { renderLogin(); } }); }; };