ปรับปรุง phase 6 ให้ครบถ้วน

This commit is contained in:
Suphonchai Phoonsawat 2026-08-20 11:15:04 +07:00
parent 93570d38d8
commit a196b8f9a8
14 changed files with 747 additions and 40 deletions

View file

@ -1,6 +1,7 @@
<script setup lang="ts">
/** FR-10 UI — dynamic form สร้างเอกสารจาก field definitions ของ template → generate เป็น v1 + ดาวน์โหลด */
import type { GenerateDocumentResponse, TemplateDetail } from '~/types/api'
/** FR-10 UI — dynamic form สร้างเอกสารจาก field definitions ของ template → generate เป็น v1 + ดาวน์โหลด
* FR-12 — รองรับ re-generate: ?from={documentId} → prefill ค่าล่าสุดของเอกสารนั้นให้แก้ก่อนสร้าง */
import type { DocumentDetail, GenerateDocumentResponse, TemplateDetail } from '~/types/api'
const route = useRoute()
const { apiJson, downloadFile } = useApi()
@ -10,6 +11,10 @@ const detail = ref<TemplateDetail | null>(null)
const values = ref<Record<string, string>>({})
const issues = ref<Record<string, string>>({})
/** เอกสารต้นทางของ re-generate (จาก ?from=) — ใช้ทั้ง prefill และ banner บอกบริบท */
const fromDoc = ref<{ id: string; fileName: string } | null>(null)
const prefill = ref<Record<string, string>>({})
const loading = ref(true)
const loadError = ref<string | null>(null)
@ -29,12 +34,33 @@ useHead({
),
})
function blankValues(): Record<string, string> {
return Object.fromEntries((detail.value?.fields ?? []).map((f) => [f.key, '']))
}
async function load() {
loading.value = true
loadError.value = null
try {
detail.value = (await apiJson<TemplateDetail>(`/api/templates/${templateId.value}`))!
values.value = Object.fromEntries(detail.value.fields.map((f) => [f.key, '']))
const from = typeof route.query.from === 'string' ? route.query.from : null
if (from) {
try {
const src = await apiJson<DocumentDetail>(`/api/documents/${from}`)
if (src?.templateId) {
fromDoc.value = { id: src.id, fileName: src.fileName }
// prefill เฉพาะ key ที่มีใน defs ปัจจุบัน — key ที่หายไปหลัง replace ไฟล์โชว์ไม่ได้ (generate ก็ ignore)
prefill.value = Object.fromEntries(
detail.value.fields.map((f) => [f.key, src.fields.find((s) => s.key === f.key)?.value ?? '']),
)
}
} catch {
// ไม่พบเอกสารต้นทาง — ปล่อยให้เป็นฟอร์มว่างตามปกติ
}
}
values.value = { ...blankValues(), ...prefill.value }
} catch (err) {
loadError.value = err instanceof Error ? err.message : 'โหลด template ไม่สำเร็จ'
} finally {
@ -80,13 +106,13 @@ async function download() {
}
}
/** กลับไปกรอกฟอร์มใหม่เพื่อสร้างอีกฉบับจาก template เดิม */
/** กลับไปกรอกฟอร์มใหม่เพื่อสร้างอีกฉบับจาก template เดิม (ถ้าเป็น re-generate → คืนค่า prefill ไม่ใช่ฟอร์มว่าง) */
function resetForm() {
result.value = null
submitError.value = null
downloadError.value = null
issues.value = {}
values.value = Object.fromEntries((detail.value?.fields ?? []).map((f) => [f.key, '']))
values.value = { ...blankValues(), ...prefill.value }
}
onMounted(load)
@ -175,6 +201,15 @@ onMounted(load)
</p>
</div>
<AlertBox v-if="fromDoc">
กำลังสร้างเอกสารใหม่จากค่าปัจจุบันของ <strong class="break-all">{{ fromDoc.fileName }}</strong>
— แก้ค่าได้ก่อนกดสร้าง และเอกสารต้นทางจะไม่ถูกแตะ (ไม่เกิด version ใหม่)
</AlertBox>
<AlertBox v-if="!detail.isActive">
template นี้ถูก deactivate แล้ว — สร้างเอกสารใหม่ไม่ได้ (เอกสารที่สร้างไว้แล้วยังใช้ได้)
</AlertBox>
<div
v-for="f in sortedFields"
:key="f.key"
@ -210,7 +245,7 @@ onMounted(load)
<button
type="submit"
:disabled="submitting || sortedFields.length === 0"
:disabled="submitting || sortedFields.length === 0 || !detail.isActive"
class="rounded-md bg-sky-600 px-4 py-2 text-sm font-medium text-white hover:bg-sky-700 disabled:opacity-50"
>
{{ submitting ? 'กำลังสร้างเอกสาร…' : 'สร้างเอกสาร (version 1)' }}