class PulseApp { constructor() { this.userData = this.loadUserData(); this.init(); } init() { this.setupThemeToggle(); this.setupQuickActions(); this.startRealTimeUpdates(); this.setupVitalRings(); this.setupAICoach(); } // Theme Management setupThemeToggle() { const themeToggle = document.querySelector('#theme-toggle'); if (themeToggle) { themeToggle.addEventListener('click', () => { const html = document.documentElement; const isDark = html.classList.contains('dark'); html.classList.toggle('dark'); localStorage.setItem('theme', isDark ? 'light' : 'dark'); }); } // Load saved theme const savedTheme = localStorage.getItem('theme'); if (savedTheme === 'dark') { document.documentElement.classList.add('dark'); } } // Quick Actions Handler setupQuickActions() { const quickActionButtons = document.querySelectorAll('quick-action'); quickActionButtons.forEach(button => { button.addEventListener('click', (e) => { const action = e.currentTarget.getAttribute('data-action'); this.handleQuickAction(action); }); }); } handleQuickAction(action) { const actions = { 'food': () => this.showNotification('กำลังเปิดบันทึกอาหาร...', 'orange'), 'exercise': () => this.showNotification('กำลังเริ่มจับเวลาออกกำลังกาย...', 'red'), 'mood': () => this.showNotification('กำลังเปิดบันทึกอารมณ์...', 'pink'), 'fasting': () => this.startFastingTimer(), }; if (actions[action]) { actions[action](); } } // Fasting Timer startFastingTimer() { const startTime = Date.now(); const timerInterval = setInterval(() => { const elapsed = Date.now() - startTime; const hours = Math.floor(elapsed / 3600000); const minutes = Math.floor((elapsed % 3600000) / 60000); const timerElement = document.querySelector('#fasting-timer'); if (timerElement) { timerElement.textContent = `${hours}ชม. ${minutes}น.`; } }, 60000); this.showNotification('จับเวลาการ IF เริ่มแล้ว!', 'teal'); } // Real-time Data Updates startRealTimeUpdates() { // Simulate real-time vital changes setInterval(() => { this.updateVitalRings(); this.updateContextualData(); }, 30000); // Update every 30 seconds } updateVitalRings() { const rings = [ { selector: '.text-emerald-500', key: 'recovery', variance: 2 }, { selector: '.text-orange-500', key: 'strain', variance: 0.5 }, { selector: '.text-purple-500', key: 'sleep', variance: 3 } ]; rings.forEach(ring => { const element = document.querySelector(ring.selector); if (element) { const currentValue = parseFloat(element.textContent); const change = (Math.random() - 0.5) * ring.variance; const newValue = Math.max(0, Math.min(100, currentValue + change)); element.textContent = ring.key === 'strain' ? newValue.toFixed(1) : Math.round(newValue) + '%'; } }); } updateContextualData() { // Simulate AQI changes const aqiElement = document.querySelector('contextual-vital[title="คุณภาพอากาศ"]'); if (aqiElement) { const currentAQI = parseInt(aqiElement.getAttribute('value').match(/\d+/)[0]); const newAQI = Math.max(30, Math.min(200, currentAQI + (Math.random() - 0.5) * 10)); const status = newAQI < 50 ? 'safe' : newAQI < 100 ? 'warning' : 'danger'; const statusText = status === 'safe' ? 'ดี' : status === 'warning' ? 'ปานกลาง' : 'ไม่ดี'; aqiElement.setAttribute('value', `${statusText} (${Math.round(newAQI)})`); aqiElement.setAttribute('status', status); } } // Vital Rings Animation setupVitalRings() { const rings = document.querySelectorAll('.relative.w-24.h-24'); rings.forEach((ring, index) => { setTimeout(() => { ring.classList.add('vital-pulse'); }, index * 200); }); } // AI Coach Interactions setupAICoach() { const aiButton = document.querySelector('.bg-gradient-to-r button'); if (aiButton) { aiButton.addEventListener('click', () => { this.showDetailedRecommendations(); }); } } showDetailedRecommendations() { const modal = document.createElement('div'); modal.className = 'fixed inset-0 bg-black/50 backdrop-blur-sm z-50 flex items-center justify-center p-4'; modal.innerHTML = `

คำแนะนำเพิ่มเติม

ใส่หน้ากาก N95

เมื่อออกนอกบ้านในวันฝุ่นสูง

ดื่มน้ำมากขึ้น 20%

ช่วยขับสารพิษจากฝุ่น

ใช้เครื่องฟอกอากาศ

อย่างน้อย 2 ชม. ก่อนนอน

`; document.body.appendChild(modal); feather.replace(); } // Notifications showNotification(message, color = 'violet') { const notification = document.createElement('div'); notification.className = `fixed top-20 right-4 z-50 transform translate-x-full transition-transform duration-300`; notification.innerHTML = `
${message}
`; document.body.appendChild(notification); setTimeout(() => { notification.classList.remove('translate-x-full'); }, 100); setTimeout(() => { notification.classList.add('translate-x-full'); setTimeout(() => notification.remove(), 300); }, 3000); feather.replace(); } // Data Persistence loadUserData() { const defaultData = { recovery: 72, strain: 14.2, sleep: 85, theme: 'light' }; const saved = localStorage.getItem('pulseData'); return saved ? JSON.parse(saved) : defaultData; } saveUserData() { localStorage.setItem('pulseData', JSON.stringify(this.userData)); } } // Initialize App document.addEventListener('DOMContentLoaded', () => { new PulseApp(); }); // Global Utilities window.showNotification = function(message, color) { const notification = document.createElement('div'); notification.className = `fixed top-20 right-4 z-50 transform translate-x-full transition-transform duration-300`; notification.innerHTML = `
${message}
`; document.body.appendChild(notification); setTimeout(() => notification.classList.remove('translate-x-full'), 100); setTimeout(() => { notification.classList.add('translate-x-full'); setTimeout(() => notification.remove(), 300); }, 3000); feather.replace(); };