// Dashboard - 统计概览 import { api } from '../api.js'; export async function renderDashboard(container) { container.innerHTML = '

加载中...

'; try { const [overviewRes, dailyRes, storeRes, employeeKpiRes] = await Promise.all([ api.getOverview(), api.getDailyStats(), api.getStoreStats(), api.getEmployeeKpi(), ]); const overview = overviewRes.data || {}; const daily = dailyRes.data || []; const storeStats = storeRes.data || []; const employeeKpi = employeeKpiRes.data || []; container.innerHTML = `
📋
总反馈数
${overview.total_records || 0}
😊
满意率
${overview.satisfaction_rate || 0}%
满意 ${overview.satisfied || 0} + 非常满意 ${overview.very_satisfied || 0}
😕
不满意
${overview.unsatisfied || 0}
😡
很差
${overview.very_bad || 0}
满意度分布
每日反馈趋势
门店统计
${storeStats.length === 0 ? '' : storeStats.map(s => { const storeName = s.storeName || s.store_name || '-'; const storeCode = s.storeCode || s.store_code || '-'; const totalRecords = s.totalRecords ?? s.total_records ?? 0; const satisfactionRate = s.satisfactionRate ?? s.satisfaction_rate ?? 0; const satisfactionDistribution = s.satisfactionDistribution || s.satisfaction_distribution || {}; return ` `; }).join('')}
门店 编码 反馈数 满意率 非常满意 满意 不满意 很差
暂无数据
${escapeHtml(storeName)} ${storeCode} ${totalRecords} ${satisfactionRate}% ${satisfactionDistribution.very_satisfied || 0} ${satisfactionDistribution.satisfied || 0} ${satisfactionDistribution.unsatisfied || 0} ${satisfactionDistribution.very_bad || 0}
员工KPI(好评 / 中评 / 差评)
${employeeKpi.length === 0 ? '' : employeeKpi.map(e => ` `).join('')}
员工工号总反馈好评中评差评好评率操作
暂无员工评价数据
${escapeHtml(e.employeeName || '-')} ${escapeHtml(e.employeeNumber || '-')} ${e.total || 0} ${e.good || 0} ${e.middle || 0} ${e.bad || 0} ${e.goodRate || 0}%
`; document.querySelectorAll('.employee-records-btn').forEach(btn => { btn.addEventListener('click', () => showEmployeeRecords(btn.dataset.id, btn.dataset.name)); }); // Render simple charts (no ECharts dependency - pure canvas) renderSatChart(overview); renderTrendChart(daily); } catch (e) { container.innerHTML = `
⚠️

加载失败: ${e.message}

`; } } function showEmployeeRecords(employeeId, employeeName) { if (!employeeId) return; const overlay = document.createElement('div'); overlay.className = 'modal-overlay'; overlay.innerHTML = ` `; document.body.appendChild(overlay); const close = () => document.body.removeChild(overlay); document.getElementById('empRecClose').onclick = close; document.getElementById('empRecCloseBtn').onclick = close; document.getElementById('empRecSearch').onclick = () => loadEmployeeRecords(employeeId); loadEmployeeRecords(employeeId); } async function loadEmployeeRecords(employeeId) { const body = document.getElementById('empRecBody'); if (!body) return; body.innerHTML = '
'; const params = new URLSearchParams({ employee_id: employeeId, page: 1, page_size: 50 }); const group = document.getElementById('empRecGroup')?.value; if (group) params.set('satisfaction_group', group); try { const res = await api.getEmployeeRecords(params.toString()); const list = res.data?.list || []; body.innerHTML = list.length ? `
${list.map(r => ``).join('')}
满意度点评/反馈时间
${satisfactionBadge(r.satisfaction)}
${escapeHtml(r.reviewLong || r.reviewShort || truncate(r.cleanedText || r.rawAsrText || '-', 160))}
反馈ID:${escapeHtml(r.id)}
${formatDate(r.createDate)}
` : '
暂无评价明细
'; } catch (e) { body.innerHTML = `
加载失败:${escapeHtml(e.message)}
`; } } function renderSatChart(overview) { const el = document.getElementById('satChart'); if (!el) return; const dist = overview.satisfactionDistribution || overview.satisfaction_distribution || {}; const data = [ { label: '非常满意', value: dist.very_satisfied || 0, color: '#059669', bg: '#ECFDF5' }, { label: '满意', value: dist.satisfied || 0, color: '#10B981', bg: '#F0FDF4' }, { label: '不满意', value: dist.unsatisfied || 0, color: '#F59E0B', bg: '#FFFBEB' }, { label: '很差', value: dist.very_bad || 0, color: '#EF4444', bg: '#FEF2F2' }, ]; const total = data.reduce((s, d) => s + d.value, 0); if (total === 0) { el.innerHTML = '

暂无满意度数据

'; return; } el.innerHTML = `
${total}
总反馈
满意率 ${overview.satisfaction_rate || 0}%
${data.map(d => { const percent = total > 0 ? Math.round(d.value / total * 100) : 0; return `
${d.label} ${d.value} · ${percent}%
`; }).join('')}
`; } function renderTrendChart(daily) { const el = document.getElementById('trendChart'); if (!el) return; if (!daily || daily.length === 0) { el.innerHTML = '

暂无趋势数据

'; return; } const recent = daily.slice(-14); const maxVal = Math.max(...recent.map(d => d.count || 0), 1); el.innerHTML = `
${recent.map(d => { const value = d.count || 0; const height = Math.max(8, Math.round(value / maxVal * 150)); const dateStr = d.date ? d.date.substring(5) : ''; return `
${value}
${dateStr}
`; }).join('')}
`; }