window.DecisionPage = (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(`/decision-nodes?company_id=${encodeURIComponent(companyId)}`); const items = res.data || []; if (items.length === 0) { html += UI.emptyState('该客户暂无决策关系'); } else { html += ''; items.forEach(d => { const types = Array.isArray(d.decision_type) ? d.decision_type.join(', ') : d.decision_type; html += ``; }); html += '
联系人决策层级决策类型影响力关系需审批操作
${esc(d.contact_name)}
${esc(d.contact_title)} / ${esc(d.contact_dept)}
${UI.badge(esc(d.decision_level), 'tech')} ${esc(types)} ${d.influence_weight}/10 ${esc(d.relationship_type || '-')} ${d.approval_required ? UI.badge('是', 'warning') : '-'}
'; } } catch (e) { html += `
加载失败: ${esc(e.message)}
`; } bindEvents(); return html; } function bindEvents() { setTimeout(() => { const cf = document.getElementById('decision-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-decision'); if (addBtn) addBtn.onclick = () => { const cf = document.getElementById('decision-company-filter'); showEdit(null, cf ? cf.value : ''); }; }, 0); } function showEdit(id, companyId) { const isNew = !id; let formHtml = '
'; formHtml += '
'; formHtml += '
'; formHtml += '
'; formHtml += '
'; UI.showModal(isNew ? '新建决策关系' : '编辑决策关系', formHtml, async () => { if (isNew) { await API.post('/decision-nodes', { company_id: companyId, contact_id: elVal('f-contact_id'), decision_level: elVal('f-decision_level'), influence_weight: parseInt(elVal('f-influence_weight')) || 5, reports_to: elVal('f-reports_to') || null, relationship_type: elVal('f-relationship_type') || null, approval_required: elVal('f-approval_required') === '1', 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(`/decision-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 }; })();