File size: 11,474 Bytes
0e61b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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

// Navigation functionality
document.addEventListener('DOMContentLoaded', function() {
    // Initialize navigation
    const navItems = document.querySelectorAll('.nav-item');
    const views = document.querySelectorAll('.view');
    
    // Add click listeners to navigation items
    navItems.forEach(item => {
        item.addEventListener('click', function() {
            const targetView = this.getAttribute('data-view');
            
            // Hide all views
            views.forEach(view => {
                view.classList.add('hidden');
                view.classList.remove('active');
            });
            
            // Show target view
            const targetViewElement = document.getElementById(targetView);
            if (targetViewElement) {
                targetViewElement.classList.remove('hidden');
                targetViewElement.classList.add('active');
            }
            
            // Update navigation active states
            navItems.forEach(navItem => {
                navItem.classList.remove('text-green-600');
                navItem.classList.add('text-gray-400');
            });
            this.classList.remove('text-gray-400');
            this.classList.add('text-green-600');
            
            // Re-initialize feather icons for the new view
            feather.replace();
        });
    });
    
    // Initialize charts for graph view
    initializeCharts();
    
    // Handle checkbox functionality in plan view
    handleTaskCheckboxes();
    
    // Handle analyze button
    handleAnalyzeButton();
    
    // Handle product buttons
    handleProductButtons();
    
    // Handle BioCoach buttons
    handleBioCoachButtons();
});

// Chart initialization
function initializeCharts() {
    // HRV Chart
    const hrvCtx = document.getElementById('hrvChart');
    if (hrvCtx) {
        new Chart(hrvCtx, {
            type: 'line',
            data: {
                labels: ['จ', 'อ', 'พ', 'พฤ', 'ศ', 'ส', 'อา'],
                datasets: [{
                    label: 'HRV',
                    data: [45, 47, 46, 48, 47, 49, 47],
                    borderColor: '#10B981',
                    backgroundColor: 'rgba(16, 185, 129, 0.1)',
                    tension: 0.4
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        display: false
                    }
                },
                scales: {
                    y: {
                        beginAtZero: false,
                        min: 40,
                        max: 55
                    }
                }
            }
        });
    }
    
    // Sleep Chart
    const sleepCtx = document.getElementById('sleepChart');
    if (sleepCtx) {
        new Chart(sleepCtx, {
            type: 'line',
            data: {
                labels: ['จ', 'อ', 'พ', 'พฤ', 'ศ', 'ส', 'อา'],
                datasets: [{
                    label: 'คุณภาพการนอน',
                    data: [75, 78, 72, 80, 76, 79, 78],
                    borderColor: '#3B82F6',
                    backgroundColor: 'rgba(59, 130, 246, 0.1)',
                    tension: 0.4
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        display: false
                    }
                },
                scales: {
                    y: {
                        beginAtZero: false,
                        min: 60,
                        max: 100
                    }
                }
            }
        });
    }
    
    // Weight Chart
    const weightCtx = document.getElementById('weightChart');
    if (weightCtx) {
        new Chart(weightCtx, {
            type: 'line',
            data: {
                labels: ['จ', 'อ', 'พ', 'พฤ', 'ศ', 'ส', 'อา'],
                datasets: [{
                    label: 'น้ำหนัก',
                    data: [72.8, 72.5, 72.6, 72.3, 72.4, 72.2, 72.3],
                    borderColor: '#8B5CF6',
                    backgroundColor: 'rgba(139, 92, 246, 0.1)',
                    tension: 0.4
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        display: false
                    }
                },
                scales: {
                    y: {
                        beginAtZero: false,
                        min: 71,
                        max: 74
                    }
                }
            }
        });
    }
    
    // Body Fat Chart
    const fatCtx = document.getElementById('fatChart');
    if (fatCtx) {
        new Chart(fatCtx, {
            type: 'line',
            data: {
                labels: ['จ', 'อ', 'พ', 'พฤ', 'ศ', 'ส', 'อา'],
                datasets: [{
                    label: '% ไขมัน',
                    data: [27.5, 27.3, 27.4, 27.1, 27.2, 27.0, 27.0],
                    borderColor: '#F97316',
                    backgroundColor: 'rgba(249, 115, 22, 0.1)',
                    tension: 0.4
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        display: false
                    }
                },
                scales: {
                    y: {
                        beginAtZero: false,
                        min: 26,
                        max: 28
                    }
                }
            }
        });
    }
}

