FormModal.vue 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. archivesPatientBasicAdd,
  24. archivesPatientBasicEdit,
  25. archivesPatientBasicDetail,
  26. } from '/@/api/biz/archives/patientBasicApi';
  27. const emit = defineEmits(['success', 'register']);
  28. const getTitle = computed(() => (!unref(isUpdate) ? '新增基础病历' : '编辑基础病历'));
  29. const width = '45%';
  30. const isUpdate = ref(false);
  31. const rowId = ref();
  32. const { createMessage } = useMessage();
  33. const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
  34. labelWidth: 100,
  35. schemas: dataFormSchema,
  36. showActionButtonGroup: false,
  37. actionColOptions: {
  38. span: 23,
  39. },
  40. baseColProps: {
  41. span: 12,
  42. },
  43. wrapperCol: {
  44. span: 22,
  45. },
  46. });
  47. const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
  48. await resetFields();
  49. setModalProps({ confirmLoading: false });
  50. isUpdate.value = !!data?.isUpdate;
  51. if (unref(isUpdate)) {
  52. const resData = await archivesPatientBasicDetail(data.record.id);
  53. rowId.value = resData.id;
  54. resData.cardNo = {
  55. input: resData.cardNo,
  56. dictValue: resData.cardType,
  57. };
  58. await setFieldsValue({
  59. ...resData,
  60. });
  61. } else {
  62. await setFieldsValue({
  63. cardNo: {
  64. input: '',
  65. dictValue: 'pb_card_sfz',
  66. },
  67. });
  68. }
  69. });
  70. // 提交按钮事件
  71. async function handleSubmit() {
  72. try {
  73. const values = await validate();
  74. setModalProps({ confirmLoading: true });
  75. const cardInfo = values.cardNo;
  76. values.cardNo = cardInfo?.input;
  77. values.cardType = cardInfo?.dictValue;
  78. !unref(isUpdate)
  79. ? await archivesPatientBasicAdd({ ...values })
  80. : await archivesPatientBasicEdit({ ...values, id: rowId.value });
  81. !unref(isUpdate) ? createMessage.success('新增成功!') : createMessage.success('编辑成功!');
  82. closeModal();
  83. emit('success', {
  84. isUpdate: unref(isUpdate),
  85. values: { ...values, id: rowId.value },
  86. });
  87. } finally {
  88. setModalProps({ confirmLoading: false });
  89. }
  90. }
  91. </script>