// Tab switching functionality function switchTab(tabName) { // Hide all tabs document.querySelectorAll('.tab-content').forEach(tab => { tab.classList.remove('active'); }); // Remove active state from all nav buttons document.querySelectorAll('.tab-btn').forEach(btn => { btn.classList.remove('text-health-green'); btn.classList.add('text-gray-400'); }); // Show selected tab document.getElementById(`tab-${tabName}`).classList.add('active'); // Set active state for selected nav button const activeBtn = document.querySelector(`[data-tab="${tabName}"]`); if (activeBtn) { activeBtn.classList.remove('text-gray-400'); activeBtn.classList.add('text-health-green'); } // Re-render feather icons feather.replace(); // Initialize charts if switching to history tab if (tabName === 'history') { setTimeout(() => drawCharts(), 100); } } // Draw charts for history tab function drawCharts() { // HRV Chart const hrvCanvas = document.getElementById('hrvChart'); if (hrvCanvas && hrvCanvas.getContext) { const ctx = hrvCanvas.getContext('2d'); const hrvData = [48, 49, 52, 57, 61, 63, 62, 59, 65, 68, 70, 71, 69, 72, 74, 76, 75, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89]; drawLineChart(ctx, hrvData, '#10b981'); } // Blood Pressure Chart const bpCanvas = document.getElementById('bpChart'); if (bpCanvas && bpCanvas.getContext) { const ctx = bpCanvas.getContext('2d'); const bpData = [145, 140, 138, 132, 130, 129, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105]; drawLineChart(ctx, bpData, '#ef4444'); } // Weight Chart const weightCanvas = document.getElementById('weightChart'); if (weightCanvas && weightCanvas.getContext) { const ctx = weightCanvas.getContext('2d'); const weightData = [72.4, 72.2, 72.0, 71.8, 71.6, 71.5, 71.4, 71.3, 71.2, 71.1, 71.0, 70.9, 70.8, 70.7, 70.6, 70.5, 70.4, 70.3, 70.2, 70.1, 70.0, 69.9, 69.8, 69.7, 69.6, 69.5, 69.4, 69.3, 69.2, 69.1]; drawLineChart(ctx, weightData, '#f59e0b'); } // pH Chart const phCanvas = document.getElementById('phChart'); if (phCanvas && phCanvas.getContext) { const ctx = phCanvas.getContext('2d'); const phData = [5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6.0, 6.1, 6.2, 6.3, 6.4, 6.5, 6.5, 6.6, 6.6, 6.6, 6.7, 6.7, 6.7, 6.8, 6.8, 6.8, 6.8, 6.8, 6.8, 6.8, 6.8]; drawLineChart(ctx, phData, '#8b5cf6'); } // Progress Chart for plan tab const progressCanvas = document.getElementById('progressChart'); if (progressCanvas && progressCanvas.getContext) { const ctx = progressCanvas.getContext('2d'); drawBarChart(ctx, [1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0]); } } function drawLineChart(ctx, data, color) { const width = ctx.canvas.width; const height = ctx.canvas.height; const padding = 10; // Clear canvas ctx.clearRect(0, 0, width, height); // Calculate points const maxValue = Math.max(...data); const minValue = Math.min(...data); const range = maxValue - minValue || 1; const points = data.map((value, index) => ({ x: padding + (index * (width - padding * 2)) / (data.length - 1), y: height - padding - ((value - minValue) / range) * (height - padding * 2) })); // Draw line ctx.beginPath(); ctx.strokeStyle = color; ctx.lineWidth = 2; points.forEach((point, index) => { if (index === 0) { ctx.moveTo(point.x, point.y); } else { ctx.lineTo(point.x, point.y); } }); ctx.stroke(); // Draw points points.forEach(point => { ctx.beginPath(); ctx.arc(point.x, point.y, 3, 0, Math.PI * 2); ctx.fillStyle = color; ctx.fill(); }); } function drawBarChart(ctx, data) { const width = ctx.canvas.width; const height = ctx.canvas.height; const padding = 20; const barWidth = (width - padding * 2) / data.length - 5; ctx.clearRect(0, 0, width, height); data.forEach((value, index) => { const x = padding + index * (barWidth + 5); const barHeight = value * (height - padding); const y = height - barHeight; ctx.fillStyle = value ? '#10b981' : '#ef4444'; ctx.fillRect(x, y, barWidth, barHeight); }); } // Update history period function updateHistoryPeriod(days) { console.log(`Updating to ${days} days`); // In a real app, this would fetch new data and redraw charts drawCharts(); } // Initialize on page load document.addEventListener('DOMContentLoaded', function() { // Set initial tab switchTab('dashboard'); // Initialize animations const observerOptions = { threshold: 0.1, rootMargin: '0px 0px -50px 0px' }; const observer = new IntersectionObserver(function(entries) { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('animate-fade-in'); } }); }, observerOptions); // Observe elements document.querySelectorAll('.bg-white').forEach(el => { observer.observe(el); }); }); // Simulate real-time updates setInterval(() => { // Update time-based elements const now = new Date(); const timeString = now.toLocaleTimeString('th-TH', { hour: '2-digit', minute: '2-digit' }); // You could update any real-time elements here }, 60000); // Add touch gestures for mobile let touchStartX = 0; let touchEndX = 0; document.addEventListener('touchstart', function(event) { touchStartX = event.changedTouches[0].screenX; }); document.addEventListener('touchend', function(event) { touchEndX = event.changedTouches[0].screenX; handleSwipe(); }); function handleSwipe() { const swipeThreshold = 50; const diff = touchStartX - touchEndX; if (Math.abs(diff) > swipeThreshold) { // Get current active tab const activeTab = document.querySelector('.tab-content.active'); const tabName = activeTab.id.replace('tab-', ''); const tabs = ['dashboard', 'today', 'history', 'products', 'profile']; const currentIndex = tabs.indexOf(tabName); if (diff > 0 && currentIndex < tabs.length - 1) { // Swipe left - next tab switchTab(tabs[currentIndex + 1]); } else if (diff < 0 && currentIndex > 0) { // Swipe right - previous tab switchTab(tabs[currentIndex - 1]); } } } // Add smooth scroll behavior document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); const target = document.querySelector(this.getAttribute('href')); if (target) { target.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); });