FormModal.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. destroyOnClose
  5. @register="registerModal"
  6. :title="getTitle"
  7. :width="width"
  8. @ok="handleSubmit"
  9. :showFooter="true"
  10. >
  11. <BasicForm @register="registerForm" layout="vertical" class="!px-6 !pt-4" />
  12. </BasicModal>
  13. </template>
  14. <script lang="ts" setup>
  15. import { ref, reactive, unref } from 'vue';
  16. import { BasicModal, useModalInner } from '/@/components/Modal';
  17. import { BasicForm, useForm } from '/@/components/Form';
  18. import { useMessage } from '/@/hooks/web/useMessage';
  19. import { dataFormSchemaVascularAccess } from './data';
  20. import {
  21. archivesVascularAccessDetail,
  22. archivesVascularAccessEdit,
  23. archivesVascularAccessAdd,
  24. } from '/@/api/biz/archives/vascularAccessApi';
  25. import { listDictModel } from '/@/api/common';
  26. import { formatDictValue } from '/@/utils';
  27. import { useUserStore } from '@/store/modules/user';
  28. import dayjs from 'dayjs';
  29. const emit = defineEmits(['success', 'register']);
  30. const userStore = useUserStore();
  31. const getTitle = ref(`血管通路`);
  32. const width = '35%';
  33. const isUpdate = ref(false);
  34. const rowId = ref();
  35. const patientBasicId = ref();
  36. const bizDictOptions = reactive({
  37. accessType: [],
  38. });
  39. const accessType = ref();
  40. const { createMessage } = useMessage();
  41. const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
  42. schemas: dataFormSchemaVascularAccess,
  43. showActionButtonGroup: false,
  44. baseColProps: {
  45. span: 24,
  46. },
  47. });
  48. const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
  49. console.log('🚀 ~ file: FormModal.vue:49 ~ data:', data);
  50. await resetFields();
  51. setModalProps({ confirmLoading: false });
  52. bizDictOptions.accessType = await listDictModel({ dictCode: 'va_type' });
  53. isUpdate.value = !!data?.isUpdate;
  54. patientBasicId.value = data.record.id;
  55. if (unref(isUpdate)) {
  56. const resData = await archivesVascularAccessDetail(patientBasicId.value);
  57. rowId.value = resData.id;
  58. patientBasicId.value = resData.patientBasicId;
  59. resData.accessType = resData.type;
  60. accessType.value = formatDictValue(bizDictOptions.accessType, resData.type);
  61. await setFieldsValue({
  62. ...resData,
  63. });
  64. } else {
  65. accessType.value = formatDictValue(bizDictOptions.accessType, data.record.accessType);
  66. await setFieldsValue({
  67. setUpHospital: userStore.getTenant?.name,
  68. setUpTime: dayjs(),
  69. accessType: data.record.accessType,
  70. });
  71. }
  72. getTitle.value = `${isUpdate.value ? '编辑' : '新建'}${accessType.value} ( ${
  73. data.record.name
  74. } | ${data.record.gender} | ${data.record.age}岁 )`;
  75. });
  76. // 提交按钮事件
  77. async function handleSubmit() {
  78. try {
  79. const values = await validate();
  80. setModalProps({ confirmLoading: true });
  81. values.patientBasicId = patientBasicId.value;
  82. values.type = values.accessType;
  83. delete values.accessType;
  84. !unref(isUpdate)
  85. ? await archivesVascularAccessAdd({ ...values })
  86. : await archivesVascularAccessEdit({ ...values, id: rowId.value });
  87. !unref(isUpdate) ? createMessage.success('新增成功!') : createMessage.success('编辑成功!');
  88. closeModal();
  89. emit('success', {
  90. isUpdate: unref(isUpdate),
  91. values: { ...values, id: rowId.value },
  92. });
  93. } finally {
  94. setModalProps({ confirmLoading: false });
  95. }
  96. }
  97. </script>