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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// 全局变量
let refreshInterval = 60; // 默认刷新间隔(秒)
let autoRefreshEnabled = true; // 默认启用自动刷新
let chartInstances = {}; // 存储图表实例的对象
let darkModeEnabled = localStorage.getItem('theme') === 'dark'; // 深色模式状态

// 格式化大数值的函数
function formatChartNumber(value) {
    if (value >= 1000000000) {
        return (value / 1000000000).toFixed(1) + 'G';
    } else if (value >= 1000000) {
        return (value / 1000000).toFixed(1) + 'M';
    } else if (value >= 1000) {
        return (value / 1000).toFixed(1) + 'K';
    }
    return value;
}

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 初始化图表
    initializeCharts();
    
    // 设置自动刷新
    setupAutoRefresh();
    
    // 主题切换
    setupThemeToggle();
    
    // 加载保存的主题
    loadSavedTheme();
    
    // 添加表格交互功能
    enhanceTableInteraction();
    
    // 添加保存统计数据按钮事件
    setupSaveStatsButton();
    
    // 更新页脚信息
    updateFooterInfo();
    
    // 表格排序和筛选
    const table = document.getElementById('history-table');
    if (table) {
        const headers = table.querySelectorAll('th[data-sort]');
        const rows = Array.from(table.querySelectorAll('tbody tr'));
        const rowsPerPage = 10;
        let currentPage = 1;
        let filteredRows = [...rows];
        
        // 初始化分页
        function initPagination() {
            const totalPages = Math.ceil(filteredRows.length / rowsPerPage);
            document.getElementById('total-pages').textContent = totalPages;
            document.getElementById('current-page').textContent = currentPage;
            document.getElementById('prev-page').disabled = currentPage === 1;
            document.getElementById('next-page').disabled = currentPage === totalPages || totalPages === 0;
            
            // 显示当前页的行
            const startIndex = (currentPage - 1) * rowsPerPage;
            const endIndex = startIndex + rowsPerPage;
            
            rows.forEach(row => row.style.display = 'none');
            filteredRows.slice(startIndex, endIndex).forEach(row => row.style.display = '');
        }
        
        // 排序功能
        headers.forEach(header => {
            header.addEventListener('click', () => {
                const sortBy = header.getAttribute('data-sort');
                const isAscending = header.classList.contains('asc');
                
                // 移除所有排序指示器
                headers.forEach(h => {
                    h.classList.remove('asc', 'desc');
                    h.querySelector('i').className = 'fas fa-sort';
                });
                
                // 设置当前排序方向
                if (isAscending) {
                    header.classList.add('desc');
                    header.querySelector('i').className = 'fas fa-sort-down';
                } else {
                    header.classList.add('asc');
                    header.querySelector('i').className = 'fas fa-sort-up';
                }
                
                // 排序行
                filteredRows.sort((a, b) => {
                    let aValue, bValue;
                    
                    if (sortBy === 'id') {
                        aValue = a.cells[0].getAttribute('title');
                        bValue = b.cells[0].getAttribute('title');
                    } else if (sortBy === 'timestamp') {
                        aValue = a.cells[1].textContent;
                        bValue = b.cells[1].textContent;
                    } else if (sortBy === 'duration' || sortBy === 'total') {
                        const aText = a.cells[sortBy === 'duration' ? 5 : 6].textContent;
                        const bText = b.cells[sortBy === 'duration' ? 5 : 6].textContent;
                        aValue = aText === '-' ? 0 : parseInt(aText.replace(/,/g, '').replace(/[KMG]/g, ''));
                        bValue = bText === '-' ? 0 : parseInt(bText.replace(/,/g, '').replace(/[KMG]/g, ''));
                    } else {
                        aValue = a.cells[sortBy === 'model' ? 2 : (sortBy === 'account' ? 3 : 4)].textContent;
                        bValue = b.cells[sortBy === 'model' ? 2 : (sortBy === 'account' ? 3 : 4)].textContent;
                    }
                    
                    if (aValue < bValue) return isAscending ? -1 : 1;
                    if (aValue > bValue) return isAscending ? 1 : -1;
                    return 0;
                });
                
                // 更新显示
                currentPage = 1;
                initPagination();
            });
        });
        
        // 搜索功能
        const searchInput = document.getElementById('history-search');
        if (searchInput) {
            searchInput.addEventListener('input', function() {
                const searchTerm = this.value.toLowerCase();
                
                filteredRows = rows.filter(row => {
                    const rowText = Array.from(row.cells).map(cell => cell.textContent.toLowerCase()).join(' ');
                    return rowText.includes(searchTerm);
                });
                
                currentPage = 1;
                initPagination();
            });
        }
        
        // 分页控制
        const prevPageBtn = document.getElementById('prev-page');
        const nextPageBtn = document.getElementById('next-page');
        
        if (prevPageBtn) {
            prevPageBtn.addEventListener('click', () => {
                if (currentPage > 1) {
                    currentPage--;
                    initPagination();
                }
            });
        }
        
        if (nextPageBtn) {
            nextPageBtn.addEventListener('click', () => {
                const totalPages = Math.ceil(filteredRows.length / rowsPerPage);
                if (currentPage < totalPages) {
                    currentPage++;
                    initPagination();
                }
            });
        }
        
        // 初始化表格
        initPagination();
    }
    
    // 刷新按钮
    const refreshBtn = document.getElementById('refresh-btn');
    if (refreshBtn) {
        refreshBtn.addEventListener('click', () => {
            location.reload();
        });
    }
});

