FormModal.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. <div class="!pl-8 !pt-4">
  12. <BasicForm @register="registerForm" layout="vertical" />
  13. </div>
  14. </BasicModal>
  15. </template>
  16. <script lang="ts" setup>
  17. import { ref, computed, unref } from 'vue';
  18. import { BasicModal, useModalInner } from '/@/components/Modal';
  19. import { BasicForm, useForm } from '/@/components/Form';
  20. import { useMessage } from '/@/hooks/web/useMessage';
  21. import { dataFormSchema } from './data';
  22. import {
  23. archivesMedicalDocumentsAdd,
  24. archivesMedicalDocumentsEdit,
  25. archivesMedicalDocumentsDetail,
  26. } from '/@/api/biz/archives/medicalDocumentsApi';
  27. import dayjs from 'dayjs';
  28. const emit = defineEmits(['success', 'register']);
  29. const getTitle = computed(() => (!unref(isUpdate) ? '新增文书' : '编辑文书'));
  30. const width = '600px';
  31. const isUpdate = ref(false);
  32. const rowId = ref();
  33. const patientBasicId = ref();
  34. const { createMessage } = useMessage();
  35. const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
  36. labelWidth: 100,
  37. schemas: dataFormSchema,
  38. showActionButtonGroup: false,
  39. // baseColProps: {
  40. // span: 12,
  41. // },
  42. wrapperCol: {
  43. span: 22,
  44. },
  45. });
  46. const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
  47. await resetFields();
  48. setModalProps({ confirmLoading: false });
  49. isUpdate.value = data?.isUpdate;
  50. patientBasicId.value = data.record.patientBasicId;
  51. if (unref(isUpdate)) {
  52. const resData = await archivesMedicalDocumentsDetail(data.record.id);
  53. console.log('🚀 ~ file: FormModal.vue:58 ~ resData:', resData);
  54. rowId.value = resData.id;
  55. await setFieldsValue({
  56. ...resData,
  57. });
  58. } else {
  59. await setFieldsValue({
  60. updateTime: dayjs().format('YYYY-MM-DD'),
  61. files: [],
  62. });
  63. }
  64. });
  65. // 提交按钮事件
  66. async function handleSubmit() {
  67. try {
  68. const values = await validate();
  69. setModalProps({ confirmLoading: true });
  70. values.patientBasicId = patientBasicId.value;
  71. values.fileIds = values.files && values.files.map(ele => ele.id);
  72. console.log('🚀 ~ file: FormModal.vue:71 ~ handleSubmit ~ values:', {
  73. ...values,
  74. id: rowId.value,
  75. });
  76. !unref(isUpdate)
  77. ? await archivesMedicalDocumentsAdd({ ...values })
  78. : await archivesMedicalDocumentsEdit({ ...values, id: rowId.value });
  79. !unref(isUpdate) ? createMessage.success('新增成功!') : createMessage.success('编辑成功!');
  80. closeModal();
  81. emit('success', {
  82. isUpdate: unref(isUpdate),
  83. values: { ...values, id: rowId.value },
  84. });
  85. } finally {
  86. setModalProps({ confirmLoading: false });
  87. }
  88. }
  89. </script>