// 委外在制库存查询
(() => {
let wipStockList = [];
let currentPage = 1;
const pageSize = 20;
Router.register('/outsourcing-mgmt/wip-stock', async () => {
const html = `
`;
setTimeout(() => {
document.getElementById('search-keyword')?.addEventListener('keypress', (e) => {
if (e.key === 'Enter') search();
});
loadList();
}, 0);
return html;
});
async function loadList() {
try {
const res = await API.get('/api/outsourcing-wip-stock');
wipStockList = res.list || [];
updateSummary();
renderList();
} catch (e) {
console.error('加载委外在制库存失败:', e);
document.getElementById('wip-stock-list').innerHTML = '| 加载失败 |
';
}
}
function updateSummary() {
const materialCount = wipStockList.length;
const totalWipQty = wipStockList.reduce((sum, item) => sum + (item.wip_qty || 0), 0);
const materialCountEl = document.getElementById('material-count');
const totalWipQtyEl = document.getElementById('total-wip-qty');
if (materialCountEl) materialCountEl.textContent = materialCount;
if (totalWipQtyEl) totalWipQtyEl.textContent = totalWipQty;
}
function renderList() {
const tbody = document.getElementById('wip-stock-list');
const keyword = (document.getElementById('search-keyword')?.value || '').toLowerCase();
let filtered = wipStockList;
if (keyword) {
filtered = wipStockList.filter(item =>
(item.outsourcing_order_no || '').toLowerCase().includes(keyword) ||
(item.material_code || '').toLowerCase().includes(keyword) ||
(item.material_name || '').toLowerCase().includes(keyword)
);
}
const totalPages = Math.ceil(filtered.length / pageSize);
const start = (currentPage - 1) * pageSize;
const pageData = filtered.slice(start, start + pageSize);
if (pageData.length === 0) {
tbody.innerHTML = '| 暂无在制库存 |
';
} else {
tbody.innerHTML = pageData.map(item => `
| ${escapeHtml(item.outsourcing_order_no || '')} |
${escapeHtml(item.material_code || '')} |
${escapeHtml(item.material_name || '')} |
${item.wip_qty || 0} |
${escapeHtml(item.unit || 'pcs')} |
${formatTime(item.updated_at)} |
`).join('');
}
renderPagination(totalPages);
}
function renderPagination(totalPages) {
const container = document.getElementById('pagination');
if (totalPages <= 1) {
container.innerHTML = '';
return;
}
let html = '';
html += ``;
html += `第 ${currentPage} / ${totalPages} 页`;
html += ``;
container.innerHTML = html;
}
function search() {
currentPage = 1;
renderList();
}
function resetSearch() {
document.getElementById('search-keyword').value = '';
currentPage = 1;
renderList();
}
function goPage(page) {
currentPage = page;
renderList();
}
function formatTime(ts) {
if (!ts) return '-';
try {
const d = new Date(ts);
return d.toLocaleString('zh-CN', { hour12: false });
} catch {
return ts;
}
}
function escapeHtml(str) {
if (!str) return '';
return String(str).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
}
window.OutsourcingWipStock = {
search,
resetSearch,
goPage,
loadList
};
})();