// WP WELLNET Longevity Platform JavaScript // Initialize the app document.addEventListener('DOMContentLoaded', function() { initializeCharts(); initializeNavigation(); initializeInteractions(); initializeNotifications(); loadUserData(); }); // Chart initialization function initializeCharts() { // Simple chart visualization for Fat percentage const fatCanvas = document.getElementById('fatChart'); if (fatCanvas) { const ctx = fatCanvas.getContext('2d'); drawSimpleLineChart(ctx, [19.2, 19.0, 18.9, 18.8, 18.7, 18.6, 18.5]); } } function drawSimpleLineChart(ctx, data) { const width = ctx.canvas.width; const height = ctx.canvas.height; const padding = 10; // Clear canvas ctx.clearRect(0, 0, width, height); // Set styles ctx.strokeStyle = '#A5D6A7'; ctx.lineWidth = 2; ctx.fillStyle = '#A5D6A7'; // Calculate points const maxValue = Math.max(...data); const minValue = Math.min(...data); const range = maxValue - minValue; const xStep = (width - padding * 2) / (data.length - 1); const yScale = (height - padding * 2) / range; // Draw line ctx.beginPath(); data.forEach((value, index) => { const x = padding + index * xStep; const y = height - padding - (value - minValue) * yScale; if (index === 0) { ctx.moveTo(x, y); } else { ctx.lineTo(x, y); } }); ctx.stroke(); // Draw points data.forEach((value, index) => { const x = padding + index * xStep; const y = height - padding - (value - minValue) * yScale; ctx.beginPath(); ctx.arc(x, y, 3, 0, Math.PI * 2); ctx.fill(); }); } // Navigation handling function initializeNavigation() { const navButtons = document.querySelectorAll('nav button'); navButtons.forEach(button => { button.addEventListener('click', function() { // Remove active state from all buttons navButtons.forEach(btn => { btn.classList.remove('text-primary'); btn.classList.add('text-gray-500'); }); // Add active state to clicked button this.classList.remove('text-gray-500'); this.classList.add('text-primary'); // Handle navigation (scroll to section) const buttonText = this.querySelector('span').textContent; navigateToSection(buttonText); }); }); } function navigateToSection(section) { const sectionMap = { 'หน้าหลัก': 'dashboard', 'วิเคราะห์': 'ai-analysis', 'แผน': 'longevity-plan', 'สินค้า': 'products', 'รางวัล': 'rewards' }; const targetSection = document.getElementById(sectionMap[section]); if (targetSection) { targetSection.scrollIntoView({ behavior: 'smooth', block: 'start' }); } } // Interactive elements function initializeInteractions() { // Analyze button const analyzeBtn = document.querySelector('button:has(.refresh-cw)'); if (analyzeBtn) { analyzeBtn.addEventListener('click', function() { this.disabled = true; this.innerHTML = 'กำลังวิเคราะห์...'; setTimeout(() => { this.disabled = false; this.innerHTML = 'วิเคราะห์ใหม่'; feather.replace(); showNotification('วิเคราะห์ข้อมูลเสร็จสิ้น!', 'success'); }, 2000); }); } // Mission completion buttons const missionButtons = document.querySelectorAll('.bg-green-100, .bg-gray-200'); missionButtons.forEach(button => { if (button.textContent.includes('สำเร็จ')) { button.addEventListener('click', function() { toggleMissionStatus(this); }); } }); // Product purchase buttons const purchaseButtons = document.querySelectorAll('button:contains("สั่งซื้อ")'); purchaseButtons.forEach(button => { button.addEventListener('click', function() { handlePurchase(this); }); }); // Reorder buttons const reorderButtons = document.querySelectorAll('button:contains("ซื้อซ้ำ")'); reorderButtons.forEach(button => { button.addEventListener('click', function() { handleReorder(this); }); }); } // Mission status toggle function toggleMissionStatus(button) { if (button.textContent.includes('สำเร็จ')) { button.classList.remove('bg-green-100', 'text-green-600'); button.classList.add('bg-gray-200', 'text-gray-600'); button.textContent = 'ยังไม่สำเร็จ'; } else { button.classList.remove('bg-gray-200', 'text-gray-600'); button.classList.add('bg-green-100', 'text-green-600'); button.textContent = 'สำเร็จ'; button.classList.add('success-animation'); // Update points updatePoints(20); showNotification('ภารกิจสำเร็จ! +20 แต้ม', 'success'); } } // Purchase handling function handlePurchase(button) { button.disabled = true; button.textContent = 'กำลังดำเนินการ...'; setTimeout(() => { button.disabled = false; button.textContent = 'สั่งซื้อทันที'; showNotification('เพิ่มสินค้าลงตะกร้าแล้ว', 'success'); // Update cart count updateCartCount(); }, 1500); } // Reorder handling function handleReorder(button) { button.disabled = true; button.textContent = 'กำลังสั่งซื้อ...'; setTimeout(() => { button.disabled = false; button.textContent = 'ซื้อซ้ำ 1 คลิก'; showNotification('สั่งซื้อสำเร็จ! จัดส่งใน 1-2 วัน', 'success'); }, 2000); } // Points system let currentPoints = 750; function updatePoints(points) { currentPoints += points; const pointsDisplay = document.querySelector('.text-2xl.font-bold'); if (pointsDisplay && pointsDisplay.textContent.includes('แต้ม')) { pointsDisplay.textContent = currentPoints; pointsDisplay.classList.add('success-animation'); setTimeout(() => { pointsDisplay.classList.remove('success-animation'); }, 600); } } // Cart system let cartCount = 0; function updateCartCount() { cartCount++; // Add cart badge to navigation if not exists const cartButton = document.querySelector('button:has(.shopping-cart)'); if (cartButton && cartCount > 0) { let badge = cartButton.querySelector('.cart-badge'); if (!badge) { badge = document.createElement('span'); badge.className = 'cart-badge absolute -top-1 -right-1 bg-red-500 text-white text-xs rounded-full w-5 h-5 flex items-center justify-center'; cartButton.appendChild(badge); } badge.textContent = cartCount; badge.classList.add('badge-pulse'); } } // Notification system function showNotification(message, type = 'info') { const notification = document.createElement('div'); notification.className = `fixed top-20 left-4 right-4 z-50 p-4 rounded-lg shadow-lg transform transition-all duration-300 ${ type === 'success' ? 'bg-green-500 text-white' : type === 'error' ? 'bg-red-500 text-white' : 'bg-blue-500 text-white' }`; notification.innerHTML = `