// Global variables let currentTab = 1; const charts = {}; // Initialize the app document.addEventListener('DOMContentLoaded', function() { initializeCharts(); feather.replace(); showTab(currentTab); }); // Tab switching functionality function switchTab(tabNumber) { currentTab = tabNumber; showTab(tabNumber); updateNavigation(tabNumber); } function showTab(tabNumber) { // Hide all tabs const allTabs = document.querySelectorAll('.tab-content'); allTabs.forEach(tab => tab.classList.add('hidden')); // Show selected tab const selectedTab = document.getElementById(`tab${tabNumber}`); if (selectedTab) { selectedTab.classList.remove('hidden'); } } function updateNavigation(activeTab) { const navButtons = document.querySelectorAll('.nav-btn'); navButtons.forEach(btn => { if (parseInt(btn.dataset.tab) === activeTab) { btn.classList.add('text-green-600'); btn.classList.remove('text-gray-600'); } else { btn.classList.remove('text-green-600'); btn.classList.add('text-gray-600'); } }); } // Initialize charts function initializeCharts() { // Yield Chart (Bar Chart) const yieldCanvas = document.getElementById('yieldChart'); if (yieldCanvas) { const ctx = yieldCanvas.getContext('2d'); drawBarChart(ctx, yieldCanvas, { labels: ['ปีที่ 1', 'ปีที่ 2', 'ปีที่ 3'], datasets: [ { label: 'ก่อนใช้ใจเกษตร', data: [3.2, 3.3, 3.1], color: '#ef4444' }, { label: 'หลังใช้ใจเกษตร', data: [4.2, 4.5, 4.6], color: '#22c55e' } ] }); } // Cost Chart (Pie Chart) const costCanvas = document.getElementById('costChart'); if (costCanvas) { const ctx = costCanvas.getContext('2d'); drawPieChart(ctx, costCanvas, { labels: ['ปุ๋ยเคมีเดิม', 'ใจเกษตร', 'อื่นๆ'], data: [60, 25, 15], colors: ['#ef4444', '#22c55e', '#e5e7eb'] }); } // Price Chart (Line Chart) const priceCanvas = document.getElementById('priceChart'); if (priceCanvas) { const ctx = priceCanvas.getContext('2d'); drawLineChart(ctx, priceCanvas, { labels: Array.from({length: 30}, (_, i) => `วันที่ ${i+1}`), data: [14.2, 14.3, 14.5, 14.4, 14.6, 14.5, 14.7, 14.8, 14.9, 14.8, 14.9, 15.0, 15.1, 15.0, 14.9, 14.8, 14.7, 14.9, 15.0, 15.1, 15.2, 15.3, 15.2, 15.1, 15.0, 14.9, 15.0, 15.1, 15.2, 15.0], color: '#16a34a' }); } } // Simple bar chart drawing function drawBarChart(ctx, canvas, data) { const width = canvas.width; const height = canvas.height; const padding = 40; const barWidth = (width - padding * 2) / (data.labels.length * 3); const maxValue = 5; ctx.clearRect(0, 0, width, height); // Draw axes ctx.strokeStyle = '#e5e7eb'; ctx.beginPath(); ctx.moveTo(padding, padding); ctx.lineTo(padding, height - padding); ctx.lineTo(width - padding, height - padding); ctx.stroke(); // Draw bars data.labels.forEach((label, i) => { const x = padding + i * barWidth * 3 + barWidth / 2; // Draw bars for each dataset data.datasets.forEach((dataset, j) => { const value = dataset.data[i]; const barHeight = (value / maxValue) * (height - padding * 2); const barX = x + j * barWidth; const barY = height - padding - barHeight; ctx.fillStyle = dataset.color; ctx.fillRect(barX, barY, barWidth - 5, barHeight); }); // Draw labels ctx.fillStyle = '#374151'; ctx.font = '12px Prompt'; ctx.textAlign = 'center'; ctx.fillText(label, x + barWidth, height - padding + 20); }); // Draw legend ctx.font = '12px Prompt'; let legendY = 20; data.datasets.forEach((dataset) => { ctx.fillStyle = dataset.color; ctx.fillRect(width - 150, legendY - 10, 15, 15); ctx.fillStyle = '#374151'; ctx.textAlign = 'left'; ctx.fillText(dataset.label, width - 130, legendY); legendY += 25; }); } // Simple pie chart drawing function drawPieChart(ctx, canvas, data) { const width = canvas.width; const height = canvas.height; const centerX = width / 2; const centerY = height / 2; const radius = Math.min(width, height) / 2 - 40; const total = data.data.reduce((a, b) => a + b, 0); let currentAngle = -Math.PI / 2; ctx.clearRect(0, 0, width, height); // Draw pie slices data.data.forEach((value, i) => { const sliceAngle = (value / total) * 2 * Math.PI; ctx.beginPath(); ctx.arc(centerX, centerY, radius, currentAngle, currentAngle + sliceAngle); ctx.lineTo(centerX, centerY); ctx.fillStyle = data.colors[i]; ctx.fill(); // Draw percentage const percentage = ((value / total) * 100).toFixed(1) + '%'; const textAngle = currentAngle + sliceAngle / 2; const textX = centerX + Math.cos(textAngle) * (radius / 2); const textY = centerY + Math.sin(textAngle) * (radius / 2); ctx.fillStyle = 'white'; ctx.font = 'bold 14px Prompt'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(percentage, textX, textY); currentAngle += sliceAngle; }); // Draw legend ctx.font = '12px Prompt'; let legendY = 20; data.labels.forEach((label, i) => { ctx.fillStyle = data.colors[i]; ctx.fillRect(10, legendY - 10, 15, 15); ctx.fillStyle = '#374151'; ctx.textAlign = 'left'; ctx.fillText(`${label}: ${data.data[i]}%`, 30, legendY); legendY += 25; }); } // Simple line chart drawing function drawLineChart(ctx, canvas, data) { const width = canvas.width; const height = canvas.height; const padding = 40; const pointSpacing = (width - padding * 2) / (data.labels.length - 1); const maxValue = Math.max(...data.data); const minValue = Math.min(...data.data); const valueRange = maxValue - minValue; ctx.clearRect(0, 0, width, height); // Draw axes ctx.strokeStyle = '#e5e7eb'; ctx.beginPath(); ctx.moveTo(padding, padding); ctx.lineTo(padding, height - padding); ctx.lineTo(width - padding, height - padding); ctx.stroke(); // Draw line ctx.strokeStyle = data.color; ctx.lineWidth = 2; ctx.beginPath(); data.data.forEach((value, i) => { const x = padding + i * pointSpacing; const y = height - padding - ((value - minValue) / valueRange) * (height - padding * 2); if (i === 0) { ctx.moveTo(x, y); } else { ctx.lineTo(x, y); } }); ctx.stroke(); // Draw points data.data.forEach((value, i) => { const x = padding + i * pointSpacing; const y = height - padding - ((value - minValue) / valueRange) * (height - padding * 2); ctx.beginPath(); ctx.arc(x, y, 3, 0, Math.PI * 2); ctx.fillStyle = data.color; ctx.fill(); }); // Draw Y-axis labels ctx.fillStyle = '#6b7280'; ctx.font = '10px Prompt'; ctx.textAlign = 'right'; for (let i = 0; i <= 5; i++) { const value = minValue + (valueRange * i / 5); const y = height - padding - (i / 5) * (height - padding * 2); ctx.fillText(value.toFixed(1) + 'k', padding - 10, y + 3); } // Draw X-axis label ctx.fillStyle = '#6b7280'; ctx.font = '10px Prompt'; ctx.textAlign = 'center'; ctx.fillText('30 วันย้อนหลัง', width / 2, height - 10); } // Modal functions function showNotifications() { const modal = document.getElementById('notificationModal'); modal.classList.remove('hidden'); modal.classList.add('flex'); } function closeNotifications() { const modal = document.getElementById('notificationModal'); modal.classList.add('hidden'); modal.classList.remove('flex'); } function showProfile() { switchTab(8); } // Action functions function calculateYield() { const modal = document.createElement('div'); modal.className = 'fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center'; modal.innerHTML = `

