// Navigation Router function router(pageName) { const viewContainer = document.getElementById('app-view'); const title = document.getElementById('page-title'); const navItems = document.querySelectorAll('.nav-item'); // Update Nav navItems.forEach(item => { if (item.dataset.page === pageName) item.classList.add('active'); else item.classList.remove('active'); }); // Set Title title.textContent = pageName.charAt(0).toUpperCase() + pageName.slice(1); // Load View const template = document.getElementById(`view-${pageName}`); if (template) { viewContainer.innerHTML = ''; viewContainer.appendChild(template.content.cloneNode(true)); // Initialize view specific logic if (pageName === 'dashboard') loadDashboard(); if (pageName === 'shop') loadShop(); if (pageName === 'profile') loadProfile(); // Re-init generic UI stuff like icons if new ones added if (window.lucide) lucide.createIcons(); } } // Data Fetching const API_BASE = '/api'; // Telegram Integration let tgUser = null; if (window.Telegram && window.Telegram.WebApp) { const tg = window.Telegram.WebApp; tg.ready(); tgUser = tg.initDataUnsafe?.user; // Theme sync if (tg.colorScheme === 'dark') document.body.classList.add('dark'); // Expand tg.expand(); } // Fallback for browser testing if (!tgUser) { console.warn("No Telegram user detected, using mock user"); tgUser = { id: 123456789, first_name: 'Test', username: 'testuser' }; } // Update UI with User Info const sidebarName = document.getElementById('sidebar-name'); const sidebarAvatar = document.getElementById('sidebar-avatar'); if (sidebarName) sidebarName.textContent = tgUser.first_name || tgUser.username; if (sidebarAvatar) sidebarAvatar.textContent = (tgUser.first_name || 'U')[0].toUpperCase(); async function loadDashboard() { try { const res = await fetch(`${API_BASE}/user/${tgUser.id}`); if (!res.ok) throw new Error("Failed to fetch user"); const data = await res.json(); const statusEl = document.getElementById('dash-status'); const daysEl = document.getElementById('dash-days'); const dataEl = document.getElementById('dash-data'); const planEl = document.getElementById('sub-plan-name'); const expireEl = document.getElementById('sub-expire-date'); if (statusEl) statusEl.textContent = data.status; if (daysEl) daysEl.textContent = data.days_left; if (dataEl) dataEl.textContent = `${data.data_usage || 0} GB`; if (planEl) planEl.textContent = data.plan; if (expireEl) expireEl.textContent = data.expire_date; // Colorize status if (data.status === 'Active') { document.querySelector('.stat-info .value').style.color = '#4ade80'; } else { document.querySelector('.stat-info .value').style.color = '#f87171'; } } catch (e) { console.error(e); // Show error state? } } async function loadShop() { const container = document.getElementById('plans-container'); if (!container) return; container.innerHTML = '