admin-vben5/apps/web-antd/src/views/property/clean/cleanOrders/clean-modal.vue

169 lines
4.9 KiB
Vue
Raw Normal View History

2025-06-19 14:26:08 +08:00
<script setup lang="ts">
import { computed, ref } from 'vue';
2025-06-23 16:50:37 +08:00
import dayjs from 'dayjs';
2025-06-19 14:26:08 +08:00
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { cloneDeep } from '@vben/utils';
import { useVbenForm } from '#/adapter/form';
import { cleanList } from '#/api/property/clean';
2025-06-23 16:50:37 +08:00
import type { CleanVO } from '#/api/property/clean/model';
import { clean_orderAdd, clean_orderInfo, clean_orderUpdate } from '#/api/property/clean_order';
2025-06-19 14:26:08 +08:00
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
2025-06-23 16:50:37 +08:00
import { resident_unitList } from '#/api/property/resident/unit';
2025-06-19 14:26:08 +08:00
2025-06-25 11:37:28 +08:00
import { modalSchema } from './data';
2025-06-19 14:26:08 +08:00
const emit = defineEmits<{ reload: [] }>();
2025-06-25 11:37:28 +08:00
2025-06-19 14:26:08 +08:00
const isUpdate = ref(false);
const title = computed(() => {
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
2025-06-23 16:50:37 +08:00
// 用来缓存 cleanList 的完整数据
let cleanListData: CleanVO[] = [];
// 在文件顶部加缓存
let unitListData: { id: any; name: string }[] = [];
const editUnitId = ref('');
const editCleanId = ref('');
2025-06-19 14:26:08 +08:00
const [BasicForm, formApi] = useVbenForm({
commonConfig: {
// 默认占满两列
2025-06-23 16:50:37 +08:00
formItemClass: 'col-span-1',
2025-06-19 14:26:08 +08:00
// 默认label宽度 px
2025-06-23 16:50:37 +08:00
labelWidth: 120,
2025-06-19 14:26:08 +08:00
// 通用配置项 会影响到所有表单项
componentProps: {
class: 'w-full',
}
},
2025-06-23 16:50:37 +08:00
// 1. 使用正确的属性名 handleValuesChange
handleValuesChange: async (values, fieldsChanged) => {
// 2. fieldsChanged 是一个包含变化字段名的数组
if (fieldsChanged.includes('name')) {
// 如果缓存数据为空,先请求一次并缓存
if (cleanListData.length === 0) {
try {
const res = await cleanList(); // 查询所有
cleanListData = res.rows || [];
} catch (e) {
console.error('获取劳务列表失败:', e);
cleanListData = []; // 出错时清空
}
}
// 3. 从 values 中获取当前选中的 id
const selectedId = values.name;
const selectedItem = cleanListData.find(item => item.id === selectedId);
if (selectedItem) {
// 4. 使用正确的 formApi.setValues 方法
await formApi.setValues({
prices: selectedItem.peices, // 服务单价
frequency: selectedItem.frequency, // 保洁频率
standard: selectedItem.standard, // 保洁内容
peices: selectedItem.peices, // 保洁标准
});
}
}
},
2025-06-19 14:26:08 +08:00
schema: modalSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
});
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
);
const [BasicModal, modalApi] = useVbenModal({
// 在这里更改宽度
2025-06-23 16:50:37 +08:00
class: 'w-[70%]',
2025-06-19 14:26:08 +08:00
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) {
2025-06-25 11:37:28 +08:00
const record = await clean_orderInfo(id);
2025-06-23 16:50:37 +08:00
if (record.starTime) record.starTime = dayjs(record.starTime);
if (record.endTime) record.endTime = dayjs(record.endTime);
editUnitId.value = record.unitId || '';
editCleanId.value = record.cleanId || '';
2025-06-19 14:26:08 +08:00
await formApi.setValues(record);
}
await markInitialized();
modalApi.modalLoading(false);
},
});
async function handleConfirm() {
try {
modalApi.lock(true);
const { valid } = await formApi.validate();
if (!valid) {
return;
}
const data = cloneDeep(await formApi.getValues());
2025-06-25 11:37:28 +08:00
console.log(data);
2025-06-23 16:50:37 +08:00
// 单位数据缓存
if (unitListData.length === 0) {
const res = await resident_unitList();
unitListData = res.rows || [];
}
// 劳务数据缓存 cleanListData 已有
2025-06-25 11:37:28 +08:00
2025-06-23 16:50:37 +08:00
// 查找label
const unitObj = unitListData.find(item => item.id === data.unit);
const cleanObj = cleanListData.find(item => item.id === data.name);
data.unit = unitObj ? unitObj.name : data.unit || '' ;
data.name = cleanObj ? cleanObj.name :data.name || '';
data.unitId = unitObj ? unitObj.id : (isUpdate.value ? editUnitId.value : '');
data.cleanId = cleanObj ? cleanObj.id : (isUpdate.value ? editCleanId.value : '');
// 如不需要原字段可删除
// delete data.unit;
// delete data.name;
await (isUpdate.value ? clean_orderUpdate(data) : clean_orderAdd(data));
2025-06-19 14:26:08 +08:00
resetInitialized();
emit('reload');
modalApi.close();
} catch (error) {
console.error(error);
} finally {
modalApi.lock(false);
}
}
async function handleClosed() {
2025-06-25 11:17:58 +08:00
detailTable.value = []
2025-06-19 14:26:08 +08:00
await formApi.resetForm();
resetInitialized();
}
</script>
<template>
2025-06-25 11:37:28 +08:00
<BasicModal :title="title">
2025-06-19 14:26:08 +08:00
<BasicForm />
</BasicModal>
</template>