// Mock data for 30 days - Bangkok office worker marathon runner const mockData = { days: Array.from({ length: 30 }, (_, i) => { const dayOffset = 29 - i; const date = new Date(); date.setDate(date.getDate() - dayOffset); // Simulate realistic patterns const isWeekend = date.getDay() === 0 || date.getDay() === 6; const isHotDay = date.getMonth() >= 2 && date.getMonth() <= 5; // March-May hot season const pollutionLevel = Math.random() > 0.7 ? 'high' : 'normal'; return { date: date.toISOString().split('T')[0], recoveryScore: Math.max(20, Math.min(100, Math.floor( 70 + (isWeekend ? 10 : 0) - (pollutionLevel === 'high' ? 15 : 0) - (isHotDay ? 5 : 0) + Math.random() * 20 - 10 ))), hrv: Math.floor(40 + Math.random() * 60 + (isWeekend ? 15 : 0) - (pollutionLevel === 'high' ? 10 : 0)), rhr: Math.floor(55 + Math.random() * 15 + (pollutionLevel === 'high' ? 5 : 0)), sleepScore: Math.floor(65 + Math.random() * 25 + (isWeekend ? 8 : 0)), respiratoryRate: Math.floor(14 + Math.random() * 4 + (Math.random() > 0.9 ? 3 : 0)), tempDelta: (Math.random() * 1 - 0.5).toFixed(2), aqi_pm25: pollutionLevel === 'high' ? Math.floor(100 + Math.random() * 150) : Math.floor(20 + Math.random() * 50), ambientTemp: isHotDay ? Math.floor(35 + Math.random() * 5) : Math.floor(28 + Math.random() * 5), trafficIndex: Math.floor(2 + Math.random() * 6 + (isWeekend ? -3 : 0)), spicyIntake: Math.floor(Math.random() * 4), sugarIntake: Math.floor(Math.random() * 6), sodiumIntake: Math.floor(Math.random() * 6), sleepDebt: Math.floor(Math.random() * 3 - (isWeekend ? 2 : 0)), perceivedStress: Math.floor(3 + Math.random() * 5 + (pollutionLevel === 'high' ? 2 : 0)) }; }) }; // Current day data const todayData = mockData.days[mockData.days.length - 1]; // Utility functions function getScoreColor(score) { if (score >= 80) return '#10b981'; // Green if (score >= 60) return '#f59e0b'; // Yellow return '#ef4444'; // Red } function getScoreText(score) { if (score >= 80) return { text: 'พร้อมรบ', color: 'text-green-500' }; if (score >= 60) return { text: 'ปานกลาง', color: 'text-yellow-500' }; return { text: 'ต้องพัก', color: 'text-red-500' }; } function formatDateThai(dateString) { const date = new Date(dateString); const options = { year: 'numeric', month: 'long', day: 'numeric', locale: 'th-TH' }; return date.toLocaleDateString('th-TH', options); } function calculateTrend(values) { const recent = values.slice(-7); const previous = values.slice(-14, -7); const recentAvg = recent.reduce((a, b) => a + b, 0) / recent.length; const prevAvg = previous.reduce((a, b) => a + b, 0) / previous.length; return recentAvg - prevAvg; } // Initialize app document.addEventListener('DOMContentLoaded', function() { // Set last update time document.getElementById('lastUpdate').textContent = formatDateThai(new Date().toISOString()); // Load environmental warnings loadEnvironmentalWarnings(); // Load daily suggestions loadDailySuggestions(); // Initialize HRV chart initHRVChart(); // Load sleep data loadSleepData(); // Load nutrition matrix loadNutritionMatrix(); // Load seasonal report loadSeasonalReport(); // Initialize journal form data initJournalForm(); }); // Tab navigation function showSection(sectionName) { // Hide all sections document.querySelectorAll('.content-section').forEach(section => { section.classList.add('hidden'); }); // Show selected section document.getElementById(`${sectionName}-section`).classList.remove('hidden'); // Update tab buttons document.querySelectorAll('.tab-btn').forEach(btn => { btn.classList.remove('active', 'bg-secondary'); btn.classList.add('bg-cardBg'); }); event.target.closest('.tab-btn').classList.add('active', 'bg-secondary'); event.target.closest('.tab-btn').classList.remove('bg-cardBg'); // Trigger chart resize if needed if (sectionName === 'trends' && window.trendsChartInstance) { setTimeout(() => window.trendsChartInstance.resize(), 100); } } // Environmental warnings function loadEnvironmentalWarnings() { const warningsContainer = document.getElementById('environmental-warnings'); const warnings = []; if (todayData.aqi_pm25 > 100) { warnings.push(`🌫️ ค่า PM 2.5 สูง (${todayData.aqi_pm25} μg/m³) อาจลด HRV ของคุณได้`); } if (todayData.ambientTemp > 35) { warnings.push(`🌡️ อุณหภูมิสูง (${todayData.ambientTemp}°C) เพิ่มความเครียดในการออกกำลังกาย`); } if (todayData.trafficIndex > 7) { warnings.push(`🚗 รถติดหนัก (ระดับ ${todayData.trafficIndex}/10) เพิ่มความเครียดทางอารมณ์`); } warningsContainer.innerHTML = warnings.length > 0 ? warnings.map(w => `
📊 ช่วงเดือนมีนาคมถึงพฤษภาคม เป็นฤดูร้อนที่เครียดที่สุด
⚠️ ค่าเฉลี่ยพลังกายพร้อมรบในฤดูร้อน: ${avgRecoveryHotSeason.toFixed(1)}
💡 แนะนำ: เน้นการฟื้นตัวแบบความเย็น ลดการออกกำลังกายกลางแจ้งในช่วง 10:00-16:00
🧊 เพิ่มการดื่มน้ำเกลือแร่ อาบน้ำเย็น และใช้ห้องแอร์ 24-26°C