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
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487

// Behavior Database
const behaviorDatabase = {
    "การนอนและสุขภาพรอบวัน": [
        "แว่นกันแสงสีฟ้า",
        "เครื่องเสียง",
        "อ่านหนังสือบนเตียง",
        "นอนร่วมกับผู้อื่น",
        "แมวอยู่ในห้องนอน",
        "สุนัขอยู่ในห้องนอน",
        "หน้ากากปิดตา",
        "นอนในห้องมืด",
        "แสงแดดตอนเช้า",
        "รับประทานอาหารดึก"
    ],
    "การฟื้นตัว": [
        "นวดบำบัด",
        "อาบน้ำเย็น",
        "การยืดกล้ามเนื้อ",
        "ซาวน่า",
        "ห้องไอน้ำ",
        "ฝังเข็ม",
        "ถ้วยฝาครอบ",
        "การ meditate",
        "การฝึกหายใจ",
        "อาบน้ำอุ่น"
    ],
    "โภชนาการ": [
        "การลดน้ำหนักแบบ Intermittent Fasting",
        "อาหาร Paleo",
        "อาหารพืช",
        "อาหาร Keto",
        "อาหาร Mediterranean",
        "คาเฟอีน",
        "การดื่มแอลกอฮอล์",
        "ไม่ดื่มแอลกอฮอล์",
        "การติดตามการบริโภคน้ำ"
    ],
    "ยาและอาหารเสริม": [
        "อาหารเสริมแมกนีเซียม",
        "เมลาโทนิน",
        "วิตามินรวม",
        "วิตามิน D",
        "โปรไบโอติกส์",
        "CBD",
        "ยาแก้ปวด",
        "ครีเอทีน",
        "ผงโปรตีน"
    ],
    "ไลฟ์สไตล์และสุขภาพจิต": [
        "ความวิตกกังวล",
        "ความเครียด",
        "เวลาหน้าจอ",
        "เดินเที่ยวในธรรมชาติ",
        "การ detox จากดิจิทัล",
        "การตั้งสติ",
        "โยคะ",
        "การฝึกหายใจ"
    ]
};

// Initialize Feather Icons
feather.replace();

// Chart Configurations
Chart.defaults.color = '#9ca3af';
Chart.defaults.borderColor = '#374151';
// Heart Rate Zones Chart
const heartRateZonesCtx = document.getElementById('heartRateZonesChart');
if (heartRateZonesCtx) {
    const heartRateZonesChart = new Chart(heartRateZonesCtx, {
        type: 'bar',
        data: {
            labels: ['00:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'],
            datasets: [
                {
                    label: 'พัก',
                    data: [45, 48, 52, 65, 58, 62, 55, 50],
                    backgroundColor: 'rgba(59, 130, 246, 0.8)',
                    borderColor: '#3b82f6',
                    borderWidth: 0
                },
                {
                    label: 'เบา',
                    data: [10, 8, 15, 25, 20, 18, 12, 8],
                    backgroundColor: 'rgba(34, 197, 94, 0.8)',
                    borderColor: '#22c55e',
                    borderWidth: 0
                },
                {
                    label: 'ปานกลาง',
                    data: [0, 0, 5, 15, 12, 10, 8, 2],
                    backgroundColor: 'rgba(250, 204, 21, 0.8)',
                    borderColor: '#facc15',
                    borderWidth: 0
                },
                {
                    label: 'หนัก',
                    data: [0, 0, 0, 8, 5, 4, 3, 0],
                    backgroundColor: 'rgba(251, 146, 60, 0.8)',
                    borderColor: '#fb923c',
                    borderWidth: 0
                },
                {
                    label: 'สูงสุด',
                    data: [0, 0, 0, 2, 1, 1, 0, 0],
                    backgroundColor: 'rgba(239, 68, 68, 0.8)',
                    borderColor: '#ef4444',
                    borderWidth: 0
                }
            ]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    display: false
                },
                tooltip: {
                    callbacks: {
                        label: function(context) {
                            return context.dataset.label + ': ' + context.parsed.y + ' นาที';
                        }
                    }
                }
            },
            scales: {
                x: {
                    stacked: true,
                    grid: {
                        display: false
                    }
                },
                y: {
                    stacked: true,
                    grid: {
                        color: 'rgba(55, 65, 81, 0.3)'
                    },
                    ticks: {
                        callback: function(value) {
                            return value + ' นาที';
                        }
                    }
                }
            }
        }
    });
}

