FormModal.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-2">
  12. <BasicForm @register="registerForm" layout="vertical">
  13. <template #wardInfo="{ model, field }">
  14. <div class="flex">
  15. <div
  16. v-for="item in model[field]"
  17. :key="item"
  18. :style="{
  19. backgroundColor: formatDictColor(bizDictOptions.wardType, item),
  20. color: formatDictFontColor(bizDictOptions.wardType, item),
  21. padding: '1px 6px',
  22. borderRadius: '2px',
  23. marginRight: '4px',
  24. }"
  25. >
  26. {{ formatDictValue(bizDictOptions.wardType, item) }}
  27. </div>
  28. </div>
  29. </template>
  30. <template #deviceInfo="{ model, field }">
  31. {{ model[field] }}
  32. </template>
  33. <template #deviceType="{ model, field }">
  34. <span
  35. :class="['table-dot']"
  36. :style="{
  37. backgroundColor: formatDictPreColor(bizDictOptions.deviceType, model[field]),
  38. }"
  39. />
  40. <span>{{ formatDictValue(bizDictOptions.deviceType, model[field]) }}</span>
  41. </template>
  42. </BasicForm>
  43. </div>
  44. </BasicModal>
  45. </template>
  46. <script lang="ts" setup>
  47. import { onBeforeMount, ref, computed, reactive, unref } from 'vue';
  48. import { BasicModal, useModalInner } from '/@/components/Modal';
  49. import { BasicForm, useForm } from '/@/components/Form';
  50. import { useMessage } from '/@/hooks/web/useMessage';
  51. import { dataFormSchema } from './data';
  52. import { listDictModelBatch } from '/@/api/common';
  53. import { engineerBedAdd, engineerBedDetail, engineerBedEdit } from '@/api/biz/engineer/bedApi';
  54. import {
  55. formatDictColor,
  56. formatDictValue,
  57. formatDictPreColor,
  58. formatDictFontColor,
  59. } from '@/utils';
  60. const bizDictData = ref([
  61. { key: 'wardType', dictCode: 'pb_epidemic' },
  62. { key: 'deviceType', dictCode: 'bm_det' },
  63. ]);
  64. const bizDictOptions = reactive<any>({});
  65. // 组件加载前事件
  66. onBeforeMount(async () => {
  67. const res = await listDictModelBatch(bizDictData.value.map(ele => ele.dictCode));
  68. for (const i in res) {
  69. const filter = bizDictData.value.filter(ele => ele.dictCode == i)[0];
  70. bizDictOptions[filter.key] = res[i];
  71. }
  72. });
  73. const emit = defineEmits(['success', 'register']);
  74. const isUpdate = ref(false);
  75. const getTitle = computed(() => (!unref(isUpdate) ? '新增床位' : '绑定床位'));
  76. const width = '600px';
  77. const rowId = ref();
  78. const { createMessage } = useMessage();
  79. const [registerForm, { setFieldsValue, resetFields, validate, updateSchema }] = useForm({
  80. labelWidth: 150,
  81. schemas: dataFormSchema,
  82. showActionButtonGroup: false,
  83. baseColProps: {
  84. span: 24,
  85. },
  86. wrapperCol: {
  87. span: 22,
  88. },
  89. });
  90. const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
  91. await resetFields();
  92. setModalProps({ confirmLoading: false });
  93. isUpdate.value = !!data?.isUpdate;
  94. if (unref(isUpdate)) {
  95. const resData = await engineerBedDetail(data.record.id);
  96. rowId.value = resData.id;
  97. await setFieldsValue({
  98. ...resData,
  99. name: resData.bedName,
  100. wardInfo: resData.infectiousDiseases,
  101. });
  102. await updateSchema([
  103. {
  104. field: 'wardId',
  105. componentProps: {
  106. disabled: true,
  107. },
  108. },
  109. ]);
  110. await updateSchema({
  111. field: 'name',
  112. componentProps: {
  113. disabled: true,
  114. },
  115. });
  116. }
  117. });
  118. // 提交按钮事件
  119. async function handleSubmit() {
  120. try {
  121. const values = await validate();
  122. setModalProps({ confirmLoading: true });
  123. if (!isUpdate.value) {
  124. await engineerBedAdd({ ...values });
  125. createMessage.success('新增成功!');
  126. } else {
  127. await engineerBedEdit({ ...values, id: rowId.value });
  128. console.log(values);
  129. createMessage.success('绑定成功!');
  130. }
  131. closeModal();
  132. emit('success', {
  133. values: { ...values, id: rowId.value },
  134. });
  135. } finally {
  136. setModalProps({ confirmLoading: false });
  137. }
  138. }
  139. </script>
  140. <style lang="less" scoped>
  141. .table-dot {
  142. display: inline-block;
  143. width: 10px;
  144. height: 10px;
  145. margin-right: 6px;
  146. border-radius: 50%;
  147. }
  148. </style>