คำนวณผลผลิตใหม่

`; document.body.appendChild(modal); } function calculateNewYield(button) { button.innerHTML = '
'; setTimeout(() => { button.closest('.fixed').remove(); showSuccessToast('คำนวณผลผลิตสำเร็จ! คาดว่าผลผลิตเพิ่มขึ้น 35%'); }, 1000); } function viewNationalPrices() { showSuccessToast('กำลังโหลดข้อมูลราคาทั่วประเทศ...'); } function downloadPDF() { showSuccessToast('กำลังดาวน์โหลด Crop Cycle เป็น PDF...'); } function addProduct() { const modal = document.createElement('div'); modal.className = 'fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center'; modal.innerHTML = `

เพิ่มผลผลิตใหม่

`; document.body.appendChild(modal); } function submitProduct(button) { button.innerHTML = '
'; setTimeout(() => { button.closest('.fixed').remove(); showSuccessToast('เพิ่มผลผลิตสำเร็จ! กำลังจับคู่ผู้ซื้อ...'); }, 1000); } function becomeCoach() { showSuccessToast('กำลังดำเนินการสมัคร... ทีมงานจะติดต่อกลับโดยเร็ว!'); } // Toast notification function showSuccessToast(message) { const toast = document.createElement('div'); toast.className = 'fixed top-20 left-4 right-4 bg-green-600 text-white p-4 rounded-lg shadow-lg z-50 success-animation'; toast.innerHTML = `
${message}
`; document.body.appendChild(toast); feather.replace(); setTimeout(() => { toast.remove(); }, 3000); } // Simulate real-time price updates setInterval(() => { const priceElements = document.querySelectorAll('.text-green-600, .text-red-600'); priceElements.forEach(el => { if (el.textContent.includes('↑') || el.textContent.includes('↓')) { // Simulate price change animation el.style.transform = 'scale(1.1)'; setTimeout(() => { el.style.transform = 'scale(1)'; }, 200); } }); }, 5000); // Pull to refresh functionality let startY = 0; let isPulling = false; document.addEventListener('touchstart', (e) => { if (window.scrollY === 0) { startY = e.touches[0].pageY; isPulling = true; } }); document.addEventListener('touchmove', (e) => { if (!isPulling) return; const currentY = e.touches[0].pageY; const pullDistance = currentY - startY; if (pullDistance > 100 && window.scrollY === 0) { // Show refresh indicator showRefreshIndicator(); } }); document.addEventListener('touchend', () => { if (isPulling) { isPulling = false; hideRefreshIndicator(); } }); function showRefreshIndicator() { const indicator = document.createElement('div'); indicator.id = 'refreshIndicator'; indicator.className = 'fixed top-16 left-0 right-0 bg-green-600 text-white text-center py-2 z-40'; indicator.textContent = 'กำลังรีเฟรชข้อมูล...'; document.body.appendChild(indicator); } function hideRefreshIndicator() { const indicator = document.getElementById('refreshIndicator'); if (indicator) { indicator.remove(); } }