Files
simrsx-fe/app/components/content/soapi/form.vue
2025-12-09 23:23:07 +07:00

181 lines
4.4 KiB
Vue

<script setup lang="ts">
import { z } from 'zod'
import Entry from '~/components/app/soapi/entry.vue'
import Action from '~/components/pub/my-ui/nav-footer/ba-dr-su.vue'
import ActionDialog from '~/components/pub/my-ui/nav-footer/ba-su.vue'
import Dialog from '~/components/pub/my-ui/modal/dialog.vue'
import { EarlySchema } from '~/schemas/soapi.schema'
import { toast } from '~/components/pub/ui/toast'
import { handleActionSave, handleActionEdit } from '~/handlers/soapi-early.handler'
const { goToEntry, backToList } = useQueryCRUDMode('mode')
const { recordId } = useQueryCRUDRecordId('record-id')
const route = useRoute()
const isOpenProcedure = ref(false)
const isOpenDiagnose = ref(false)
const procedures = ref([])
const diagnoses = ref([])
const selectedProcedure = ref<any>(null)
const selectedDiagnose = ref<any>(null)
const schema = EarlySchema
const payload = ref({
encounter_id: 0,
time: '',
typeCode: 'early-medic',
value: '',
})
const model = ref({
'prim-compl': '',
'cur-disea-hist': '',
'syst-bp': '',
'diast-bp': '',
pulse: '',
'resp-rate': '',
temp: '',
weight: '',
height: '',
'reflect-fisio': '',
'reflect-pato': '',
'autonom-neuron': '',
'medical-act': '',
therapy: '',
})
const isLoading = reactive<DataTableLoader>({
isTableLoading: false,
})
const formKey = ref(0)
async function getDetail() {
isLoading.isTableLoading = true
const resp = await xfetch(`/api/v1/soapi/${recordId.value}`)
if (resp.success) {
const raw = (resp.body as Record<string, any>).data
const values = JSON.parse(raw.value)
console.log('values', values)
model.value = values
formKey.value++ // Force re-render with new data
}
isLoading.isTableLoading = false
}
async function getDiagnose() {
isLoading.isTableLoading = true
const resp = await xfetch('/api/v1/diagnose-src')
if (resp.success) {
procedures.value = (resp.body as Record<string, any>).data
}
isLoading.isTableLoading = false
}
async function getProcedures() {
isLoading.isTableLoading = true
const resp = await xfetch('/api/v1/procedure-src')
if (resp.success) {
procedures.value = (resp.body as Record<string, any>).data
}
isLoading.isTableLoading = false
}
onMounted(() => {
getDetail()
})
function handleOpen(type: string) {
if (type === 'prosedur') {
isOpenProcedure.value = true
} else if (type === 'diagnosa') {
isOpenDiagnose.value = true
}
}
const entryRef = ref()
async function actionHandler(type: string) {
console.log(type)
if (type === 'back') {
backToList()
return
}
const result = await entryRef.value?.validate()
if (result?.valid) {
if (selectedProcedure.value?.length > 0 || selectedDiagnose.value?.length > 0) {
result.data.procedure = selectedProcedure.value || []
result.data.diagnose = selectedDiagnose.value || []
}
console.log('data', result.data)
handleActionSave(
{
...payload.value,
value: JSON.stringify(result.data),
encounter_id: +route.params.id,
time: new Date().toISOString(),
},
{},
toast,
)
} else {
console.log('Ada error di form', result)
}
}
const icdPreview = ref({
procedures: [],
diagnoses: [],
})
function actionDialogHandler(type: string) {
if (type === 'submit') {
icdPreview.value.procedures = selectedProcedure.value || []
icdPreview.value.diagnoses = selectedDiagnose.value || []
}
isOpenProcedure.value = false
isOpenDiagnose.value = false
}
provide('table_data_loader', isLoading)
provide('icdPreview', icdPreview)
</script>
<template>
<Entry
:key="formKey"
ref="entryRef"
v-model="model"
:schema="schema"
type="early"
@modal="handleOpen"
/>
<div class="my-2 flex justify-end py-2">
<Action @click="actionHandler" />
</div>
<Dialog
v-model:open="isOpenProcedure"
title="Pilih Prosedur"
size="xl"
prevent-outside
>
<AppIcdMultiselectPicker
v-model:model-value="selectedProcedure"
:data="procedures"
/>
<div class="my-2 flex justify-end py-2">
<ActionDialog @click="actionDialogHandler" />
</div>
</Dialog>
<Dialog
v-model:open="isOpenDiagnose"
title="Pilih Diagnosa"
size="xl"
prevent-outside
>
<AppIcdMultiselectPicker
v-model:model-value="selectedDiagnose"
:data="diagnoses"
/>
<div class="my-2 flex justify-end py-2">
<ActionDialog @click="actionDialogHandler" />
</div>
</Dialog>
</template>