wellnet-bioage / script.js
CSNET's picture
ทำต่อให้เสร็จ
0e61b0f verified
Raw History Blame Contribute Delete
11.5 kB
// 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('เปิดหน้าจอนัดหมายวิดีโอคอล...');
});
}
}