// Strain Accumulation Chart
const strainAccumulationCtx = document.getElementById('strainAccumulationChart');
if (strainAccumulationCtx) {
    const strainAccumulationChart = new Chart(strainAccumulationCtx, {
        type: 'line',
        data: {
            labels: ['06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00'],
            datasets: [{
                label: 'Strain Score',
                data: [0.2, 1.5, 3.2, 5.8, 8.4, 10.2, 11.8, 12.5, 12.8],
                borderColor: '#fb923c',
                backgroundColor: 'rgba(251, 146, 60, 0.1)',
                fill: true,
                tension: 0.4,
                pointRadius: 4,
                pointHoverRadius: 6,
                borderWidth: 3,
                pointBackgroundColor: '#fb923c',
                pointBorderColor: '#0f172a'
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    display: false
                },
                tooltip: {
                    callbacks: {
                        label: function(context) {
                            return 'Strain: ' + context.parsed.y;
                        }
                    }
                }
            },
            scales: {
                x: {
                    grid: {
                        display: false
                    }
                },
                y: {
                    beginAtZero: true,
                    grid: {
                        color: 'rgba(55, 65, 81, 0.3)'
                    }
                }
            }
        }
    });
}

// Sleep Cycle Chart
const sleepCtx = document.getElementById('sleepChart');
if (sleepCtx) {
    const sleepChart = new Chart(sleepCtx, {
        type: 'line',
        data: {
            labels: ['22:00', '23:00', '00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00'],
            datasets: [{
                label: 'Sleep Stages',
                data: [2, 1, 3, 3, 3, 2, 2, 1, 0],
                borderColor: '#60a5fa',
                backgroundColor: 'rgba(96, 165, 250, 0.1)',
                fill: true,
                tension: 0.4,
                pointRadius: 0,
                pointHoverRadius: 6,
                borderWidth: 2,
                segment: {
                    borderColor: ctx => {
                        const value = ctx.p0.parsed.y;
                        if (value === 0) return '#fbbf24'; // Wake
                        if (value === 1) return '#93c5fd'; // Light
                        if (value === 2) return '#6366f1'; // Deep
                        if (value === 3) return '#a855f7'; // REM
                        return '#60a5fa';
                    },
                    backgroundColor: ctx => {
                        const value = ctx.p0.parsed.y;
                        if (value === 0) return 'rgba(251, 191, 36, 0.1)';
                        if (value === 1) return 'rgba(147, 197, 253, 0.1)';
                        if (value === 2) return 'rgba(99, 102, 241, 0.1)';
                        if (value === 3) return 'rgba(168, 85, 247, 0.1)';
                        return 'rgba(96, 165, 250, 0.1)';
                    }
                }
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    display: false
                },
                tooltip: {
                    callbacks: {
                        label: function(context) {
                            const stages = ['ตื่น', 'หลับตื้น', 'หลับลึก', 'REM'];
                            return stages[context.parsed.y] || '';
                        }
                    }
                }
            },
            scales: {
                x: {
                    grid: {
                        display: false
                    }
                },
                y: {
                    min: 0,
                    max: 3,
                    ticks: {
                        stepSize: 1,
                        callback: function(value) {
                            const stages = ['ตื่น', 'หลับตื้น', 'หลับลึก', 'REM'];
                            return stages[value] || '';
                        }
                    }
                }
            }
        }
    });
}
// HRV Trend Chart
const hrvCtx = document.getElementById('hrvChart');
if (hrvCtx) {
    const hrvChart = new Chart(hrvCtx, {
        type: 'line',
        data: {
            labels: ['จ.', 'อ.', 'พ.', 'พฤ.', 'ศ.', 'ส.', 'อา.'],
            datasets: [{
                label: 'HRV (ms)',
                data: [45, 48, 52, 50, 55, 52, 58],
                borderColor: '#34d399',
                backgroundColor: 'rgba(52, 211, 153, 0.1)',
                fill: true,
                tension: 0.4,
                pointRadius: 4,
                pointHoverRadius: 6,
                borderWidth: 3,
                pointBackgroundColor: '#34d399',
                pointBorderColor: '#0f172a'
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    display: false
                },
                tooltip: {
                    callbacks: {
                        label: function(context) {
                            return `HRV: ${context.parsed.y} ms`;
                        }
                    }
                }
            },
            scales: {
                x: {
                    grid: {
                        display: false
                    }
                },
                y: {
                    beginAtZero: false,
                    grid: {
                        color: 'rgba(55, 65, 81, 0.3)'
                    }
                }
            }
        }
    });
}

// Simulate Real-time Data Updates
function simulateDataUpdate() {
    const recoveryScore = document.querySelector('.metric-card:nth-child(1) .text-3xl');
    const strainScore = document.querySelector('.metric-card:nth-child(2) .text-3xl');
    const sleepScore = document.querySelector('.metric-card:nth-child(3) .text-3xl');
    
    // Add subtle pulse animation to indicate live data
    setInterval(() => {
        document.querySelectorAll('.metric-card').forEach(card => {
            card.classList.add('animate-pulse-glow');
            setTimeout(() => card.classList.remove('animate-pulse-glow'), 2000);
        });
    }, 10000);
}