// 初始化图表
function initializeCharts() {
    try {
        // 注册Chart.js插件
        Chart.register(ChartDataLabels);
        
        // 设置全局默认值
        Chart.defaults.font.family = 'Nunito, sans-serif';
        Chart.defaults.color = getComputedStyle(document.documentElement).getPropertyValue('--text-color');
        
        // 每日请求趋势图表
        const dailyChartElement = document.getElementById('dailyChart');
        if (dailyChartElement) {
            const labels = JSON.parse(dailyChartElement.dataset.labels || '[]');
            const values = JSON.parse(dailyChartElement.dataset.values || '[]');
            
            const dailyChart = new Chart(dailyChartElement, {
                type: 'line',
                data: {
                    labels: labels,
                    datasets: [{
                        label: '请求数',
                        data: values,
                        backgroundColor: 'rgba(52, 152, 219, 0.2)',
                        borderColor: 'rgba(52, 152, 219, 1)',
                        borderWidth: 2,
                        pointBackgroundColor: 'rgba(52, 152, 219, 1)',
                        pointRadius: 4,
                        tension: 0.3,
                        fill: true
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            display: false
                        },
                        tooltip: {
                            mode: 'index',
                            intersect: false,
                            backgroundColor: 'rgba(0, 0, 0, 0.7)',
                            titleFont: {
                                size: 14
                            },
                            bodyFont: {
                                size: 13
                            },
                            padding: 10,
                            displayColors: false
                        },
                        datalabels: {
                            display: false
                        }
                    },
                    scales: {
                        x: {
                            grid: {
                                display: false
                            },
                            ticks: {
                                maxRotation: 45,
                                minRotation: 45
                            }
                        },
                        y: {
                            beginAtZero: true,
                            grid: {
                                color: 'rgba(200, 200, 200, 0.1)'
                            },
                            ticks: {
                                precision: 0
                            }
                        }
                    }
                }
            });
            
            chartInstances['dailyChart'] = dailyChart;
        }
        
        // 模型使用分布图表
        const modelChartElement = document.getElementById('modelChart');
        if (modelChartElement) {
            const labels = JSON.parse(modelChartElement.dataset.labels || '[]');
            const values = JSON.parse(modelChartElement.dataset.values || '[]');
            
            const modelChart = new Chart(modelChartElement, {
                type: 'pie',
                data: {
                    labels: labels,
                    datasets: [{
                        label: '模型使用次数',
                        data: values,
                        backgroundColor: [
                            'rgba(255, 99, 132, 0.5)',
                            'rgba(54, 162, 235, 0.5)',
                            'rgba(255, 206, 86, 0.5)',
                            'rgba(75, 192, 192, 0.5)',
                            'rgba(153, 102, 255, 0.5)',
                            'rgba(255, 159, 64, 0.5)',
                            'rgba(199, 199, 199, 0.5)',
                            'rgba(83, 102, 255, 0.5)',
                            'rgba(40, 159, 64, 0.5)',
                            'rgba(210, 199, 199, 0.5)'
                        ],
                        borderColor: [
                            'rgba(255, 99, 132, 1)',
                            'rgba(54, 162, 235, 1)',
                            'rgba(255, 206, 86, 1)',
                            'rgba(75, 192, 192, 1)',
                            'rgba(153, 102, 255, 1)',
                            'rgba(255, 159, 64, 1)',
                            'rgba(199, 199, 199, 1)',
                            'rgba(83, 102, 255, 1)',
                            'rgba(40, 159, 64, 1)',
                            'rgba(210, 199, 199, 1)'
                        ],
                        borderWidth: 1
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        tooltip: {
                            callbacks: {
                                label: function(context) {
                                    let label = context.label || '';
                                    if (label) {
                                        label += ': ';
                                    }
                                    label += formatChartNumber(context.parsed);
                                    return label;
                                }
                            }
                        }
                    }
                }
            });
            
            chartInstances['modelChart'] = modelChart;
        }
    } catch (error) {
        console.error('初始化图表失败:', error);
    }
}

// 设置自动刷新功能
function setupAutoRefresh() {
    // 获取已有的刷新进度条元素
    const progressBar = document.getElementById('refresh-progress-bar');
    const countdownElement = document.getElementById('countdown');
    let countdownTimer;
    
    // 倒计时功能
    let countdown = refreshInterval;
    
    function startCountdown() {
        if (countdownTimer) clearInterval(countdownTimer);
        
        countdown = refreshInterval;
        countdownElement.textContent = countdown;
        
        // 重置进度条
        progressBar.style.width = '100%';
        
        if (autoRefreshEnabled) {
            // 设置进度条动画
            progressBar.style.transition = `width ${refreshInterval}s linear`;
            progressBar.style.width = '0%';
            
            countdownTimer = setInterval(function() {
                countdown--;
                if (countdown <= 0) {
                    countdown = refreshInterval;
                    location.reload();
                }
                countdownElement.textContent = countdown;
            }, 1000);
        } else {
            // 暂停进度条动画
            progressBar.style.transition = 'none';
            progressBar.style.width = '0%';
        }
    }
    
    // 立即启动倒计时
    startCountdown();
}

// 设置主题切换
function setupThemeToggle() {
    // 在简化版中,我们移除了主题切换按钮,但保留功能以备将来使用
    const themeToggleBtn = document.getElementById('theme-toggle-btn');
    if (themeToggleBtn) {
        themeToggleBtn.addEventListener('click', function() {
            document.body.classList.toggle('dark-mode');
            darkModeEnabled = document.body.classList.contains('dark-mode');
            
            localStorage.setItem('theme', darkModeEnabled ? 'dark' : 'light');
            
            // 更新所有图表的颜色
            updateChartsTheme();
        });
    }
}

// 加载保存的主题
function loadSavedTheme() {
    if (darkModeEnabled) {
        document.body.classList.add('dark-mode');
        const themeToggleBtn = document.querySelector('#theme-toggle-btn i');
        if (themeToggleBtn) {
            themeToggleBtn.classList.remove('fa-moon');
            themeToggleBtn.classList.add('fa-sun');
        }
    }
}

// 更新图表主题
function updateChartsTheme() {
    // 更新所有图表的颜色主题
    Object.values(chartInstances).forEach(chart => {
        // 更新网格线颜色
        if (chart.options.scales && chart.options.scales.y) {
            chart.options.scales.y.grid.color = darkModeEnabled ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)';
            chart.options.scales.x.grid.color = darkModeEnabled ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)';
            
            // 更新刻度颜色
            chart.options.scales.y.ticks.color = darkModeEnabled ? '#ddd' : '#666';
            chart.options.scales.x.ticks.color = darkModeEnabled ? '#ddd' : '#666';
        }
        
        // 更新图例颜色
        if (chart.options.plugins && chart.options.plugins.legend) {
            chart.options.plugins.legend.labels.color = darkModeEnabled ? '#ddd' : '#666';
        }
        
        chart.update();
    });
}

// 设置保存统计数据按钮事件
function setupSaveStatsButton() {
    const saveButton = document.querySelector('.save-button');
    if (saveButton) {
        // 添加点击动画效果
        saveButton.addEventListener('click', function() {
            this.classList.add('saving');
            setTimeout(() => {
                this.classList.remove('saving');
            }, 1000);
        });
    }
}

// 添加表格交互功能
function enhanceTableInteraction() {
    // 为请求历史表格添加高亮效果
    const historyRows = document.querySelectorAll('#history-table tbody tr');
    historyRows.forEach(row => {
        row.addEventListener('mouseenter', function() {
            this.classList.add('highlight');
        });
        
        row.addEventListener('mouseleave', function() {
            this.classList.remove('highlight');
        });
    });
}

// 更新页脚信息
function updateFooterInfo() {
    const footer = document.querySelector('.main-footer');
    if (!footer) return;
    
    // 获取当前年份
    const currentYear = new Date().getFullYear();
    
    // 更新版权年份
    const copyrightText = footer.querySelector('p:first-child');
    if (copyrightText) {
        copyrightText.textContent = ${currentYear} 2API 统计面板 | 版本 1.0.1`;
    }
}