modifyTeaching.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <a-modal
  3. v-model:visible="visible"
  4. :width="500"
  5. title-align="start"
  6. top="10vh"
  7. :align-center="false"
  8. @before-open="modalBeforeOpen"
  9. >
  10. <template #title> {{ title }} </template>
  11. <a-form ref="formRef" :model="formData" :rules="rules" auto-label-width>
  12. <a-form-item field="name" label="教学点名称">
  13. <a-input
  14. v-model.trim="formData.name"
  15. placeholder="请输入"
  16. allow-clear
  17. ></a-input>
  18. </a-form-item>
  19. <a-form-item field="code" label="教学点代码">
  20. <a-input
  21. v-model.trim="formData.code"
  22. placeholder="请输入"
  23. allow-clear
  24. ></a-input>
  25. </a-form-item>
  26. <a-form-item field="capacity" label="容量">
  27. <a-input-number
  28. v-model="formData.capacity"
  29. placeholder="请输入"
  30. :style="{ width: '120px' }"
  31. :min="1"
  32. :max="99999"
  33. :step="1"
  34. />
  35. </a-form-item>
  36. <a-form-item field="cityId" label="城市">
  37. <a-select
  38. v-model="formData.cityId"
  39. placeholder="请选择"
  40. filter-option
  41. allow-clear
  42. :options="[]"
  43. >
  44. </a-select>
  45. </a-form-item>
  46. </a-form>
  47. <template #footer>
  48. <a-button type="primary" :disabled="loading" @click="confirm"
  49. >确认</a-button
  50. >
  51. <a-button @click="close">取消</a-button>
  52. </template>
  53. </a-modal>
  54. </template>
  55. <script setup lang="ts">
  56. import { computed, nextTick, reactive, ref } from 'vue';
  57. import { Message } from '@arco-design/web-vue';
  58. import type { FormInstance } from '@arco-design/web-vue/es/form';
  59. import { updateTeaching } from '@/api/base';
  60. import useLoading from '@/hooks/loading';
  61. import useModal from '@/hooks/modal';
  62. import { objAssign, objModifyAssign } from '@/utils/utils';
  63. import { TeachingItem, TeachingUpdateParams } from '@/api/types/base';
  64. import { FormRules } from '@/types/global';
  65. defineOptions({
  66. name: 'ModifyTeaching',
  67. });
  68. /* modal */
  69. const { visible, open, close } = useModal();
  70. defineExpose({ open, close });
  71. const defaultFormData = {
  72. id: null,
  73. name: '',
  74. code: '',
  75. cityId: null,
  76. capacity: undefined,
  77. };
  78. const props = defineProps<{
  79. rowData: TeachingItem;
  80. }>();
  81. const emit = defineEmits(['modified']);
  82. const isEdit = computed(() => !!props.rowData.id);
  83. const title = computed(() => `${isEdit.value ? '编辑' : '新增'}教学点`);
  84. const formRef = ref<FormInstance>();
  85. const formData = reactive<TeachingUpdateParams>({ ...defaultFormData });
  86. const rules: FormRules<TeachingUpdateParams> = {
  87. name: [
  88. {
  89. required: true,
  90. message: '请输入名称',
  91. },
  92. {
  93. max: 50,
  94. message: '名称不能超过50字符',
  95. },
  96. ],
  97. code: [
  98. {
  99. required: true,
  100. message: '请输入代码',
  101. },
  102. {
  103. max: 50,
  104. message: '代码不能超过50字符',
  105. },
  106. ],
  107. capacity: [
  108. {
  109. required: true,
  110. message: '请输入容量',
  111. },
  112. ],
  113. cityId: [
  114. {
  115. required: true,
  116. message: '请选择城市',
  117. },
  118. ],
  119. };
  120. /* confirm */
  121. const { loading, setLoading } = useLoading();
  122. async function confirm() {
  123. const err = await formRef.value?.validate();
  124. if (err) return;
  125. setLoading(true);
  126. const datas = objAssign(formData, {});
  127. const res = await updateTeaching(datas).catch(() => false);
  128. setLoading(false);
  129. if (!res) return;
  130. Message.success('修改成功!');
  131. emit('modified', datas);
  132. close();
  133. }
  134. /* init modal */
  135. function modalBeforeOpen() {
  136. if (props.rowData.id) {
  137. objModifyAssign(formData, props.rowData);
  138. } else {
  139. objModifyAssign(formData, defaultFormData);
  140. }
  141. nextTick(() => {
  142. formRef.value?.clearValidate();
  143. });
  144. }
  145. </script>