// Initialize animations on page load
document.addEventListener('DOMContentLoaded', function() {
    // Fade in sections
    const sections = document.querySelectorAll('section');
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.style.opacity = '1';
                entry.target.style.transform = 'translateY(0)';
            }
        });
    });
    
    sections.forEach(section => {
        section.style.opacity = '0';
        section.style.transform = 'translateY(20px)';
        section.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
        observer.observe(section);
    });
    simulateDataUpdate();
    
    // Initialize behavior tracker on journal page
    if (document.getElementById('behaviorTrackerContainer')) {
        initializeBehaviorTracker();
    }
});
// Handle navigation active state
document.addEventListener('DOMContentLoaded', function() {
    const currentPage = window.location.pathname.split('/').pop() || 'index.html';
    const navEvent = new CustomEvent('nav-update', { detail: { page: currentPage } });
    document.dispatchEvent(navEvent);
});

// Behavior Tracking Functions
function initializeBehaviorTracker() {
    const container = document.getElementById('behaviorTrackerContainer');
    if (!container) return;

    // Load saved behaviors
    const savedBehaviors = JSON.parse(localStorage.getItem('selectedBehaviors') || '[]');

    // Generate HTML for each category
    Object.entries(behaviorDatabase).forEach(([category, behaviors]) => {
        const categoryDiv = document.createElement('div');
        categoryDiv.className = 'mb-6';
        
        const categoryHeader = document.createElement('div');
        categoryHeader.className = 'flex items-center justify-between mb-3 cursor-pointer';
        categoryHeader.innerHTML = `
            <h3 class="text-lg font-semibold text-gray-200">${category}</h3>
            <i data-feather="chevron-down" class="w-5 h-5 text-gray-400 transition-transform"></i>
        `;
        
        const behaviorsGrid = document.createElement('div');
        behaviorsGrid.className = 'grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-3 max-h-0 overflow-hidden transition-all duration-300';
        
        behaviors.forEach(behavior => {
            const isSelected = savedBehaviors.includes(behavior);
            const button = document.createElement('button');
            button.className = `behavior-btn p-3 rounded-lg border text-sm transition-all ${
                isSelected 
                    ? 'bg-indigo-500/20 border-indigo-400 text-indigo-400' 
                    : 'bg-gray-900/50 border-gray-600 hover:border-gray-500 text-gray-300'
            }`;
            button.textContent = behavior;
            button.dataset.behavior = behavior;
            
            button.addEventListener('click', function() {
                toggleBehavior(behavior, this);
            });
            
            behaviorsGrid.appendChild(button);
        });

        // Toggle category visibility
        categoryHeader.addEventListener('click', function() {
            const icon = this.querySelector('i');
            if (behaviorsGrid.classList.contains('max-h-0')) {
                behaviorsGrid.classList.remove('max-h-0');
                behaviorsGrid.classList.add('max-h-96');
                icon.style.transform = 'rotate(180deg)';
            } else {
                behaviorsGrid.classList.add('max-h-0');
                behaviorsGrid.classList.remove('max-h-96');
                icon.style.transform = 'rotate(0deg)';
            }
        });

        categoryDiv.appendChild(categoryHeader);
        categoryDiv.appendChild(behaviorsGrid);
        container.appendChild(categoryDiv);
    });

    feather.replace();
}

function toggleBehavior(behavior, button) {
    let savedBehaviors = JSON.parse(localStorage.getItem('selectedBehaviors') || '[]');
    
    if (savedBehaviors.includes(behavior)) {
        // Remove behavior
        savedBehaviors = savedBehaviors.filter(b => b !== behavior);
        button.classList.remove('bg-indigo-500/20', 'border-indigo-400', 'text-indigo-400');
        button.classList.add('bg-gray-900/50', 'border-gray-600', 'hover:border-gray-500', 'text-gray-300');
    } else {
        // Add behavior
        savedBehaviors.push(behavior);
        button.classList.remove('bg-gray-900/50', 'border-gray-600', 'hover:border-gray-500', 'text-gray-300');
        button.classList.add('bg-indigo-500/20', 'border-indigo-400', 'text-indigo-400');
    }
    
    localStorage.setItem('selectedBehaviors', JSON.stringify(savedBehaviors));
    updateBehaviorStats(savedBehaviors);
}

function updateBehaviorStats(selectedBehaviors) {
    const statsContainer = document.getElementById('behaviorStats');
    if (!statsContainer) return;

    const total = selectedBehaviors.length;
    const categories = {};
    
    Object.entries(behaviorDatabase).forEach(([category, behaviors]) => {
        const count = behaviors.filter(b => selectedBehaviors.includes(b)).length;
        if (count > 0) {
            categories[category] = count;
        }
    });

    let statsHTML = `<div class="text-sm text-gray-400">พฤติกรรมที่เลือกวันนี้: <span class="text-indigo-400 font-semibold">${total} รายการ</span></div>`;
    
    if (total > 0) {
        statsHTML += '<div class="mt-2 flex flex-wrap gap-2">';
        Object.entries(categories).forEach(([category, count]) => {
            statsHTML += `<span class="px-2 py-1 bg-gray-700 rounded-full text-xs">${category}: ${count}</span>`;
        });
        statsHTML += '</div>';
    }

    statsContainer.innerHTML = statsHTML;
}