
<template>
  <div class="h-full py-4 flex flex-col">
    <el-select v-model="curForm" filterable class="mx-4" :disabled="formDisabled">
      <el-option v-if="!formList[curForm]" :value="0" label="请选择表单"></el-option>
      <el-option v-for="(item, index) in formList" :key="index" :label="item.formTitle" :value="index"></el-option>
    </el-select>
    <div class="w-full overflow-y-auto mt-4 flex-1">
      <div v-if="formList[curForm]">
        <ArchivesModule
          ref="HealthyFileRef" is-plat
          :module-id="formList[curForm].formId"
          :form="formList[curForm].form"
        />
      </div>
    </div>
    <div class="p-4 flex justify-center items-center">
      <el-button class="w-full" type="primary" :disabled="!formList[curForm]"	size="large" @click="submitFormData">保存</el-button>
    </div>
  </div>
</template>
<script setup>

import { computed, ref, shallowRef, provide, nextTick, watch, inject, defineAsyncComponent } from 'vue';

import { request, parseJsonData } from "@/utils";

import ArchivesModule from './ArchivesModule.vue';
import { ElMessage } from 'element-plus';

const curForm = ref(0)
const formDisabled = ref(false)
const formList = shallowRef([])
const getFormConfig = async () => {
  const { data } = await request.get(`/idcService/mechanism/formConfig/get`)
  formList.value = data.list
  if (formId.value) {
    const idx = formList.value.findIndex(v => v.formId == formId.value)
    if (idx >= 0 && transferRawJson.value) {
      curForm.value = idx
      formDisabled.value = true
    }
  }
}

const transferRawJson = inject('transferRawJson')
const formId = inject('formId')
getFormConfig()

// const submitFormData = inject('submitFormData')

const submitSimpleFormData = async (answers, close) => {
  const answersNo = answers.map(v => v.answer.questionNo)
  transferRawJson.value = [...transferRawJson.value?.filter(v => !answersNo.includes(v.answer.questionNo)), ...answers]
  close && close()
}
provide('submitSimpleFormData', submitSimpleFormData)
const sn = inject('sn')
const submitFormData = async () => {
  const req = {
    sn,
    data: {
      formId: formList.value[curForm.value].formId,
      answers: transferRawJson.value
    }
  }
  console.log(req)
  await request.post(`/idcService/mechanism/medicalData/update/transferResult`, req)
  ElMessage.success('操作成功')
  formDisabled.value = true
  // getDetail()
}
</script>

<style lang='scss' scoped>

</style>

