张诺
2 天以前 437a70bd86d8adda3da41853b8b1c2706a22fdd7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<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>