import type { EvidenceRow } from "../types";
import type { Scenario } from "../mock/scenarios";
import { pct, wan, yuan } from "./format";
/** 流程收尾自动出具的《需求评估报告》:自包含 HTML 网页,新标签页打开 */
export type ReviewOutcome = "order" | "credit";
const pad = (n: number) => `${n}`.padStart(2, "0");
export function reportFileName(s: Scenario, when: Date = new Date()): string {
const p = s.demand.parsed;
const stamp = `${when.getFullYear()}${pad(when.getMonth() + 1)}${pad(when.getDate())}-${pad(when.getHours())}${pad(when.getMinutes())}`;
return `需求评估报告-${p.productType}-${p.quantity}个-${stamp}.html`;
}
function esc(t: string): string {
return t.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """);
}
function evidenceRows(rows: EvidenceRow[]): string {
return rows
.map(
(r) =>
`
| ${esc(r.label)} | ${esc(r.value)} | ${r.verdict ? esc(r.verdict) : ""} |
`,
)
.join("");
}
const CSS = `
* { box-sizing: border-box; }
body { margin: 0; background: #f4f6fb; color: #1e293b; font: 14px/1.65 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif; }
.wrap { max-width: 980px; margin: 0 auto; padding: 32px 20px 64px; }
.hero { background: linear-gradient(135deg, #0b2a6b 0%, #2563eb 62%, #3b82f6 100%); color: #fff; border-radius: 16px; padding: 28px 30px; box-shadow: 0 12px 32px rgba(37, 99, 235, .25); }
.hero .eyebrow { letter-spacing: .18em; font-size: 11px; opacity: .82; }
.hero h1 { margin: 6px 0 10px; font-size: 26px; letter-spacing: .02em; }
.hero .meta { display: flex; flex-wrap: wrap; gap: 8px 18px; font-size: 12px; opacity: .92; }
.banner { margin: 18px 0 0; border-radius: 12px; padding: 14px 18px; font-size: 15px; font-weight: 700; }
.banner.ok { background: #f0fdf4; color: #15803d; border: 1px solid #bbf7d0; }
.banner.fail { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }
.card { background: #fff; border: 1px solid #e2e8f0; border-radius: 14px; padding: 18px 20px; margin-top: 16px; box-shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 8px 24px rgba(15, 23, 42, .06); }
.card h2 { margin: 0 0 10px; font-size: 15px; color: #0b2a6b; border-left: 4px solid #2563eb; padding-left: 10px; }
.lead { margin: 4px 0 10px; font-size: 13px; color: #475569; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px 18px; margin: 0; font-size: 13px; }
.grid dt { color: #64748b; font-size: 12px; }
.grid dd { margin: 2px 0 0; font-weight: 600; font-variant-numeric: tabular-nums; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
td, th { padding: 8px 10px; border-bottom: 1px solid #eef2f7; text-align: left; vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.k { color: #64748b; width: 34%; }
td.v { width: 64px; text-align: right; font-weight: 700; white-space: nowrap; }
td.v.pass { color: #15803d; }
td.v.fail { color: #b91c1c; }
.note { margin: 10px 0 0; padding: 10px 12px; background: #eff6ff; border-radius: 10px; color: #1d4ed8; font-size: 12.5px; }
.score { display: flex; align-items: baseline; gap: 10px; margin: 4px 0 8px; }
.score b { font-size: 30px; color: #0b2a6b; font-variant-numeric: tabular-nums; }
.score span { color: #64748b; font-size: 12px; }
.bar { height: 10px; border-radius: 999px; background: #e2e8f0; overflow: hidden; position: relative; }
.bar i { display: block; height: 100%; border-radius: 999px; background: linear-gradient(90deg, #2563eb, #60a5fa); }
.bar u { position: absolute; left: 90%; top: -3px; width: 2px; height: 16px; background: #b91c1c; }
.tags { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.tag { font-size: 12px; padding: 4px 10px; border-radius: 999px; background: #f1f5f9; color: #475569; }
.tag.ok { background: #f0fdf4; color: #15803d; }
.tag.fail { background: #fef2f2; color: #b91c1c; }
ul.sug { margin: 8px 0 0; padding-left: 20px; font-size: 13px; color: #475569; }
footer { margin-top: 22px; color: #94a3b8; font-size: 12px; text-align: center; }
b.ok { color: #15803d; } b.fail { color: #b91c1c; }
@media print { body { background: #fff; } .card, .hero { box-shadow: none; } }
`;
export function buildReportHtml(s: Scenario, outcome: ReviewOutcome): string {
const d = s.demand;
const p = d.parsed;
const order = s.summary.order;
const now = new Date().toLocaleString("zh-CN", { hour12: false });
const rndSection =
s.feasibility.route === "rnd"
? `新品研发评审
结论:${s.rnd.pass ? "通过" : "不通过"}(4 项评审全过才通过)
${s.rnd.items
.map(
(i) =>
`| ${esc(i.label)} | 标准 ${esc(i.standard)} | 实际 ${esc(i.actual ?? "—")} | ${i.pass ? "通过" : "不通过"} |
`,
)
.join("")}
毛利复算:收入 ${yuan(s.margin.revenue)} − 钢卷 ${yuan(s.margin.coilCostTotal)} = ${yuan(s.margin.grossProfit)} 元(毛利率 ${pct(s.margin.grossMarginRate)})
`
: "";
const ratingTags = s.rating.items
.map((i) => `${esc(i.label)} ${i.score}/${i.weight}`)
.join("");
const html = `
需求评估报告 · ${esc(p.productType)} ${p.quantity} 个
整体结论:${esc(s.summary.overall)}${order ? ` · 订单 ${esc(order.orderNo)}` : ""}
需求概要
- 客户
- ${esc(d.customerCode)}
- 行业
- ${esc(p.industry ?? "—")}
- 产品类型
- ${esc(p.productType)}
- 数量 / 单价
- ${p.quantity} 个 / ${yuan(p.unitPrice)} 元
- 当期需求金额
- ${wan(p.quantity * p.unitPrice)} 万
- 交期
- ${esc(p.deliveryDate)}
- 钢卷材质
- ${esc(p.coilMaterial)}
可行性判定
结论:${esc(s.feasibility.category)}(${esc(s.feasibility.routeLabel)})
${evidenceRows(s.feasibility.evidence)}
${rndSection}
交期评审
结论:${s.delivery.pass ? "通过" : "不通过"} · 距交期 ${s.delivery.daysToDelivery} 天
${evidenceRows(s.delivery.evidence)}
${s.delivery.suggestions.length ? `${s.delivery.suggestions.map((t) => `- ${esc(t)}
`).join("")}
` : ""}
客户评级
${s.rating.total}/ 100 分 · ≥90 合格 · 红线为合格线
${ratingTags}
${s.rating.items
.map((i) => `| ${esc(i.label)}(权重 ${i.weight}) | ${esc(i.basis)} | ${i.score} 分 |
`)
.join("")}
综合结论与处理
整体结论:${esc(s.summary.overall)}
${order ? `订单:${esc(order.orderNo)}|金额 ${yuan(order.amount)} 元|交期 ${esc(order.deliveryDate)}
` : ""}
${s.summary.suggestions.length ? `${s.summary.suggestions.map((t) => `- ${esc(t)}
`).join("")}
` : ""}
${outcome === "order" ? "处理结果:用户已确认评审结论并发起订单生成。" : "处理结果:用户已发起客户临时授信流程(授信审批 → 评级复跑 → 重新汇总)。"}
`;
return html;
}