wardInfoFormModal.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. destroyOnClose
  5. @register="registerModal"
  6. :title="getTitle"
  7. @ok="handleSubmit"
  8. >
  9. <BasicForm @register="registerForm" />
  10. </BasicModal>
  11. </template>
  12. <script lang="ts" setup>
  13. import { ref, computed, unref } from 'vue';
  14. import { BasicModal, useModalInner } from '/@/components/Modal';
  15. import { BasicForm, useForm } from '/@/components/Form';
  16. import { useMessage } from '/@/hooks/web/useMessage';
  17. import { wardInfoDataFormSchema } from './data';
  18. import { wardInfoAdd, wardInfoEdit, wardInfoById } from '/@/api/biz/management/wardInfo';
  19. const emit = defineEmits(['success', 'register']);
  20. const getTitle = computed(() => (!unref(isUpdate) ? '新增病区信息' : '编辑病区信息'));
  21. const isUpdate = ref(false);
  22. const rowId = ref();
  23. const { createMessage } = useMessage();
  24. const [registerForm, { resetFields, validate, setFieldsValue }] = useForm({
  25. layout: 'vertical',
  26. showResetButton: true,
  27. labelWidth: 100,
  28. schemas: wardInfoDataFormSchema,
  29. showActionButtonGroup: false,
  30. actionColOptions: {
  31. span: 23,
  32. },
  33. });
  34. const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
  35. await resetFields();
  36. setModalProps({ confirmLoading: false });
  37. isUpdate.value = !!data?.isUpdate;
  38. if (unref(isUpdate)) {
  39. rowId.value = data.record.configId;
  40. const resData = await wardInfoById(data.record.id);
  41. await setFieldsValue({
  42. ...resData,
  43. });
  44. }
  45. });
  46. // 提交按钮事件
  47. async function handleSubmit() {
  48. try {
  49. const values = await validate();
  50. setModalProps({ confirmLoading: true });
  51. !unref(isUpdate)
  52. ? await wardInfoAdd({ ...values })
  53. : await wardInfoEdit({ ...values, configId: rowId.value });
  54. !unref(isUpdate) ? createMessage.success('新增成功!') : createMessage.success('编辑成功!');
  55. closeModal();
  56. emit('success', { isUpdate: unref(isUpdate), values: { ...values, configId: rowId.value } });
  57. } finally {
  58. setModalProps({ confirmLoading: false });
  59. }
  60. }
  61. </script>