class AuthModal extends HTMLElement { connectedCallback() { this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = ` `; this.setupEventListeners(); } setupEventListeners() { const modal = this.shadowRoot.getElementById('authModal'); const closeBtn = this.shadowRoot.querySelector('.close'); closeBtn.addEventListener('click', () => { this.closeModal(); }); window.addEventListener('click', (event) => { if (event.target === modal) { this.closeModal(); } }); // Listen for auth state changes window.addEventListener('authStateChange', () => { this.updateAuthContent(); }); } openModal() { this.shadowRoot.getElementById('authModal').style.display = 'block'; this.updateAuthContent(); } closeModal() { this.shadowRoot.getElementById('authModal').style.display = 'none'; } updateAuthContent() { const authContent = this.shadowRoot.getElementById('authContent'); const user = this.getCurrentUser(); if (user) { authContent.innerHTML = `
Avatar

${user.first_name} ${user.last_name || ''}

@${user.username || 'Foydalanuvchi'}

ID: ${user.id}

`; this.shadowRoot.querySelector('.logout-btn').addEventListener('click', () => { this.logout(); }); } else { authContent.innerHTML = `

Telegram orqali hisobingizga kiring:

`; this.shadowRoot.getElementById('telegramAuthBtn').addEventListener('click', () => { this.initiateTelegramAuth(); }); } } getCurrentUser() { return JSON.parse(localStorage.getItem('telegramUser')) || null; } initiateTelegramAuth() { // In a real implementation, this would redirect to Telegram Login Widget // For demo purposes, we'll simulate a successful login const fakeUser = { id: Math.floor(10000000 + Math.random() * 90000000), first_name: "Test", last_name: "User", username: "testuser", photo_url: "https://static.photos/people/320x240/2", auth_date: Math.floor(Date.now() / 1000) }; this.loginUser(fakeUser); } loginUser(user) { localStorage.setItem('telegramUser', JSON.stringify(user)); const event = new CustomEvent('authStateChange'); window.dispatchEvent(event); this.closeModal(); // Show success message const originalContent = this.shadowRoot.getElementById('authContent').innerHTML; this.shadowRoot.getElementById('authContent').innerHTML = `

✅ Muvaffaqiyatli kirish!

`; setTimeout(() => { this.shadowRoot.getElementById('authContent').innerHTML = originalContent; this.updateAuthContent(); }, 2000); } logout() { localStorage.removeItem('telegramUser'); const event = new CustomEvent('authStateChange'); window.dispatchEvent(event); this.updateAuthContent(); } } customElements.define('auth-modal', AuthModal);