categoryModal.vue
2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<template>
<div>
<BasicModal
v-bind="$attrs"
width="35rem"
:title="getTitle"
@register="register"
@cancel="handleCancel"
@ok="handleOk"
destroyOnClose
>
<div>
<BasicForm @register="registerForm" />
</div>
</BasicModal>
</div>
</template>
<script setup lang="ts">
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form';
import { computed, ref, unref } from 'vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { schemas } from '/@/views/equipment/category/index';
import { useMessage } from '/@/hooks/web/useMessage';
import { saveCategory } from '/@/api/equipment/category';
const isUpdate = ref<Boolean>(false);
const parentId = ref<string>('');
const { t } = useI18n();
const emit = defineEmits(['handleReload', 'register']);
const { createMessage } = useMessage();
const [registerForm, { getFieldsValue, setFieldsValue, validate, resetFields }] = useForm({
labelWidth: 150,
schemas,
actionColOptions: {
span: 14,
},
showActionButtonGroup: false,
});
const recordInfo = ref<Recordable>({});
const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
setModalProps({ confirmLoading: false, loading: true });
isUpdate.value = data?.isUpdate;
parentId.value = data?.parentId;
recordInfo.value = data?.record;
if (data?.record) {
setFieldsValue(data?.record);
}
setModalProps({ loading: false });
});
const getTitle = computed(() =>
!unref(isUpdate)
? t('equipment.category.createCategoryText')
: t('equipment.category.editCategoryText')
);
const handleCancel = () => closeModal();
const handleOk = async () => {
await validate();
let values = getFieldsValue();
if (unref(isUpdate)) {
values = { ...values, id: unref(recordInfo).id, parentId: unref(recordInfo).parentId };
} else {
values = { ...values, parentId: unref(parentId) };
}
await saveCategory(values);
createMessage.success(t('common.operationSuccessText'));
emit('handleReload');
handleCancel();
};
</script>