window.FollowsPage = (function() {
'use strict';
let currentPage = 1, pageSize = 20;
let companyMap = {};
let user = null;
async function render() {
user = Auth.getUser();
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, teamType) {
let params = `?page=${currentPage}&page_size=${pageSize}`;
if (companyId) params += `&company_id=${encodeURIComponent(companyId)}`;
if (teamType) params += `&team_type=${encodeURIComponent(teamType)}`;
let html = '
';
try {
const res = await API.get('/follow-records' + params);
const data = res.data || {};
const items = data.items || [];
if (items.length === 0) {
html += UI.emptyState('暂无沟通记录');
} else {
html += '';
html += '| 日期 | 客户 | 联系人 | 团队 | 类型 | 内容 | 结果 | 下一步 | 操作 | ';
html += '
';
items.forEach(f => {
html += `
| ${UI.formatDate(f.record_date)} |
${esc(f.company_name || '')} |
${esc(f.contact_name || f.contact_id || '-')} |
${UI.roleBadge(f.team_type)} |
${esc(f.record_type)} |
${esc(f.content)} |
${esc(f.result || '-')} |
${UI.formatDate(f.next_follow_date)} |
|
`;
});
html += '
';
html += UI.renderPagination(data.total, data.page, data.page_size, 'FollowsPage.goPage');
}
} catch (e) {
html += `加载失败: ${esc(e.message)}
`;
}
bindEvents();
return html;
}
function bindEvents() {
setTimeout(() => {
const cf = document.getElementById('follow-company-filter');
const tf = document.getElementById('follow-team-filter');
const doSearch = () => { currentPage = 1; refresh(); };
if (cf) cf.onchange = doSearch;
if (tf) tf.onchange = doSearch;
const addBtn = document.getElementById('btn-add-follow');
if (addBtn) addBtn.onclick = () => showEdit(null);
}, 0);
}
async function refresh() {
const cf = document.getElementById('follow-company-filter');
const tf = document.getElementById('follow-team-filter');
const html = await buildPage(cf ? cf.value : '', tf ? tf.value : '');
document.getElementById('content-area').innerHTML = html;
}
async function goPage(p) {
currentPage = p;
await refresh();
}
function showEdit(id) {
const isNew = !id;
let formHtml = '';
formHtml += '';
formHtml += '';
formHtml += '';
formHtml += '';
formHtml += '';
formHtml += '';
UI.showModal(isNew ? '新建沟通记录' : '编辑沟通记录', formHtml, async () => {
if (isNew) {
await API.post('/follow-records', {
company_id: elVal('f-company_id'),
contact_id: elVal('f-contact_id') || null,
team_type: elVal('f-team_type'),
record_type: elVal('f-record_type'),
record_date: elVal('f-record_date'),
follow_method: elVal('f-follow_method') || null,
content: elVal('f-content'),
result: elVal('f-result') || null,
customer_feedback: elVal('f-customer_feedback') || null,
next_action: elVal('f-next_action') || null,
next_follow_date: elVal('f-next_follow_date') || null,
});
UI.toast('沟通记录创建成功', 'success');
}
UI.closeModal();
Router.navigate(Router.getCurrentHash());
});
}
async function doDelete(id) {
if (!await UI.confirm('确定删除此沟通记录?')) return;
try {
await API.delete(`/follow-records/${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, goPage };
})();