<template>
|
<el-dialog v-model="centerDialogVisible" title="Warning" width="500" center>
|
<el-form
|
ref="ruleFormRef"
|
style="max-width: 600px"
|
:model="ruleForm"
|
:rules="rules"
|
label-width="auto"
|
>
|
<el-form-item label="名称" prop="name">
|
<el-input v-model="ruleForm.name" placeholder="请输入文档名称"/>
|
</el-form-item>
|
<el-form-item label="请输入文档类型" prop="type">
|
<el-select v-model="ruleForm.type" placeholder="请输入文档类型">
|
<el-option label="合同" value="合同" />
|
<el-option label="报告" value="报告" />
|
</el-select>
|
</el-form-item>
|
<el-form-item label="请输入文档状态" prop="status">
|
<el-select v-model="ruleForm.status" placeholder="请输入文档状态">
|
<el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value" />
|
</el-select>
|
</el-form-item>
|
</el-form>
|
<template #footer>
|
<div class="dialog-footer">
|
<el-button @click="centerDialogVisible = false">Cancel</el-button>
|
<el-button type="primary" @click="submit">
|
Confirm
|
</el-button>
|
</div>
|
</template>
|
</el-dialog>
|
</template>
|
|
<script setup>
|
import { ref, watch } from "vue";
|
import { addOrEditArchive } from "@/api/archiveManagement"
|
|
const centerDialogVisible = defineModel("centerDialogVisible", {
|
type: Boolean,
|
default: false,
|
});
|
|
const props = defineProps({
|
row: {
|
type: Object,
|
default: () => ({}),
|
},
|
});
|
const copyFormData = (data) => {
|
return JSON.parse(JSON.stringify(data));
|
};
|
// 初始化表单数据的辅助函数
|
const initFormData = (rowData) => {
|
if (rowData && rowData.id) {
|
return copyFormData(rowData);
|
}
|
return {
|
name: "",
|
type: "",
|
status: "",
|
};
|
};
|
|
// 初始化表单数据
|
const ruleFormRef = ref(null);
|
const ruleForm = ref(initFormData(props.row));
|
const copyForm = ref()
|
// 监听 row 的变化,更新 ruleForm
|
watch(() => props.row, (newRow) => {
|
copyForm.value = initFormData(newRow);
|
ruleForm.value = JSON.parse(JSON.stringify(copyForm.value));
|
}, { deep: true });
|
const rules = {
|
name: [
|
{ required: true, message: "Please input activity name", trigger: "blur" },
|
],
|
type: [
|
{ required: true, message: "Please select activity zone", trigger: "change" },
|
],
|
status: [
|
{ required: true, message: "Please select activity count", trigger: "change" },
|
],
|
};
|
|
const options = [
|
{ value: "有效", label: "有效" },
|
{ value: "无效", label: "无效" },
|
{ value: "作废", label: "作废" },
|
];
|
const emit = defineEmits(["submitForm"]);
|
const submit = async () => {
|
// 验证表单
|
if (!ruleFormRef.value) return;
|
|
try {
|
const valid = await ruleFormRef.value.validate();
|
if (!valid) {
|
return;
|
}
|
|
// 调用 API
|
let res = await addOrEditArchive(ruleForm.value);
|
console.log("API 响应:", res);
|
|
// 发送 emit 事件
|
emit("submitForm", res);
|
console.log("emit submitForm 已发送");
|
|
// 关闭对话框
|
centerDialogVisible.value = false;
|
} catch (error) {
|
console.error("表单验证失败或API调用失败:", error);
|
}
|
}
|
</script>
|
|
<style lang="less" scoped></style>
|