diff --git a/src/lib/components/dictionary/test/config/test-form-config.js b/src/lib/components/dictionary/test/config/test-form-config.js index 6dc2080..235c83a 100644 --- a/src/lib/components/dictionary/test/config/test-form-config.js +++ b/src/lib/components/dictionary/test/config/test-form-config.js @@ -550,6 +550,7 @@ export function getTestFormActions(handlers) { export function buildTestPayload({ mainForm, calForm, + refForm, testType }) { let payload = { @@ -561,7 +562,12 @@ export function buildTestPayload({ ...payload, ...calForm }; - } + } else if (testType === 'TEST' || 'PARAM') { + payload = { + ...payload, + ...refForm + } + } return cleanEmptyStrings(payload); } \ No newline at end of file diff --git a/src/lib/components/dictionary/test/page/create-page.svelte b/src/lib/components/dictionary/test/page/create-page.svelte index 9c0f19a..3a8724f 100644 --- a/src/lib/components/dictionary/test/page/create-page.svelte +++ b/src/lib/components/dictionary/test/page/create-page.svelte @@ -48,10 +48,12 @@ async function handleSave() { const mainForm = masterDetail.formState.form; const calForm = calFormState.form; + const refForm = refNumState.form; const payload = buildTestPayload({ mainForm, calForm, + refForm, testType: mainForm.TestType }); console.log(payload); diff --git a/src/lib/components/dictionary/test/page/tabs/calculation.svelte b/src/lib/components/dictionary/test/page/tabs/calculation.svelte index c16c88f..c19182d 100644 --- a/src/lib/components/dictionary/test/page/tabs/calculation.svelte +++ b/src/lib/components/dictionary/test/page/tabs/calculation.svelte @@ -27,7 +27,6 @@ try { const res = await fetch(`${API.BASE_URL}${API.TEST}`); const data = await res.json(); - console.log(data); options = data.data.map((item) => ({ value: item.TestSiteCode, diff --git a/src/lib/components/dictionary/test/page/tabs/ref-num.svelte b/src/lib/components/dictionary/test/page/tabs/ref-num.svelte index 5a7b27e..da42ecc 100644 --- a/src/lib/components/dictionary/test/page/tabs/ref-num.svelte +++ b/src/lib/components/dictionary/test/page/tabs/ref-num.svelte @@ -4,9 +4,22 @@ import * as Table from "$lib/components/ui/table/index.js"; import { Button } from "$lib/components/ui/button/index.js"; import { Badge } from "$lib/components/ui/badge/index.js"; + import { buildAgeText } from "$lib/utils/ageUtils"; + import PencilIcon from "@lucide/svelte/icons/pencil"; + import Trash2Icon from "@lucide/svelte/icons/trash-2"; + import { untrack } from "svelte"; let props = $props(); + let tempNumeric = $state([]); + let editingId = $state(null); + let idCounter = $state(0); +$inspect(tempNumeric) + let joinFields = $state({ + AgeStart: { DD: "", MM: "", YY: "" }, + AgeEnd: { DD: "", MM: "", YY: "" }, + }); + let disabledSign = $derived.by(() => { const refType = props.refType; @@ -16,11 +29,133 @@ return false; }); + function snapshotForm() { + const f = props.refNumState.form; + return { + SpcType: f.SpcType ?? "", + Sex: f.Sex ?? "", + AgeStart: f.AgeStart ?? "", + AgeEnd: f.AgeEnd ?? "", + NumRefType: f.NumRefType ?? "", + RangeType: f.RangeType ?? "", + LowSign: f.LowSign ?? "", + Low: f.Low ?? "", + HighSign: f.HighSign ?? "", + High: f.High ?? "", + Display: f.Display ?? "", + Flag: f.Flag ?? "", + Interpretation: f.Interpretation ?? "", + Notes: f.Notes ?? "", + }; + } + + function resetForm() { + props.refNumState.reset?.(); + joinFields = { + AgeStart: { DD: "", MM: "", YY: "" }, + AgeEnd: { DD: "", MM: "", YY: "" }, + }; + editingId = null; + } + + function handleInsert() { + const row = { id: ++idCounter, ...snapshotForm() }; + tempNumeric = [...tempNumeric, row]; + resetForm(); + } + + function handleEdit(row) { + editingId = row.id; + + const f = props.refNumState.form; + f.SpcType = row.SpcType; + f.Sex = row.Sex; + f.AgeStart = row.AgeStart; + f.AgeEnd = row.AgeEnd; + f.NumRefType = row.NumRefType; + f.RangeType = row.RangeType; + f.LowSign = row.LowSign; + f.Low = row.Low; + f.HighSign = row.HighSign; + f.High = row.High; + f.Display = row.Display; + f.Flag = row.Flag; + f.Interpretation = row.Interpretation; + f.Notes = row.Notes; + } + + function handleUpdate() { + tempNumeric = tempNumeric.map((row) => + row.id === editingId ? { id: row.id, ...snapshotForm() } : row + ); + resetForm(); + } + + function handleCancelEdit() { + resetForm(); + } + + function handleRemove(id) { + tempNumeric = tempNumeric.filter((row) => row.id !== id); + if (editingId === id) resetForm(); + } + + function getLabel(fieldKey, value) { + const opts = props.refNumState.selectOptions[fieldKey] ?? []; + const found = opts.find((o) => o.value == value); + return found ? found.label : value; + } + + function getCode(fieldKey, value) { + const opts = props.refNumState.selectOptions[fieldKey] ?? []; + const found = opts.find((o) => o.value == value); + return found ? found.code : value; + } + + const rangeTypeBadge = (type) => ({ REF: "REF", CRTC: "CRTC" }[type] ?? type); + + const rangeDisplay = (row) => { + if (row.NumRefType === "RANGE") return `${row.LowValue} - ${row.HighValue}`; + if (row.NumRefType === "THOLD") return row.TholdValue; + return "-"; + }; + $effect(() => { if (props.refType) { props.refNumState.form.NumRefType = props.refType; } }); + + $effect(() => { + for (const key of ["AgeStart", "AgeEnd"]) { + props.refNumState.form[key] = + buildAgeText(joinFields[key]); + } + }); + + $effect(() => { + const allColumns = props.refNumFormFields.flatMap( + (section) => section.rows.flatMap( + (row) => row.columns ?? [] + ) + ); + + untrack(() => { + for (const col of allColumns) { + if (!col.optionsEndpoint) continue; + + props.refNumState.fetchOptions?.( + { + key: col.key, + optionsEndpoint: col.optionsEndpoint, + valueKey: col.valueKey, + labelKey: col.labelKey, + }, + props.refNumState.form + ); + } + }) + });