File size: 7,207 Bytes
71e7791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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'
            });
        }
    });
});