// 弹窗系统(对应原 modal/guide/openHistoricalCase/openQuotationLibrary/知识图谱/表单弹窗)
import { useEffect, useMemo, useRef, useState } from 'react'
import { money, esc } from './logic'
import { HISTORY_CASES, kbRecords } from './data'
import type { Material, Resource, State } from './types'
import { useApp } from './store'
import { Btn, Notice, Table } from './components'
// ============ 知识图谱 ============
function materialNetworkData(s: State, type: 'material' | 'resource') {
const list: any[] = type === 'resource' ? (s.resources as any[]) : (s.materials as any[])
const sources: { key: string; known: boolean; x: number; y: number }[] = []
const nodes = list.map((m, i) => {
const record: any = (kbRecords as any)[type].find((r: any) => r.id === m.id && r.name === m.name && r.ref === m.ref)
const key = type === 'resource'
? (record ? (record.ref.includes('内部工艺') ? '内部工艺定额' : record.name.includes('焊接') ? '焊接工时依据' : '机加工工艺依据') : '上传 / 人工数据')
: record
? (record.ref.includes('SIM-IS') ? 'SIM-IS-01' : record.ref.includes('SIM-OS') ? 'SIM-OS-01' : record.ref.includes('§4') ? '需求书 §4' : '需求书 §3')
: '上传 / 人工数据'
let source = sources.findIndex((x) => x.key === key)
if (source < 0) { source = sources.length; sources.push({ key, known: !!record, x: 0, y: 0 }) }
const angle = -Math.PI / 2 + (i * Math.PI * 2) / Math.max(1, list.length)
return { m, record, source, x: 480 + 338 * Math.cos(angle), y: 248 + 174 * Math.sin(angle), i }
})
sources.forEach((src, i) => {
const a = -Math.PI / 2 + (i * Math.PI * 2) / sources.length
src.x = 480 + 147 * Math.cos(a)
src.y = 248 + 88 * Math.sin(a)
})
return { sources, nodes }
}
function NetworkSvg() {
const { s, flows, selectMaterialNetwork } = useApp()
const a = flows.materialNetwork
const type = a.type || 'material'
const { sources, nodes } = useMemo(() => materialNetworkData(s, type), [s, type])
const shown = a.done ? nodes.length : Math.max(0, a.stage - 1)
const visible = nodes.slice(0, shown)
const resource = type === 'resource'
return (
)
}
function MaterialNetworkModal() {
const { s, flows, replayKnowledgeNetwork } = useApp()
const a = flows.materialNetwork
const type = a.type || 'material'
const { sources, nodes } = useMemo(() => materialNetworkData(s, type), [s, type])
const resource = type === 'resource'
const n = nodes[a.selected]
const status = a.done
? `关联构建完成:${sources.length} 个${resource ? '工艺依据' : '来源'}节点 · ${nodes.length} ${resource ? '道工序' : '项物料'} · ${sources.length + nodes.length} 条关联。点击${resource ? '工序' : '物料'}节点核对详情。`
: a.stage === 0
? (resource ? '索引检索 Agent:读取当前项目与工艺资源索引…' : '索引检索 Agent:读取当前项目与物料索引…')
: a.stage === 1
? (resource ? '关系映射 Agent:建立工艺定额与工时依据节点…' : '关系映射 Agent:建立图纸与需求条款来源节点…')
: shown2() < nodes.length
? `关系映射 Agent:逐项关联${resource ? '工序' : '物料'} ${shown2()} / ${nodes.length}…`
: (resource ? '来源校核 Agent:核对工时、综合费率与计价口径…' : '来源校核 Agent:比对记录与当前用量,整理追溯信息…')
function shown2() { return a.done ? nodes.length : Math.max(0, a.stage - 1) }
return (
✦ AI {resource ? '工艺资源' : '数据'}关联网络
{resource ? '项目 → 工艺依据 / 工时记录 → 工序资源 → 来源核对' : '项目 → 图纸 / 需求条款 → 物料 → 来源核对'}
01 索引检索 Agent02 关系映射 Agent03 来源校核 Agent
{status}
{resource
? '离线关联演示:依据当前工序、内置工艺记录与工时来源字段构建,未连接企业实际工艺库;未匹配记录显示为上传 / 人工数据。'
: '离线关联演示:图谱依据当前物料及内置来源字段构建;未匹配记录显示为上传 / 人工数据。'}
{n ? (
resource ?
:
) : (
)}
)
}
function MaterialNetworkDetail({ m, record }: { m: Material; record?: any }) {
const { s, flows, selectMaterialNetwork } = useApp()
const ref = m.ref || '待补充'
const doc = record ? (ref.includes('SIM-IS') ? '/mod-assets/wuc-quotation/模拟内套图.svg' : ref.includes('SIM-OS') ? '/mod-assets/wuc-quotation/模拟外套图.svg' : '/mod-assets/wuc-quotation/客户需求书.md') : null
return (
<>
{s.materials.map((r, i) => (
selectMaterialNetwork(i)} cls={flows.materialNetwork.selected === i ? 'primary' : ''} />
))}
{record ? '本地示例记录匹配' : '未匹配内置记录 / 人工数据'}
{m.id} · {m.name}
来源关联:{ref}
{doc
?
打开来源文件 ↗
:
}
>
)
}
function ResourceNetworkDetail({ r, record }: { r: Resource; record?: any }) {
const { s, flows, selectMaterialNetwork } = useApp()
const hasRate = r.rate != null && Number.isFinite(r.rate) && r.rate > 0
return (
<>
{s.resources.map((item, i) => (
selectMaterialNetwork(i)} cls={flows.materialNetwork.selected === i ? 'primary' : ''} />
))}
{record ? '本地工艺示例记录匹配' : '未匹配内置记录 / 人工数据'}
{r.id} · {r.name}
工时来源:{r.ref || '待补充'}
>
)
}
// ============ 历史案例 ============
function HistoricalCaseModal({ id }: { id: string }) {
const { s, openModal, closeModal } = useApp()
const c = HISTORY_CASES[id]
if (!c) return null
const total = c.costs.reduce((sum, x) => sum + x[1], 0)
const profit = c.price - total
const manufacturing = c.process.reduce((sum, x) => sum + x[1] * x[2], 0)
void manufacturing
return (
项目概况
供货范围与技术要求
{c.scope}
{c.quality}
物料与依据
制造工艺与资源
[r[0], r[1], money(r[2]), money(r[1] * r[2])])} />
成本与成交报价
[r[0], money(r[1]) + ' 元']),
['项目全成本', money(total) + ' 元'],
['不含税成交总价', money(c.price) + ' 元'],
['不含税单位报价', money(c.price / c.sets) + ' 元/套'],
['项目利润', money(profit) + ' 元'],
['实际项目利润率', ((profit / c.price) * 100).toFixed(2) + '%'],
['模拟税额(13%)', money(c.price * 0.13) + ' 元'],
['模拟含税总额', money(c.price * 1.13) + ' 元'],
]} />
交付计划与商务条件
{c.terms}
报价参考与差异
{c.reason}
{c.risk}
a + m.qty * m.price, 0) + s.resources.reduce((a, r) => a + r.qty * (r.rate || 0), 0)) + ' 元(当前清单,未含专项与间接费)'],
['利润率参考', c.margin + '%(模拟成交)', s.floor + '%(硬性底线)'],
]} />
openModal('case', '历史案例 ' + (id === 'H01' ? 'H02' : 'H01') + ' / 完整项目参考', id === 'H01' ? 'H02' : 'H01')} />
)
}
// ============ 历史报价信息库 / 报价智能体 ============
function QuoteAgentProgress() {
const { flows } = useApp()
const a = flows.quoteAssistant
if (!a.mode) return null
const steps = a.mode === 'analysis'
? ['读取归档字段', '核算成本结构', '核对风险依据', '整理分析结论']
: ['理解查询条件', '检索本地记录', '核对项目字段', '逐项展示结果']
return (
{a.running ? '● Agent 协同处理中' : a.cancelled ? '处理已停止' : a.mode === 'analysis' ? '分析已完成' : '检索已完成'}
{steps.map((t, i) => (
{i < a.stage ? '✓' : '0' + (i + 1)}{t}
))}
展示检索与计算步骤,不代表外部模型实时调用。
)
}
function QuoteAgentAnalysis() {
const { flows, library } = useApp()
const a = flows.quoteAssistant
void library
if (!a.analysis.length) {
return (
◈
选择一个项目,开启证据驱动的归因分析逐项呈现成本结构、风险线索和待核验依据。
)
}
const shown = a.analysis.slice(0, a.analysisCount)
const kindLabel: Record = { fact: '可核算事实', attention: '需复核', hypothesis: '风险线索 / 非因果结论', action: '核验建议' }
return (
PROJECT DIAGNOSTICS
{a.analysisTitle}
{a.running && a.mode === 'analysis' ? '正在构建分析' : '分析记录'}
{!shown.length
? 正在读取项目档案与成本字段…
: shown.map((x, i) => (
{kindLabel[x.kind]}
{x.title}
{x.text}
{x.bars && (
{x.bars.map((b, j) => (
{b.name}
{money(b.value)}
))}
)}
依据 {x.evidence}
))}
)
}
function QuotationLibraryModal() {
const {
library, filters, setFilters, flows, useQuoteAgentPrompt, runQuoteAgentQuery,
analyzeSelectedQuotation, selectAgentQuotation, showAllAgentQuotes, openModal,
} = useApp()
const a = flows.quoteAssistant
const [input, setInput] = useState('')
const messagesRef = useRef(null)
const messages = a.messages.length ? a.messages : [{ role: 'assistant' as const, text: '你好,我可以帮你查找历史报价,并对选中项目核算成本结构、梳理风险依据。可按项目、客户、编号、年份、金额和交期查找。' }]
useEffect(() => {
const el = messagesRef.current
if (el) el.scrollTop = el.scrollHeight
}, [a.messages.length, a.stage, a.analysisCount])
let rows = filters.from && filters.to && filters.from > filters.to ? [] : libraryAll()
function libraryAll() {
const saved = library.map((r) => ({ ...r, kind: 'saved' }))
const examples = Object.entries(HISTORY_CASES).map(([id, c]) => ({
id, number: c.code, project: c.name, customer: c.customer,
date: c.date.startsWith('2025-09') ? '2025-09-01' : '2025-12-01',
sets: c.sets, days: c.days, price: c.price, total: c.price * 1.13, kind: 'example',
}))
return [...saved, ...examples]
}
if (a.ids !== null) rows = rows.filter((r: any) => a.ids!.includes(r.id))
const shown = a.mode === 'search' && a.running ? rows.slice(0, a.revealed) : rows
return (
QUOTATION INTELLIGENCE
历史项目与报价证据
先定位项目,再核对成本与风险依据。
精确筛选 · 项目 / 客户 / 编号 / 日期
{([['project', '项目名称', 'text'], ['customer', '客户名称', 'text'], ['number', '报价编号', 'text'], ['from', '开始日期', 'date'], ['to', '结束日期', 'date']] as const).map(([key, label, type]) => (
))}
项目匹配结果 {shown.length}
本机快照 {library.length} 条 · 示例 2 条
{filters.from && filters.to && filters.from > filters.to
?
: shown.length ? (
{shown.map((r: any) => (
{r.kind === 'saved' ? '本机归档快照' : 'DEMO参考案例'}
{r.date}
{r.project}
{r.customer}
{r.number} · {r.sets}套 · {r.days}天
¥ {money(r.price)}不含税总价
selectAgentQuotation(r.id)} cls={a.selected === r.id ? 'primary' : ''} />
analyzeSelectedQuotation(r.id)} />
openHistoryDetail(r.id)} />
))}
) : a.running && a.mode === 'search' ? (
) : (
未找到符合条件的报价,请调整自然语言或筛选条件。
)}
)
function openHistoryDetail(id: string) {
const r = library.find((x) => x.id === id)
if (!r) {
if (HISTORY_CASES[id]) openModal('case', '历史案例 ' + id + ' / 完整项目参考', id)
return
}
openModal('historyDetail', '历史报价详情 / ' + r.number, id)
}
}
function QuotationHistoryDetail({ id }: { id: string }) {
const { library, openModal } = useApp()
const r = library.find((x) => x.id === id)
if (!r) return null
const snap = r.snapshot
const material = snap.materials.reduce((sum, m) => sum + m.qty * m.price, 0)
const process = snap.resources.reduce((sum, x) => sum + x.qty * (x.rate || 0), 0)
return (
openModal('library', '历史报价信息库 / AI智能体工作台')} />
入库报价单原件
内部物料依据
[m.id, m.name, `${m.qty} ${m.unit}`, money(m.price), money(m.qty * m.price), m.ref])} />
内部工艺资源
[x.name, x.qty, money(x.rate || 0), money(x.qty * (x.rate || 0)), x.ref])} />
物料合计:{money(material)} 元;工艺资源合计:{money(process)} 元。
[k, money(v) + ' 元']),
['间接费率', snap.overhead + '%'],
['风险准备', snap.risk + '%'],
['利润底线', snap.floor + '%'],
]} />
openModal('library', '历史报价信息库 / AI智能体工作台')} />
)
}
// ============ 简单表单弹窗 ============
function NewMaterialModal() {
const { submitMaterial, closeModal } = useApp()
const [name, setName] = useState('人工补充物料')
const [qty, setQty] = useState('1')
const [unit, setUnit] = useState('件')
const [price, setPrice] = useState('100')
return (
<>
数量 setQty(e.target.value)} />
单位 setUnit(e.target.value)} />
单价 setPrice(e.target.value)} />
>
)
}
function NewResourceModal() {
const { submitResource, closeModal } = useApp()
const [name, setName] = useState('')
const [ref, setRef] = useState('')
const [qty, setQty] = useState('1')
const [note, setNote] = useState('')
return (
)
}
function PreviewModal({ payload }: { payload: { url: string; name: string; pdf: boolean } }) {
return payload.pdf
?
:
}
// ============ 弹窗宿主 ============
export function ModalHost() {
const { modal, closeModal } = useApp()
const dlg = useRef(null)
useEffect(() => {
const el = dlg.current
if (!el) return
if (modal.open && !el.open) el.showModal()
if (!modal.open && el.open) el.close()
}, [modal.open])
const cls = modal.kind === 'library' ? 'quote-agent-dialog quotation-library-dialog'
: modal.kind === 'network' ? 'material-network-dialog' : ''
return (
)
}