// Handle task checkboxes in plan view
function handleTaskCheckboxes() {
    const checkboxes = document.querySelectorAll('#plan-view input[type="checkbox"]');
    checkboxes.forEach(checkbox => {
        checkbox.addEventListener('change', function() {
            const label = this.nextElementSibling;
            if (this.checked) {
                label.classList.add('line-through');
            } else {
                label.classList.remove('line-through');
            }
            
            // Check if all tasks for today are completed
            const todayTasks = this.closest('.bg-white').querySelectorAll('input[type="checkbox"]');
            const allChecked = Array.from(todayTasks).every(cb => cb.checked);
            const statusBadge = this.closest('.bg-white').querySelector('.bg-yellow-100');
            
            if (statusBadge && allChecked) {
                statusBadge.classList.remove('bg-yellow-100', 'text-yellow-600');
                statusBadge.classList.add('bg-green-100', 'text-green-600');
                statusBadge.textContent = 'เสร็จสิ้น';
            }
        });
    });
}

// Handle analyze button
function handleAnalyzeButton() {
    const analyzeBtn = document.querySelector('.bg-green-500.text-white.rounded-xl.py-4');
    if (analyzeBtn) {
        analyzeBtn.addEventListener('click', function() {
            this.innerHTML = '<span class="spinner inline-block w-4 h-4 border-2 border-white border-t-transparent rounded-full mr-2"></span>กำลังวิเคราะห์...';
            this.disabled = true;
            
            setTimeout(() => {
                this.innerHTML = '✅ วิเคราะห์เสร็จสิ้น';
                this.classList.remove('bg-green-500');
                this.classList.add('bg-green-600');
                
                // Simulate data update
                updateHealthMetrics();
                
                setTimeout(() => {
                    this.innerHTML = 'วิเคราะห์สุขภาพอัตโนมัติใหม่';
                    this.classList.remove('bg-green-600');
                    this.classList.add('bg-green-500');
                    this.disabled = false;
                }, 2000);
            }, 3000);
        });
    }
}

// Update health metrics with animation
function updateHealthMetrics() {
    const metrics = {
        hrv: { element: null, value: 47, newValue: 48 },
        sleep: { element: null, value: 78, newValue: 80 },
        fat: { element: null, value: 27, newValue: 26.8 }
    };
    
    // Find metric elements
    document.querySelectorAll('.text-xl.font-semibold').forEach(el => {
        const parent = el.parentElement;
        if (parent.textContent.includes('HRV')) {
            metrics.hrv.element = el;
        } else if (parent.textContent.includes('คุณภาพการนอน')) {
            metrics.sleep.element = el;
        } else if (parent.textContent.includes('% ไขมัน')) {
            metrics.fat.element = el;
        }
    });
    
    // Animate updates
    Object.values(metrics).forEach(metric => {
        if (metric.element) {
            animateValue(metric.element, metric.value, metric.newValue, 1000);
        }
    });
}

// Animate number changes
function animateValue(element, start, end, duration) {
    const range = end - start;
    const startTime = performance.now();
    
    function update(currentTime) {
        const elapsed = currentTime - startTime;
        const progress = Math.min(elapsed / duration, 1);
        const current = start + (range * progress);
        
        element.textContent = current.toFixed(1);
        
        if (progress < 1) {
            requestAnimationFrame(update);
        }
    }
    
    requestAnimationFrame(update);
}

// Handle product buttons
function handleProductButtons() {
    const detailButtons = document.querySelectorAll('button:contains("ดูรายละเอียด")');
    const cartButtons = document.querySelectorAll('button:contains("เพิ่มลงตะกร้า")');
    
    detailButtons.forEach(button => {
        button.addEventListener('click', function() {
            alert('แสดงรายละเอียดสินค้า...');
        });
    });
    
    cartButtons.forEach(button => {
        button.addEventListener('click', function() {
            const originalText = this.innerHTML;
            this.innerHTML = '✅ เพิ่มแล้ว';
            this.classList.remove('bg-green-500');
            this.classList.add('bg-green-600');
            
            setTimeout(() => {
                this.innerHTML = originalText;
                this.classList.remove('bg-green-600');
                this.classList.add('bg-green-500');
            }, 2000);
        });
    });
}

// Handle BioCoach buttons
function handleBioCoachButtons() {
    const chatBtn = document.querySelector('button:contains("แชท")');
    const callBtn = document.querySelector('button:contains("นัดคอล")');
    
    if (chatBtn) {
        chatBtn.addEventListener('click', function() {
            alert('เปิดหน้าต่างแชทกับ BioCoach...');
        });
    }
    
    if (callBtn) {
        callBtn.addEventListener('click', function() {
            alert('เปิดหน้าจอนัดหมายวิดีโอคอล...');
        });
    }
}