File size: 9,665 Bytes
df62a7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class VitalCircle extends HTMLElement {
    constructor() {
        super();
        this.value = this.getAttribute('value') || 0;
        this.max = this.getAttribute('max') || 100;
        this.label = this.getAttribute('label') || '';
        this.sublabel = this.getAttribute('sublabel') || '';
        this.color = this.getAttribute('color') || 'blue';
    }
    
    connectedCallback() {
        this.attachShadow({ mode: 'open' });
        this.render();
    }
    
    static get observedAttributes() {
        return ['value', 'max', 'label', 'sublabel', 'color'];
    }
    
    attributeChangedCallback(name, oldValue, newValue) {
        if (oldValue !== newValue) {
            this[name] = newValue;
            this.render();
        }
    }
    
    getColorClasses() {
        const colors = {
            green: {
                stroke: '#10b981',
                fill: '#10b98120',
                text: 'text-green-600',
                bg: 'bg-green-50'
            },
            yellow: {
                stroke: '#f59e0b',
                fill: '#f59e0b20',
                text: 'text-yellow-600',
                bg: 'bg-yellow-50'
            },
            red: {
                stroke: '#ef4444',
                fill: '#ef444420',
                text: 'text-red-600',
                bg: 'bg-red-50'
            },
            blue: {
                stroke: '#3b82f6',
                fill: '#3b82f620',
                text: 'text-blue-600',
                bg: 'bg-blue-50'
            },
            purple: {
                stroke: '#8b5cf6',
                fill: '#8b5cf620',
                text: 'text-purple-600',
                bg: 'bg-purple-50'
            }
        };
        
        return colors[this.color] || colors.blue;
    }
    
    render() {
        const colors = this.getColorClasses();
        const percentage = (this.value / this.max) * 100;
        const circumference = 2 * Math.PI * 45;
        const strokeDashoffset = circumference - (percentage / 100) * circumference;
        
        this.shadowRoot.innerHTML = `
            <style>
                .vital-container {
                    text-align: center;
                    background: white;
                    border-radius: 16px;
                    padding: 16px;
                    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
                    transition: all 0.3s ease;
                    cursor: pointer;
                }
                .vital-container:hover {
                    transform: translateY(-2px);
                    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
                }
                .circle-svg {
                    width: 120px;
                    height: 120px;
                    transform: rotate(-90deg);
                    margin: 0 auto 8px;
                }
                .circle-background {
                    fill: none;
                    stroke: #e5e7eb;
                    stroke-width: 8;
                }
                .circle-progress {
                    fill: none;
                    stroke: ${colors.stroke};
                    stroke-width: 8;
                    stroke-linecap: round;
                    transition: stroke-dashoffset 0.5s ease;
                }
                .vital-value {
                    font-size: 24px;
                    font-weight: bold;
                    margin: -60px 0 8px 0;
                    color: #1f2937;
                }
                .vital-label {
                    font-size: 14px;
                    font-weight: 600;
                    color: #4b5563;
                    margin-bottom: 2px;
                }
                .vital-sublabel {
                    font-size: 12px;
                    color: ${colors.stroke};
                }
                .pulse {
                    animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
                }
                @keyframes pulse {
                    0%, 100% {
                        opacity: 1;
                    }
                    50% {
                        opacity: 0.7;
                    }
                }
            </style>
            
            <div class="vital-container">
                <svg class="circle-svg">
                    <circle cx="60" cy="60" r="45" class="circle-background"></circle>
                    <circle 
                        cx="60" cy="60" r="45" 
                        class="circle-progress pulse"
                        style="stroke-dasharray: ${circumference}; stroke-dashoffset: ${strokeDashoffset};"
                    ></circle>
                </svg>
                <div class="vital-value">${this.value}</div>
                <div class="vital-label">${this.label}</div>
                <div class="vital-sublabel">${this.sublabel}</div>
            </div>
        `;
        
        // Add click event
        this.shadowRoot.querySelector('.vital-container').addEventListener('click', () => {
            this.showDetails();
        });
    }
    
    showDetails() {
        // Create detail modal
        const modal = document.createElement('div');
        modal.className = 'modal';
        modal.style.display = 'flex';
        
        const colors = this.getColorClasses();
        const percentage = Math.round((this.value / this.max) * 100);
        
        modal.innerHTML = `
            <div class="modal-content">
                <div class="text-center mb-4">
                    <div class="text-4xl font-bold ${colors.text} mb-2">${this.value}</div>
                    <div class="text-lg font-semibold mb-1">${this.label}</div>
                    <div class="text-sm text-gray-500">${this.sublabel}</div>
                </div>
                
                <div class="mb-4">
                    <div class="flex justify-between text-sm mb-2">
                        <span>ความคืบหน้า</span>
                        <span>${percentage}%</span>
                    </div>
                    <div class="w-full bg-gray-200 rounded-full h-3">
                        <div class="${colors.text.replace('text', 'bg')} h-3 rounded-full transition-all" style="width: ${percentage}%"></div>
                    </div>
                </div>
                
                <div class="mb-4">
                    <div class="flex justify-between text-sm mb-1">
                        <span>ปัจจุบัน</span>
                        <span class="font-semibold">${this.value}</span>
                    </div>
                    <div class="flex justify-between text-sm">
                        <span>เป้าหมาย</span>
                        <span class="font-semibold">${this.max}</span>
                    </div>
                </div>
                
                <div class="bg-gray-50 rounded-lg p-3 mb-4">
                    <div class="text-sm text-gray-600 mb-2">ข้อมูลเพิ่มเติม:</div>
                    <div class="text-sm">
                        ${this.getDetailedInfo()}
                    </div>
                </div>
                
                <button class="w-full bg-indigo-600 text-white rounded-lg py-2 font-semibold hover:bg-indigo-700 transition" onclick="this.closest('.modal').remove()">
                    ปิด
                </button>
            </div>
        `;
        
        document.body.appendChild(modal);
        
        // Close modal when clicking outside
        modal.addEventListener('click', function(e) {
            if (e.target === modal) {
                modal.remove();
            }
        });
    }
    
    getDetailedInfo() {
        const info = {
            Recovery: {
                text: 'Recovery Score คือการวัดความพร้อมของร่างกาย คำนวณจาก HRV, RHR, และคุณภาพการนอน',
                tips: [
                    '70%+ = พร้อมฝึกหนัก',
                    '40-69% = ฝึกปานกลาง',
                    'ต่ำกว่า 40% = พักผ่อน'
                ]
            },
            Strain: {
                text: 'Strain คือปริมาณของการใช้พลังงานรวมทั้งวัน (0-21)',
                tips: [
                    '0-9 = การฟื้นตัว',
                    '10-13 = ฝึกปานกลาง',
                    '14-17 = ฝึกหนัก',
                    '18+ = ฝึกหนักมาก'
                ]
            },
            Sleep: {
                text: 'ชั่วโมงการนอนที่แนะนำสำหรับผู้ใหญ่คือ 7-9 ชั่วโมง',
                tips: [
                    'คุณภาพสำคัญกว่าปริมาณ',
                    'Deep Sleep ควรเป็น 15-20%',
                    'REM Sleep ควรเป็น 20-25%'
                ]
            }
        };
        
        const details = info[this.label] || { text: '', tips: [] };
        let html = `<p>${details.text}</p>`;
        
        if (details.tips.length > 0) {
            html += '<ul class="mt-2 space-y-1">';
            details.tips.forEach(tip => {
                html += `<li class="flex items-start"><span class="mr-2">•</span><span>${tip}</span></li>`;
            });
            html += '</ul>';
        }
        
        return html;
    }
}

customElements.define('vital-circle', VitalCircle);