File size: 8,404 Bytes
24834c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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 = '<span class="badge-success">ขายดี</span>';
        } else if (value > 5000) {
            statusBadge = '<span class="badge-warning">ปานกลาง</span>';
        } else if (value > 0) {
            statusBadge = '<span class="badge-danger">ต่ำ</span>';
        } else {
            statusBadge = '<span class="text-gray-400">ไม่มียอดขาย</span>';
        }
        
        row.innerHTML = `
            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">${name}</td>
            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">${value.toLocaleString()}</td>
            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">${percentage}%</td>
            <td class="px-6 py-4 whitespace-nowrap text-sm">${statusBadge}</td>
        `;
        
        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
        });
    }
});