/**
* company.js - 企业数据管理逻辑(列表+筛选+分页 / 新增编辑弹窗 / 导入导出 / 详情)
*/
$(function () {
renderShell('企业数据', '数据管理 / 企业数据');
renderPage();
initPage('company', function (user) {
if (!checkPagePermission('company')) return;
loadList(1);
});
var currentPage = 1;
/** 渲染页面内容(工具栏+表格+分页) */
function renderPage() {
$('#page-content').html(
'
'
);
$('#check-all').on('change', function () { $('.row-check').prop('checked', this.checked); });
}
/* ---------- 列表 ---------- */
function loadList(page) {
currentPage = page;
var params = buildFilterParam();
params.page = page;
params.limit = PAGE_SIZE;
httpGet('company/list.php', params).then(function (data) {
var rows = data.list || [];
var html = '';
rows.forEach(function (r) {
html += '' +
' | ' +
'' + r.id + ' | ' +
'' + escHtml(r.name_zh || r.name_en || '-') + ' | ' +
'' + escHtml(r.industry || '-') + ' | ' +
'' + escHtml(r.industry_subdivision || '-') + ' | ' +
'' + escHtml(r.country || '-') + ' | ' +
'' + escHtml(r.business_role || '-') + ' | ' +
'' + escHtml(r.registration_number || '-') + ' | ' +
'' + escHtml(r.legal_representative || '-') + ' | ' +
'' + (r.is_listed ? '上市' : '-') + ' | ' +
'' + fmtDate(r.created_at) + ' | ' +
'' +
' 详情' +
' 员工' +
' 编辑' +
' 删除' +
' |
';
});
if (!rows.length) {
html = '| 暂无数据 |
';
}
$('#company-tbody').html(html);
renderPagination($('#pagination'), data, loadList);
});
}
/* ---------- 筛选 ---------- */
$('#btn-search').on('click', function () { loadList(1); });
$('#btn-reset').on('click', function () {
$('.toolbar [data-filter]').val('');
loadList(1);
});
/* ---------- 新增/编辑 ---------- */
$('#btn-add').on('click', function () { openForm(null); });
window.editCompany = function (id) {
httpGet('company/detail.php', { id: id }).then(function (d) {
openForm(d.company);
});
};
function openForm(company) {
var isEdit = !!company;
var c = company || {};
var content =
'';
var idx = Dialog.open({
title: isEdit ? '编辑企业' : '新增企业',
area: ['760px', 'auto'],
content: content,
btn: false
});
$('#form-cancel').on('click', function () { Dialog.close(idx); });
$('#company-form').on('submit', function (e) {
e.preventDefault();
var fd = new FormData(this);
var nameZh = fd.get('name_zh');
var nameEn = fd.get('name_en');
if (isEdit) fd.delete('display_name'); // 编辑不允许改显示名称
if (!isEdit && !nameZh && !nameEn) {
Dialog.error('公司名称或英文名称至少填写一项');
return;
}
httpPost(isEdit ? 'company/update.php' : 'company/add.php', fd, true).then(function () {
Dialog.success('保存成功', function () {
Dialog.close(idx);
loadList(currentPage);
});
});
});
}
function roleOptions(sel) {
var opts = ['', 'manufacturer', 'integrator', 'distributor', 'brand_operator', 'others'];
var labels = { 'manufacturer': '生产型', 'integrator': '集成商', 'distributor': '代理商', 'brand_operator': '品牌运营', 'others': '其他' };
var html = '';
opts.forEach(function (v) {
if (v === '') return;
html += '';
});
return html;
}
/* ---------- 删除(软删除) ---------- */
window.delCompany = function (id) {
Dialog.confirm('确定删除该企业吗?(软删除,可恢复)', function () {
httpPost('company/delete.php', { id: id }).then(function () {
Dialog.success('删除成功', function () { loadList(currentPage); });
});
});
};
/* ---------- 批量删除 ---------- */
$('#btn-del-batch').on('click', function () {
var ids = [];
$('.row-check:checked').each(function () { ids.push($(this).val()); });
if (!ids.length) { Dialog.error('请先勾选记录'); return; }
Dialog.confirm('确定删除选中的 ' + ids.length + ' 条记录吗?', function () {
httpPost('company/delete.php', { ids: ids.join(',') }).then(function () {
Dialog.success('删除成功', function () { loadList(currentPage); });
});
});
});
/* ---------- 导入 ---------- */
$('#btn-import').on('click', function () {
triggerFileInput('.csv', function (file) {
var fd = new FormData();
fd.append('file', file);
httpPost('company/import.php', fd, true).then(function (d) {
Dialog.alert('导入结果:成功 ' + d.inserted + ' 条,失败 ' + d.failed + ' 条', function () {
loadList(1);
});
});
});
});
/* ---------- 导出(仅导出勾选的企业,未勾选弹窗提醒) ---------- */
$('#btn-export').on('click', function () {
var ids = [];
$('.row-check:checked').each(function () { ids.push($(this).val()); });
if (!ids.length) {
layer.alert('请先选择需要导出的企业数据', { icon: 2, title: '提示' });
return;
}
download('company/export.php', { ids: ids.join(',') });
});
/* ---------- 详情 ---------- */
window.viewCompany = function (id) {
httpGet('company/detail.php', { id: id }).then(function (d) {
var c = d.company;
var html =
'' +
'
' + escHtml(c.name_zh || c.name_en || '企业 #' + c.id) + '
' +
'
' +
detailRow('公司名称', c.name_zh) + detailRow('英文名称', c.name_en) +
detailRow('业务角色', c.business_role) + detailRow('国家/地区', c.country) +
detailRow('注册号', c.registration_number) + detailRow('地址', c.address) +
detailRow('法定代表人', c.legal_representative) + detailRow('行业', c.industry) +
detailRow('行业细分', c.industry_subdivision) + detailRow('官网', c.website) +
detailRow('注册资本', c.registered_capital) + detailRow('成立日期', c.established_date) +
detailRow('员工数', c.latest_employee_count) + detailRow('年营收', c.latest_annual_revenue) +
detailRow('是否上市', c.is_listed ? '是' : '否') + detailRow('股票代码', c.stock_code) +
'
' +
'
经营范围
' + escHtml(c.business_scope || '-') + '
' +
'
产品品类
' + miniTable(['品类', '描述', '核心'], d.products.map(function (p) { return [p.category_name, p.category_description || '-', p.is_core ? '是' : '否']; })) +
'
关联需求
' + miniTable(['联系人', '需求大类', '意向品类', '场景'], d.needs.map(function (n) { return [n.contact_person || '-', n.need_category || '-', n.target_product_category || '-', n.application_scenario || '-']; })) +
'
关联联系人
' + miniTable(['姓名', '职位', '联系方式'], d.contacts.map(function (p) { return [p.full_name || '-', p.position || '-', p.contacts || '-']; })) +
'
';
Dialog.open({ title: '企业详情', area: ['820px', 'auto'], content: html, btn: ['关闭'] });
});
};
function detailRow(label, val) {
return '' + label + ':' + escHtml(val || '-') + '
';
}
function miniTable(headers, rows) {
if (!rows.length) return '暂无数据
';
var html = '';
html += '' + headers.map(function (h) { return '| ' + h + ' | '; }).join('') + '
';
rows.forEach(function (r) {
html += '' + r.map(function (v) { return '| ' + escHtml(v) + ' | '; }).join('') + '
';
});
return html + '
';
}
/* ---------- 员工列表弹窗(按公司查看员工,分页 + 姓名/职级筛选) ---------- */
var empCompanyId = 0;
var empPage = 1;
window.viewEmployees = function (companyId) {
empCompanyId = companyId;
empPage = 1;
var content =
'' +
'
' +
' ' +
' ' +
' ' +
' ' +
'
' +
'
' +
' | 姓名 | 联系方式 | 邮箱 | 职级 | 部门 | 岗位 | 入职日期 | 是否在职 |
' +
' ' +
'
' +
' ' +
'
';
var idx = Dialog.open({ title: '员工列表', area: ['920px', 'auto'], content: content, btn: ['关闭'] });
window.__empDialogIdx = idx;
loadEmployees(1);
$('#emp-search').on('click', function () { loadEmployees(1); });
$('#emp-reset').on('click', function () {
$('#emp-keyword').val('');
$('#emp-level').val('');
loadEmployees(1);
});
};
function loadEmployees(page) {
empPage = page;
var params = { company_id: empCompanyId, page: page, limit: 5 };
var kw = $.trim($('#emp-keyword').val());
var lv = $('#emp-level').val();
if (kw) params.keyword = kw;
if (lv) params.job_level = lv;
httpGet('company/employees.php', params).then(function (data) {
var rows = data.list || [];
var html = '';
rows.forEach(function (r) {
html += '' +
'| ' + escHtml(r.full_name) + ' | ' +
'' + escHtml(r.phone || '-') + ' | ' +
'' + escHtml(r.email || '-') + ' | ' +
'' + escHtml(r.job_level || '-') + ' | ' +
'' + escHtml(r.department || '-') + ' | ' +
'' + escHtml(r.position || '-') + ' | ' +
'' + escHtml(r.start_date || '-') + ' | ' +
'' + (r.is_current ? '在职' : '离职') + ' | ' +
'
';
});
if (!rows.length) {
html = '| 暂无员工 |
';
}
$('#emp-tbody').html(html);
// 职级下拉(仅首次填充)
var $lv = $('#emp-level');
if ($lv.find('option').length <= 1) {
var optHtml = '';
(data.levels || []).forEach(function (l) {
optHtml += '';
});
$lv.html(optHtml);
}
renderPagination($('#emp-pagination'), data, loadEmployees);
});
}
});