// 加载企业/用户列表(支持分页和搜索)
async function loadEntities(page = 1, search = '') {
const splitType = document.getElementById('split-type').value;
const select = document.getElementById('split-value');
// 显示/隐藏合并特来电选项(仅按企业拆分时显示)
const mergeTelecomGroup = document.getElementById('merge-telecom-group');
if (mergeTelecomGroup) {
mergeTelecomGroup.style.display = splitType === 'company_id' ? 'block' : 'none';
if (splitType !== 'company_id') {
document.getElementById('merge-telecom').checked = false;
toggleTelecomVehicleNo();
}
}
if (!splitType) {
select.innerHTML = '';
return;
}
// 保存当前状态
currentEntityType = splitType === 'company_id' ? 'company' :
splitType === 'user_id' ? 'user' :
splitType === 'station_id' ? 'station' : '';
entityPage = page;
currentSearch = search;
select.innerHTML = '';
try {
const response = await fetch(`/api/entities?type=${currentEntityType}&page=${page}&page_size=${entityPageSize}&search=${encodeURIComponent(search)}`);
const result = await response.json();
if (result.success) {
const entities = result.data;
const total = result.total || 0;
const totalPages = Math.ceil(total / entityPageSize);
if (entities.length === 0) {
select.innerHTML = '';
} else {
select.innerHTML = entities.map(e => {
let value = '';
let name = '';
let typeLabel = '';
if (splitType === 'company_id') {
value = e.company_id;
name = e.company_name;
} else if (splitType === 'user_id') {
value = e.user_id;
name = e.user_name;
typeLabel = e.order_type === 3 ? '(企业用户)' : '(普通用户)';
if (e.phone) {
name = `${e.user_name} (${e.phone})`;
}
} else if (splitType === 'station_id') {
value = e.station_id;
name = e.station_name;
}
return ``;
}).join('');
}
// 更新分页UI
updatePagination(totalPages, total);
}
} catch (error) {
console.error('加载列表失败:', error);
select.innerHTML = '';
}
}
// 更新分页UI
function updatePagination(totalPages, total) {
const paginationDiv = document.getElementById('entity-pagination');
if (totalPages <= 1) {
paginationDiv.innerHTML = `共 ${total} 条`;
return;
}
let html = `共 ${total} 条`;
html += ``;
html += `${entityPage}/${totalPages}`;
html += ``;
paginationDiv.innerHTML = html;
}
// 搜索实体
function searchEntities() {
const search = document.getElementById('entity-search').value.trim();
loadEntities(1, search);
}
// 回车搜索
document.addEventListener('DOMContentLoaded', function() {
const searchInput = document.getElementById('entity-search');
if (searchInput) {
searchInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
searchEntities();
}
});
}
});