<script setup lang="ts">
|
import { computed, ref } from 'vue';
|
|
import { useVbenDrawer, useVbenModal } from '@vben/common-ui';
|
import { $t } from '@vben/locales';
|
import { addFullName, cloneDeep, getPopupContainer, listToTree } from '@vben/utils';
|
|
import { InputSearch } from 'ant-design-vue';
|
|
import { useVbenForm } from '#/adapter/form';
|
import { addEqu, getEqu, updateEqu } from '#/api/eims/equ';
|
import { getEquType, listEquType } from '#/api/eims/equ-type';
|
import { getDeptTree, userList } from '#/api/system/user';
|
import userModal from '#/views/eims/components/user-modal.vue';
|
|
import { drawerSchema } from './data';
|
|
const emit = defineEmits<{ reload: [] }>();
|
const typeDisabled = ref(false);
|
|
const isUpdate = ref(false);
|
const title = computed(() => {
|
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
});
|
|
const [BasicForm, formApi] = useVbenForm({
|
commonConfig: {
|
formItemClass: 'col-span-2',
|
componentProps: {
|
class: 'w-full'
|
},
|
labelWidth: 120
|
},
|
schema: drawerSchema(),
|
showDefaultActions: false,
|
wrapperClass: 'grid-cols-2'
|
});
|
|
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
onCancel: handleCancel,
|
onConfirm: handleConfirm,
|
async onOpenChange(isOpen) {
|
if (!isOpen) {
|
return null;
|
}
|
drawerApi.drawerLoading(true);
|
const { id } = drawerApi.getData() as { id?: number | string };
|
const { typeDisable } = drawerApi.getData() as { typeDisable?: boolean };
|
typeDisabled.value = !!typeDisable;
|
isUpdate.value = !!id;
|
// 初始化
|
await setupDeptSelect();
|
// 更新 && 赋值
|
if (isUpdate.value && id) {
|
const record = await getEqu(id);
|
await formApi.setValues(record);
|
if (isUpdate.value && record.deptUsed) {
|
await setupUserOptions(record.deptUsed);
|
}
|
}
|
|
// 加载设备类型树选择
|
await setupEquTypeSelect();
|
if (id) {
|
await formApi.setFieldValue('parentId', id);
|
if (isUpdate.value && id) {
|
const record = await getEquType(id);
|
await formApi.setValues(record);
|
}
|
}
|
drawerApi.drawerLoading(false);
|
}
|
});
|
|
// user modal
|
const [UserModal, userModalApi] = useVbenModal({
|
connectedComponent: userModal,
|
draggable: true,
|
title: '选择成员'
|
});
|
|
function handleOpenModal() {
|
userModalApi.setData({});
|
userModalApi.open();
|
}
|
|
/**
|
* 打开选择成员
|
*/
|
const column = ref<string>();
|
function onOpenSelectUser(type: any) {
|
column.value = type;
|
handleOpenModal();
|
}
|
|
/**
|
* 更新选择的成员
|
* @param user
|
*/
|
async function selectUser(user: any) {
|
if (column.value === 'purchaseUser') {
|
await formApi.setValues({ 'purchaseUser': user.userId, 'purchaseUserName': user.nickName });
|
} else if (column.value === 'handleUserName') {
|
await formApi.setValues({ 'handleUser': user.userId, 'handleUserName': user.nickName });
|
}
|
}
|
|
async function setupEquTypeSelect() {
|
// status-0 只查询未停用设备
|
const equArray = await listEquType({ status: 0 });
|
// support i18n
|
equArray.forEach((item) => {
|
item.typeName = $t(item.typeName);
|
});
|
const equTree = listToTree(equArray, { id: 'equTypeId', pid: 'parentId' });
|
const fullEquTree = [
|
{
|
equTypeId: 0,
|
typeName: $t('menu.root'),
|
children: equTree
|
}
|
];
|
addFullName(fullEquTree, 'typeName', ' / ');
|
formApi.updateSchema([
|
{
|
componentProps: {
|
fieldNames: {
|
label: 'typeName',
|
value: 'equTypeId'
|
},
|
getPopupContainer,
|
// 设置弹窗滚动高度 默认256
|
listHeight: 300,
|
showSearch: true,
|
treeData: fullEquTree,
|
disabled: typeDisabled.value,
|
treeDefaultExpandAll: false,
|
// 默认展开的树节点
|
treeDefaultExpandedKeys: [0],
|
treeLine: { showLeafIcon: false },
|
// 筛选的字段
|
treeNodeFilterProp: 'typeName',
|
treeNodeLabelProp: 'fullName'
|
},
|
fieldName: 'equTypeId'
|
}
|
]);
|
}
|
|
/**
|
* 用户的加载
|
*/
|
async function setupUserOptions(deptId: any) {
|
const params = { deptId };
|
const userPageResult = await userList({
|
pageNum: 1,
|
pageSize: 500,
|
...params
|
});
|
const options = userPageResult.rows.map((item) => ({
|
label: item.nickName || item.userName,
|
value: item.userId
|
}));
|
// 筛选
|
const filterOption = (input: string, option: any) => {
|
return option.label.toLowerCase().includes(input.toLowerCase());
|
};
|
|
const placeholder = options.length > 0 ? '请选择' : '该部门下暂无用户';
|
formApi.updateSchema([
|
{
|
componentProps: { options, placeholder, filterOption },
|
fieldName: 'respPerson'
|
}
|
]);
|
}
|
|
/**
|
* 初始化部门选择
|
*/
|
async function setupDeptSelect() {
|
// updateSchema
|
const deptTree = await getDeptTree();
|
// 选中后显示在输入框的值 即父节点 / 子节点
|
addFullName(deptTree, 'label', ' / ');
|
formApi.updateSchema([
|
{
|
componentProps: (formModel) => ({
|
class: 'w-full',
|
fieldNames: {
|
key: 'id',
|
value: 'id',
|
children: 'children'
|
},
|
getPopupContainer,
|
async onSelect(deptId: number | string) {
|
/** 根据部门ID加载用户 */
|
await setupUserOptions(deptId);
|
/** 变化后需要重新选择用户 */
|
formModel.respPerson = undefined;
|
},
|
placeholder: '请选择',
|
showSearch: true,
|
treeData: deptTree,
|
treeDefaultExpandAll: true,
|
treeLine: { showLeafIcon: false },
|
// 筛选的字段
|
treeNodeFilterProp: 'label',
|
// 选中后显示在输入框的值
|
treeNodeLabelProp: 'fullName'
|
}),
|
fieldName: 'deptUsed'
|
}
|
]);
|
}
|
|
async function handleConfirm() {
|
try {
|
drawerApi.drawerLoading(true);
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
const data = cloneDeep(await formApi.getValues());
|
await (isUpdate.value ? updateEqu(data) : addEqu(data));
|
emit('reload');
|
await handleCancel();
|
} catch (error) {
|
console.error(error);
|
} finally {
|
drawerApi.drawerLoading(false);
|
}
|
}
|
|
async function handleCancel() {
|
drawerApi.close();
|
await formApi.resetForm();
|
}
|
</script>
|
|
<template>
|
<BasicDrawer :close-on-click-modal="false" :title="title" class="w-[600px]">
|
<BasicForm>
|
<template #purchaseUserName="slotProps">
|
<InputSearch :enter-button="true" placeholder="请选择" @search="onOpenSelectUser('purchaseUser')" v-bind="slotProps" />
|
</template>
|
<template #handleUserName="slotProps">
|
<InputSearch :enter-button="true" placeholder="请选择" @search="onOpenSelectUser('handleUserName')" v-bind="slotProps" />
|
</template>
|
</BasicForm>
|
<UserModal class="w-[1200px]" @select-user="selectUser" />
|
</BasicDrawer>
|
</template>
|