/* ============================================================================= * M01_MasterData_UI_Logic_Calc.ts * 시험 계산(로직 복사 화면 · 855 2-B4) — 저장하면 지을 수식 문서(`/logic/formula/preview` 의 `문서`)를 * 문서 셈(`POST /m01/logic/doc/preview` · 줄 쌓기 화면과 같은 계산기)으로 셈 · 파일에 안 씀 * 옵션 가로(`formula_doc_options`) · 합계 상자(`formula_doc_foot`) = 줄 쌓기 화면 부품 그대로 — 금액 = 그 로직 수식 화면 금액 * 옵션을 바꾸면 바로 다시 셈(늦게 온 옛 답은 버림) * ========================================================================== */ import { API_BASE_URL } from "@config/config_frontend"; import { el } from "@ui/ui_template_elements"; import { createText } from "@ui/ui_template_text"; import type { FormulaDoc } from "../common_util/common_util_formula_doc"; import type { DocAnswer } from "../common_util/common_util_formula_doc_calc"; import { formatNumber } from "../common_util/common_util_money"; import { docFoot } from "../A00_Common/formula_doc_foot"; import { buildOptions } from "../A00_Common/formula_doc_options"; import { hiddenByWork } from "../A00_Common/formula_doc_render"; import "../A00_Common/line_stack.css"; import { safeReason, tx } from "./M01_MasterData_UI_Logic_Text"; /** 그릇마다 셈 차례 — 줄을 고쳐 다시 그려도 늦게 온 옛 답을 버림 */ const turns = new WeakMap(); async function previewDoc(문서: FormulaDoc, 입력: Record): Promise { const r = await fetch(`${API_BASE_URL}/m01/logic/doc/preview`, { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ 문서: { ...문서, 키: "_새" }, 입력 }), // 저장 전 = 빈 키(줄 쌓기와 같음) }); const data = (await r.json().catch(() => ({}))) as { 셈?: DocAnswer; detail?: unknown }; if (!r.ok || !data.셈) throw new Error(typeof data.detail === "string" ? data.detail : r.statusText); return data.셈; } function answerView(doc: FormulaDoc, a: DocAnswer): HTMLElement[] { const stop = !a.셈됨 && a.까닭 ? [ el("div", { className: "ui-banner ui-banner--danger m01-logic__reasons", children: [ el("strong", { text: tx("Calc_Stopped") }), el("div", { text: safeReason(a.까닭) }), ], }), ] : []; if (!doc.결과) return [...stop, docFoot(a)]; // 줄 쌓기 아래 합계 상자와 같은 부품 const value = typeof a.결과 === "number" ? `${formatNumber(a.결과)} ${doc.결과단위 ?? ""}`.trim() : "—"; return [ ...stop, el("dl", { className: "m01-logic__sums", children: [el("dt", { text: tx("Calc_Result") }), el("dd", { text: value })], }), ]; } /** `doc` = 저장하면 지을 수식 문서(못 지음 = null) · `given` = 고른 옵션(다시 그려도 남음) */ export function buildCalc( host: HTMLElement, doc: FormulaDoc | null, given: Record, ): void { const title = el("h3", { text: tx("Calc_Title") }); const note = el("p", { className: "ui-text ui-text--muted", text: tx("Calc_Draft") }); if (!doc) { host.replaceChildren(title, createText(tx("Calc_NoDoc"), "error")); return; } const run = async (): Promise => { const mine = (turns.get(host) ?? 0) + 1; turns.set(host, mine); try { const answer = await previewDoc(doc, given); if (turns.get(host) !== mine) return; const options = buildOptions( doc, given, false, (k, v, x) => { // 고르기 글 → 목록 값 그대로(수 45 를 글 「45」 로 보내면 표 줄을 못 찾음 · 줄 쌓기 `usable` 과 같음) const list = v.연결?.갈래 === "선택" ? (v.연결.목록 ?? []) : []; given[k] = list.find((o) => String(o) === String(x)) ?? x; void run(); }, undefined, hiddenByWork(doc, answer), // 줄 쌓기와 같이 — 시간당 식 줄이 안 읽게 된 옵션은 숨김 ); host.replaceChildren( title, note, ...(options.length ? [el("div", { className: "fdoc__options", children: options })] : []), ...answerView(doc, answer), ); } catch (error) { if (turns.get(host) !== mine) return; host.replaceChildren(title, createText(error instanceof Error ? error.message : "", "error")); } }; void run(); }