window.OrgNodesPage = (function() {
'use strict';
let companyMap = {};
async function render() {
await loadCompanies();
return buildPage();
}
async function loadCompanies() {
try {
const res = await API.get('/companies?page=1&page_size=500');
const items = (res.data && res.data.items) || [];
companyMap = {};
items.forEach(c => { companyMap[c.id] = c.company_name; });
} catch (e) { /* ignore */ }
}
async function buildPage(companyId) {
let html = '
';
if (!companyId) {
html += UI.emptyState('请先选择客户');
bindEvents();
return html;
}
try {
const res = await API.get(`/org-nodes/tree?company_id=${encodeURIComponent(companyId)}`);
const tree = res.data || [];
html += '';
if (tree.length === 0) {
html += UI.emptyState('该客户暂无组织架构');
} else {
html += renderTree(tree);
}
html += '
';
html += '节点列表
';
const allRes = await API.get(`/org-nodes?company_id=${encodeURIComponent(companyId)}`);
const nodes = allRes.data || [];
if (nodes.length > 0) {
html += '| 节点名称 | 类型 | 层级 | 负责人 | 工厂 | 操作 |
';
nodes.forEach(n => {
html += `
| ${esc(n.node_name)} | ${esc(n.node_type)} | ${n.node_level} |
${esc(n.head_name || n.head_contact_id || '-')} |
${n.is_plant ? UI.badge('是', 'success') : '-'} |
|
`;
});
html += '
';
}
} catch (e) {
html += `加载失败: ${esc(e.message)}
`;
}
bindEvents();
return html;
}
function renderTree(nodes) {
let html = '';
return html;
}
function bindEvents() {
setTimeout(() => {
const cf = document.getElementById('org-company-filter');
if (cf) cf.onchange = async () => {
const html = await buildPage(cf.value);
document.getElementById('content-area').innerHTML = html;
};
const addBtn = document.getElementById('btn-add-org');
if (addBtn) addBtn.onclick = () => {
const cf = document.getElementById('org-company-filter');
showEdit(null, cf ? cf.value : '');
};
}, 0);
}
function showEdit(id, companyId) {
const isNew = !id;
let formHtml = '';
formHtml += '';
formHtml += '';
formHtml += '';
formHtml += '';
formHtml += '';
UI.showModal(isNew ? '新建组织节点' : '编辑组织节点', formHtml, async () => {
if (isNew) {
await API.post('/org-nodes', {
company_id: companyId,
node_name: elVal('f-node_name'),
node_type: elVal('f-node_type'),
node_level: parseInt(elVal('f-node_level')) || 1,
node_order: parseInt(elVal('f-node_order')) || 0,
parent_node_id: elVal('f-parent_node_id') || null,
is_plant: elVal('f-is_plant') === '1',
plant_address: elVal('f-plant_address') || null,
head_contact_id: elVal('f-head_contact_id') || null,
head_count: parseInt(elVal('f-head_count')) || null,
remark: elVal('f-remark') || null,
});
UI.toast('组织节点创建成功', 'success');
}
UI.closeModal();
Router.navigate(Router.getCurrentHash());
});
}
async function doDelete(id) {
if (!await UI.confirm('确定删除此组织节点?')) return;
try {
await API.delete(`/org-nodes/${id}`);
UI.toast('组织节点已删除', 'success');
Router.navigate(Router.getCurrentHash());
} catch (e) { UI.toast(e.message, 'error'); }
}
function elVal(id) { const el = document.getElementById(id); return el ? el.value : ''; }
function esc(s) { return (s || '').replace(/&/g,'&').replace(//g,'>').replace(/"/g,'"'); }
function escAttr(s) { return (s || '').replace(/&/g,'&').replace(/"/g,'"'); }
return { render, showEdit, doDelete };
})();