/* ---------- 添加关联客户(票04 客户信息区联调:类型筛选/搜索/联系人级联/角色/主要) ---------- */
let _custPicked = null;
async function dlgAddCustomer() {
_custPicked = null;
openDlg('添加关联客户', `
${fld('客户类型(筛选)', `全部客户类型 ${dictOpts('customer_type')} `)}
${fld('关联客户 *', `
搜索
未选择客户
`)}
${fld('客户联系人(选客户后级联)', `—先选客户— `)}
${fld('客户角色 *', `—必选— ${dictOpts('customer_role')} `)}
${fld('是否主要客户 *', '否 是 ')}
类型/角色下拉取自 dict enabled-list(customer_type/customer_role);搜索走 POST /api/opportunity/customer/search;联系人级联走 GET /api/opportunity/customer/contacts?customerId=(A4 前为降级 stub,返回空属正常);提交走 POST /api/opportunity/customer/add
`,
[{ t: '确定关联', cls: 'ok', fn: doAddCustomer }]);
}
async function doCustSearch() {
const box = document.getElementById('custResult');
box.innerHTML = '搜索中…';
const d = await tryApi('customer/search', () => api('POST', '/api/opportunity/customer/search',
{ form: { oppId: S.oppId, keyword: v('custKw') || '', pageNum: 1, pageSize: 10 } }));
const rows = (d && (d.content || d.records)) || [];
box.innerHTML = rows.map(c =>
`· ${esc(c.customerName || c.customerId)} id=${esc(c.customerId)}
`).join('')
|| '无匹配客户
';
}
async function pickCust(cid, cname) {
_custPicked = { customerId: cid, customerName: cname };
document.getElementById('custPicked').innerHTML = `已选:${esc(cname)} (id=${esc(cid)})`;
// 级联刷新客户联系人(切换客户 → 重新拉取 → 清空重选)
const sel = document.getElementById('custContactSel');
sel.innerHTML = '加载中… ';
const contacts = await tryApi('customer/contacts', () => api('GET', '/api/opportunity/customer/contacts', { params: { customerId: cid } }), true) || [];
sel.innerHTML = '(可空) ' + contacts.map(ct =>
`${esc(ct.name)}${ct.company ? '(' + esc(ct.company) + ')' : ''} `).join('')
+ (contacts.length ? '' : '');
if (!contacts.length) sel.insertAdjacentHTML('beforeend', '(A4 前 stub 无联系人,级联已触发) ');
}
async function doAddCustomer() {
const err = document.getElementById('addCustErr');
if (!_custPicked) { err.innerHTML = '请先搜索并选择关联客户
'; return; }
if (!v('customerRole')) { err.innerHTML = '客户角色必选
'; return; }
const form = { oppId: S.oppId, customerId: _custPicked.customerId,
customerNameSnapshot: _custPicked.customerName, customerRole: v('customerRole'),
isPrimaryIntended: v('isPrimaryIntended') };
const ok = await okApi('customer/add', () => api('POST', '/api/opportunity/customer/add', { form }));
if (ok) { toast('关联客户成功', 'ok'); closeDlg(); loadSub('customer'); }
}
/* ---------- 新建商机(flat create;D-14 后六必填=名称/来源/招标形式/省/市/属地) ---------- */