// Product data from the provided table const productData = { 'ARA': 7297, 'B4U': 5914, 'BC': 1484, 'BS': 40572, 'CCT': 0, 'CF': 3096, 'JBC': 3875, 'Cream': 541, 'FOAM': 2342, 'PUFF1': 160, 'PUFF2': 138, 'LIP': 2029, 'LST': 15, 'SR': 533, 'SS': 737, 'XD': 6301, 'LS': 19148, 'LZ': 19638, 'M5': 10719, 'MYS': 90, 'PHK': 24604, 'SF': 1101, 'Lotion': 1331, 'Vs': 10228, 'COFFEE': 53465, 'WHEY': 0, 'JAI1': 3663, 'JAI2': 32521 }; // Calculate total and percentages const totalBoxes = 222273; const productEntries = Object.entries(productData).sort((a, b) => b[1] - a[1]); // Initialize charts when DOM is loaded document.addEventListener('DOMContentLoaded', function() { initializeProductChart(); initializeDistributionChart(); populateProductTable(); addAnimations(); }); // Product Bar Chart function initializeProductChart() { const ctx = document.getElementById('productChart').getContext('2d'); const topProducts = productEntries.slice(0, 10); new Chart(ctx, { type: 'bar', data: { labels: topProducts.map(([name]) => name), datasets: [{ label: 'จำนวนที่ขาย (กล่อง)', data: topProducts.map(([, value]) => value), backgroundColor: [ 'rgba(16, 185, 129, 0.8)', 'rgba(234, 179, 8, 0.8)', 'rgba(59, 130, 246, 0.8)', 'rgba(168, 85, 247, 0.8)', 'rgba(236, 72, 153, 0.8)', 'rgba(34, 197, 94, 0.8)', 'rgba(251, 146, 60, 0.8)', 'rgba(147, 51, 234, 0.8)', 'rgba(239, 68, 68, 0.8)', 'rgba(14, 165, 233, 0.8)' ], borderColor: [ 'rgb(16, 185, 129)', 'rgb(234, 179, 8)', 'rgb(59, 130, 246)', 'rgb(168, 85, 247)', 'rgb(236, 72, 153)', 'rgb(34, 197, 94)', 'rgb(251, 146, 60)', 'rgb(147, 51, 234)', 'rgb(239, 68, 68)', 'rgb(14, 165, 233)' ], borderWidth: 1 }] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false }, tooltip: { callbacks: { label: function(context) { return `จำนวน: ${context.parsed.y.toLocaleString()} กล่อง`; } } } }, scales: { y: { beginAtZero: true, ticks: { callback: function(value) { return value.toLocaleString(); } } } } } }); } // Distribution Pie Chart function initializeDistributionChart() { const ctx = document.getElementById('distributionChart').getContext('2d'); const topProducts = productEntries.slice(0, 8); const others = productEntries.slice(8).reduce((sum, [, value]) => sum + value, 0); const chartData = [ ...topProducts.map(([name, value]) => ({ name, value })), { name: 'อื่นๆ', value: others } ]; new Chart(ctx, { type: 'doughnut', data: { labels: chartData.map(({ name }) => name), datasets: [{ data: chartData.map(({ value }) => value), backgroundColor: [ 'rgba(16, 185, 129, 0.8)', 'rgba(234, 179, 8, 0.8)', 'rgba(59, 130, 246, 0.8)', 'rgba(168, 85, 247, 0.8)', 'rgba(236, 72, 153, 0.8)', 'rgba(34, 197, 94, 0.8)', 'rgba(251, 146, 60, 0.8)', 'rgba(147, 51, 234, 0.8)', 'rgba(156, 163, 175, 0.8)' ], borderColor: [ 'rgb(16, 185, 129)', 'rgb(234, 179, 8)', 'rgb(59, 130, 246)', 'rgb(168, 85, 247)', 'rgb(236, 72, 153)', 'rgb(34, 197, 94)', 'rgb(251, 146, 60)', 'rgb(147, 51, 234)', 'rgb(156, 163, 175)' ], borderWidth: 1 }] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'right', labels: { padding: 15, font: { size: 11 } } }, tooltip: { callbacks: { label: function(context) { const percentage = ((context.parsed / totalBoxes) * 100).toFixed(1); return `${context.label}: ${context.parsed.toLocaleString()} กล่อง (${percentage}%)`; } } } } } }); } // Populate product table function populateProductTable() { const tableBody = document.getElementById('productTableBody'); productEntries.slice(0, 10).forEach(([name, value], index) => { const percentage = ((value / totalBoxes) * 100).toFixed(2); const row = document.createElement('tr'); row.className = 'table-row-hover'; // Determine status badge let statusBadge = ''; if (value > 20000) { statusBadge = 'ขายดี'; } else if (value > 5000) { statusBadge = 'ปานกลาง'; } else if (value > 0) { statusBadge = 'ต่ำ'; } else { statusBadge = 'ไม่มียอดขาย'; } row.innerHTML = ` ${name} ${value.toLocaleString()} ${percentage}% ${statusBadge} `; tableBody.appendChild(row); }); // Update counts document.getElementById('showingCount').textContent = '10'; document.getElementById('totalCount').textContent = productEntries.length.toString(); } // Add animations to elements function addAnimations() { const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('animate-slide-in'); } }); }); document.querySelectorAll('.bg-white').forEach(el => { observer.observe(el); }); } // Export functionality document.addEventListener('DOMContentLoaded', function() { const exportBtn = document.querySelector('button:has(.feather-download)'); if (exportBtn) { exportBtn.addEventListener('click', function() { alert('กำลังส่งออกรายงาน...'); // Here you would implement actual export functionality }); } }); // Search functionality document.addEventListener('DOMContentLoaded', function() { const searchInput = document.querySelector('input[placeholder="ค้นหาสินค้า..."]'); if (searchInput) { searchInput.addEventListener('input', function(e) { const searchTerm = e.target.value.toLowerCase(); // Here you would implement actual search functionality }); } });