let currentSort = 'total_income';
function switchSort(el) {
document.querySelectorAll('.sort-btn').forEach(b => b.classList.remove('active'));
el.classList.add('active');
currentSort = el.dataset.sort;
}
async function loadRankingDetail() {
const startDate = document.getElementById('rankStartDate').value;
const endDate = document.getElementById('rankEndDate').value;
let url = API + '/api/stats/boss-ranking?sortBy=' + currentSort;
if (startDate) url += '&startDate=' + startDate;
if (endDate) {
const d = new Date(endDate);
d.setDate(d.getDate() + 1);
url += '&endDate=' + formatLocalDate(d);
}
try {
const res = await apiFetch(url);
if (!res.ok) return;
const rows = await res.json();
const container = document.getElementById('rankingDetailList');
if (rows.length === 0) {
container.innerHTML = '
暂无数据
';
return;
}
if (currentSort === 'dispatch_count') {
const max = Math.max(...rows.map(r => r.dispatch_count), 1);
container.innerHTML = rows.map((r, i) => {
const pct = Math.max((r.dispatch_count / max) * 100, 2);
const boss = escapeHtml(r.boss);
return `
${i + 1}
${boss}
${r.dispatch_count}单
`;
}).join('');
} else if (currentSort === 'take_count') {
const max = Math.max(...rows.map(r => r.take_count), 1);
container.innerHTML = rows.map((r, i) => {
const pct = Math.max((r.take_count / max) * 100, 2);
const boss = escapeHtml(r.boss);
return `
${i + 1}
${boss}
${r.take_count}单
`;
}).join('');
} else {
const max = Math.max(...rows.map(r => r.total_income), 1);
container.innerHTML = rows.map((r, i) => {
const pct = Math.max((r.total_income / max) * 100, 2);
const boss = escapeHtml(r.boss);
return `
${i + 1}
${boss}
${r.total_income.toFixed(2)}
`;
}).join('');
}
} catch (e) { console.error(e); }
}