orderModal.vue
5.75 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<template>
<div>
<BasicModal
v-bind="$attrs"
width="30rem"
:title="getTitle"
@register="register"
@cancel="handleCancel"
@ok="handleOk"
destroyOnClose
>
<div>
<BasicForm @register="registerForm" />
<div style="display: flex;width: 70%;margin-left: 70px">
<div style="width: 122px;text-align: center">
报修人
</div>
<a-input
placeholder="请选择"
:disabled="true"
v-model:value="selectedUsername"
>
</a-input>
<a-button type="primary" @click="goChoose">选人</a-button>
</div>
</div>
<a-modal
v-model:visible="userVisible"
:title="userModalTitle"
width="60vw"
height="50vh"
@ok="handleUserOk"
@cancel="handleUserCancel"
>
<div style="padding: 20px;display: flex">
<OrganizationIdTree @select="handleSelect" ref="organizationIdTreeRef" isOpen="true"/>
<div style="margin-top: 20px;margin-left: 30px">
<a-radio-group v-model:value="selectedItem">
<template v-for="item in Options" :key="`${item.id}`">
<a-radio :style="radioStyle" :value="item">{{ item.username }}</a-radio>
</template>
</a-radio-group>
</div>
</div>
</a-modal>
</BasicModal>
</div>
</template>
<script setup lang="ts">
import {BasicModal, useModalInner} from "/@/components/Modal";
import {BasicForm, useForm} from "/@/components/Form";
import {computed, reactive, ref, unref} from "vue";
import {useI18n} from "/@/hooks/web/useI18n";
import {schemas} from "../index";
import {getUserListByOrg} from "/@/api/equipment/ledger";
import {useUserStore} from "/@/store/modules/user";
import {saveOrder} from "/@/api/repair/order";
import {useMessage} from "/@/hooks/web/useMessage";
import { useResetOrganizationTree, OrganizationIdTree } from '/@/views/common/organizationIdTree';
const { t } = useI18n();
const isUpdate = ref<Boolean>(false);
const recordInfo = ref<any>({});
// 定义人员选项
const userInfo = useUserStore();
const { createMessage } = useMessage();
const userOptions = ref([]);
const Options = ref([]);
const searchInfo = reactive<Recordable>({});
const { organizationIdTreeRef } = useResetOrganizationTree(searchInfo);
const userVisible = ref(false);
const userModalTitle = ref('选人');
const selectedItem = ref<{ id: string; username: string } | null>(null);
const radioStyle = reactive({
display: 'block',
height: '30px',
lineHeight: '30px',
});
// 计算属性实现双向绑定
const selectedUsername = computed({
get: () => selectedItem.value?.username || '',
set: (value) => {
if (selectedItem.value) {
selectedItem.value.username = value;
}
},
});
const emit = defineEmits(['handleReload', 'register']);
const getTitle = computed(() =>
!unref(isUpdate)
? t('repair.order.createOrderText')
: t('repair.order.editOrderText')
);
const [registerForm, { getFieldsValue, setFieldsValue, validate,updateSchema }] = useForm({
labelWidth: 140,
schemas,
actionColOptions: {
span: 14,
},
showActionButtonGroup: false,
});
const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
setModalProps({ confirmLoading: false, loading: true });
isUpdate.value = data?.isUpdate;
recordInfo.value = data?.record;
if (!selectedItem.value) {
selectedItem.value = { id: '', username: '' };
}
selectedItem.value.username = data?.record?.reportByName || '';
selectedItem.value.id = data?.record?.reportBy || '';
updateSchema({
field: 'situationImg',
componentProps: {
onChange: handleOrgChange,
},
});
if (data?.record) {
setFieldsValue(data?.record);
}
setModalProps({ loading: false });
});
// 监听 org 字段的变化
const handleOrgChange = async (orgId: string) => {
if (!orgId) {
userOptions.value = []; // 清空人员选项
updateSchema({
field: 'report',
componentProps: { options: [] },
});
return;
}
const _data = {
page: '1',
pageSize: '999',
tenantId: userInfo.getUserInfo.tenantId!,
organizationId: orgId
}
// 调用接口 B 获取人员数据
const userList = await getUserListByOrg(_data);
userOptions.value = userList?.items.map(user => ({
label: user.username,
value: user.id,
}));
// 更新 user 字段的选项
updateSchema({
field: 'report',
componentProps: { options: userOptions.value },
});
};
const handleCancel = () => {
closeModal();
};
const handleOk = async () => {
await validate();
let values = getFieldsValue();
if (unref(isUpdate)) {
values = { ...values, id: unref(recordInfo).id, situationImg: values.situationImg?.[0]?.url, reportBy: selectedItem.value?.id || ''};
}else {
values = { ...values, situationImg: values.situationImg?.[0]?.url, reportBy: selectedItem.value?.id || ''};
}
await saveOrder(values);
createMessage.success(t('common.operationSuccessText'));
emit('handleReload');
handleCancel();
};
const goChoose = () => {
userVisible.value = true;
selectedItem.value = null;
}
// 确认按钮的回调
const handleUserOk = () => {
if (!selectedItem.value) {
createMessage.warning('请选择一个用户');
return;
}
userVisible.value = false; // 关闭弹框
};
const handleUserCancel = () => {
selectedItem.value = null;
userVisible.value = false;
};
const handleSelect = async (organizationId: string) => {
searchInfo.organizationId = organizationId;
const _data = {
page: '1',
pageSize: '999',
tenantId: userInfo.getUserInfo.tenantId!,
organizationId: organizationId
}
const response = await getUserListByOrg(_data); // 调用接口
Options.value = response.items;
};
</script>