Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
138 lines
3.5 KiB
Vue
138 lines
3.5 KiB
Vue
<script setup lang="ts">
|
|
import {computed, reactive, ref} from 'vue';
|
|
|
|
import {useVbenModal} from '@vben/common-ui';
|
|
import {$t} from '@vben/locales';
|
|
import {cloneDeep} from '@vben/utils';
|
|
|
|
import {useVbenForm} from '#/adapter/form';
|
|
import {personAdd, personInfo, personUpdate} from '#/api/property/resident/person';
|
|
import {defaultFormValueGetter, useBeforeCloseDiff} from '#/utils/popup';
|
|
|
|
import {modalSchema} from './data';
|
|
import QueryUserList from './query-user-list.vue'
|
|
import QueryUnitList from './query-unit-list.vue'
|
|
|
|
const emit = defineEmits<{ reload: [] }>();
|
|
|
|
const isUpdate = ref(false);
|
|
const title = computed(() => {
|
|
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
|
});
|
|
let userInfo = reactive({
|
|
userId: '',
|
|
userName: '',
|
|
phone: '',
|
|
gender: '',
|
|
});
|
|
let unitName = ref('');
|
|
const userId = ref<number | string>(0);
|
|
const unitId = ref<string>('');
|
|
const [BasicForm, formApi] = useVbenForm({
|
|
commonConfig: {
|
|
// 默认占满两列
|
|
formItemClass: 'col-span-1',
|
|
// 默认label宽度 px
|
|
labelWidth: 100,
|
|
// 通用配置项 会影响到所有表单项
|
|
componentProps: {
|
|
class: 'w-full',
|
|
}
|
|
},
|
|
schema: modalSchema(),
|
|
showDefaultActions: false,
|
|
wrapperClass: 'grid-cols-2',
|
|
});
|
|
|
|
const {onBeforeClose, markInitialized, resetInitialized} = useBeforeCloseDiff(
|
|
{
|
|
initializedGetter: defaultFormValueGetter(formApi),
|
|
currentGetter: defaultFormValueGetter(formApi),
|
|
},
|
|
);
|
|
|
|
const [BasicModal, modalApi] = useVbenModal({
|
|
// 在这里更改宽度
|
|
class: 'w-[70%]',
|
|
fullscreenButton: false,
|
|
onBeforeClose,
|
|
onClosed: handleClosed,
|
|
onConfirm: handleConfirm,
|
|
onOpenChange: async (isOpen) => {
|
|
if (!isOpen) {
|
|
return null;
|
|
}
|
|
modalApi.modalLoading(true);
|
|
|
|
const {id} = modalApi.getData() as { id?: number | string };
|
|
isUpdate.value = !!id;
|
|
|
|
if (isUpdate.value && id) {
|
|
const record = await personInfo(id);
|
|
userId.value = record.userId;
|
|
unitId.value = record.unitId.toString();
|
|
record.state=record.state?.toString()
|
|
await formApi.setValues(record);
|
|
}
|
|
await markInitialized();
|
|
|
|
modalApi.modalLoading(false);
|
|
},
|
|
});
|
|
|
|
async function handleConfirm() {
|
|
try {
|
|
modalApi.lock(true);
|
|
const {valid} = await formApi.validate();
|
|
if (!valid) {
|
|
return;
|
|
}
|
|
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
|
|
let data = cloneDeep(await formApi.getValues());
|
|
if (userInfo) {
|
|
data.userName = userInfo.userName
|
|
data.phone = userInfo.phone
|
|
data.gender = userInfo.gender
|
|
}
|
|
if(unitName.value){
|
|
data.unitName = unitName.value
|
|
}
|
|
await (isUpdate.value ? personUpdate(data) : personAdd(data));
|
|
resetInitialized();
|
|
emit('reload');
|
|
modalApi.close();
|
|
} catch (error) {
|
|
console.error(error);
|
|
} finally {
|
|
modalApi.lock(false);
|
|
}
|
|
}
|
|
|
|
async function handleClosed() {
|
|
await formApi.resetForm();
|
|
resetInitialized();
|
|
}
|
|
|
|
function getUserInfo(user: any) {
|
|
userInfo = user;
|
|
}
|
|
|
|
function getUnitInfo(unit: { name: string }) {
|
|
unitName.value = unit.name;
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<BasicModal :title="title">
|
|
<BasicForm>
|
|
<template #userId="slotProps">
|
|
<QueryUserList @update:userInfo="getUserInfo" v-bind="slotProps" :isUpdate="isUpdate" :userId="userId"/>
|
|
</template>
|
|
<template #unitId="slotProps">
|
|
<QueryUnitList @update:unitInfo="getUnitInfo" v-bind="slotProps" :isUpdate="isUpdate" :unitId="unitId"/>
|
|
</template>
|
|
</BasicForm>
|
|
</BasicModal>
|
|
</template>
|
|
|