<script setup lang="ts">
|
import { computed, ref } from 'vue';
|
|
import { useVbenDrawer } from '@vben/common-ui';
|
import { $t } from '@vben/locales';
|
import { addFullName, cloneDeep, getPopupContainer, listToTree } from '@vben/utils';
|
|
import { useVbenForm } from '#/adapter/form';
|
import { addFixture, getFixture, updateFixture } from '#/api/eims/fixture';
|
import { getFixtureType, listFixtureType } from '#/api/eims/fixture-type';
|
import { getDeptTree, userList } from '#/api/system/user';
|
|
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,
|
closeOnPressEscape: true,
|
placement: 'left',
|
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 getFixture(id);
|
await formApi.setValues(record);
|
if (isUpdate.value && record.borrowDept) {
|
await setupUserOptions(record.borrowDept);
|
}
|
}
|
|
// 加载工具类型树选择
|
await setupFixtureTypeSelect();
|
if (id) {
|
await formApi.setFieldValue('parentId', id);
|
if (isUpdate.value && id) {
|
const record = await getFixtureType(id);
|
await formApi.setValues(record);
|
}
|
}
|
drawerApi.drawerLoading(false);
|
}
|
});
|
|
async function setupFixtureTypeSelect() {
|
// status-0 只查询未停用工具
|
const fixArray = await listFixtureType({ status: 0 });
|
// support i18n
|
fixArray.forEach((item) => {
|
item.typeName = $t(item.typeName);
|
});
|
const fixTree = listToTree(fixArray, { id: 'id', pid: 'parentId' });
|
const fullTree = [
|
{
|
id: 0,
|
typeName: $t('menu.root'),
|
children: fixTree
|
}
|
];
|
addFullName(fullTree, 'typeName', ' / ');
|
formApi.updateSchema([
|
{
|
componentProps: {
|
fieldNames: {
|
label: 'typeName',
|
value: 'id'
|
},
|
getPopupContainer,
|
// 设置弹窗滚动高度 默认256
|
listHeight: 300,
|
showSearch: true,
|
treeData: fullTree,
|
disabled: typeDisabled.value,
|
treeDefaultExpandAll: false,
|
// 默认展开的树节点
|
treeDefaultExpandedKeys: [0],
|
treeLine: { showLeafIcon: false },
|
// 筛选的字段
|
treeNodeFilterProp: 'typeName',
|
treeNodeLabelProp: 'fullName'
|
},
|
fieldName: 'fixtureType'
|
}
|
]);
|
}
|
|
/**
|
* 用户的加载
|
*/
|
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: 'borrowUser'
|
}
|
]);
|
}
|
|
/**
|
* 初始化部门选择
|
*/
|
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: 'borrowDept'
|
}
|
]);
|
}
|
|
async function handleConfirm() {
|
try {
|
drawerApi.drawerLoading(true);
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
const data = cloneDeep(await formApi.getValues());
|
await (isUpdate.value ? updateFixture(data) : addFixture(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 />
|
</BasicDrawer>
|
</template>
|