gaoluyang
2 天以前 092f67b26c5ab06a479341f5af80ea8e1642d43e
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<template>
  <div class="app-container">
    <!-- 顶部搜索和操作栏 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="managerName">
        <el-input
          v-model="queryParams.managerName"
          placeholder="请输入负责人姓名"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="项目状态"
          clearable
          style="width: 150px"
        >
          <el-option label="规划中" value="planning" />
          <el-option label="进行中" value="inProgress" />
          <el-option label="已完成" value="completed" />
          <el-option label="已暂停" value="paused" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
 
    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['oaSystem:project:add']"
        >新增项目</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['oaSystem:project:edit']"
        >编辑项目</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['oaSystem:project:remove']"
        >删除项目</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['oaSystem:project:export']"
        >导出项目</el-button>
      </el-col>
    </el-row>
 
    <!-- 项目列表表格 -->
    <el-table
      v-loading="loading"
      :data="projectList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="项目编号"
        align="center"
        prop="projectId"
        width="100"
      />
      <el-table-column
        label="项目名称"
        align="center"
        prop="projectName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="负责人"
        align="center"
        prop="managerName"
      />
      <el-table-column
        label="开始日期"
        align="center"
        prop="startDate"
        width="120"
      />
      <el-table-column
        label="结束日期"
        align="center"
        prop="endDate"
        width="120"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        width="90"
      >
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="完成度"
        align="center"
        prop="completionRate"
        width="100"
      >
        <template #default="scope">
          <el-progress :percentage="scope.row.completionRate" :stroke-width="6" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="180"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Search"
            @click="handleView(scope.row)"
            v-hasPermi="['oaSystem:project:query']"
          >详情</el-button>
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['oaSystem:project:edit']"
          >编辑</el-button>
          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oaSystem:project:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
 
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
 
    <!-- 项目表单对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <project-form
        ref="projectFormRef"
        :form="form"
        :rules="rules"
        :visible="open"
      />
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import Pagination from '@/components/Pagination';
import ProjectForm from './components/projectForm.vue';
import { useRouter } from 'vue-router';
const { proxy } = getCurrentInstance();
// 导入项目管理API接口
import { listProject, addProject, updateProject, delProject, exportProject } from '@/api/oaSystem/projectManagement';
// import { listUser } from '@/api/system/user'; // 导入用户列表API接口
 
// 创建router实例
const router = useRouter();
 
// 表格数据
const projectList = ref([]);
const loading = ref(true);
const total = ref(0);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  projectName: '',
  managerName: '',
  status: ''
});
 
// 表单数据
const form = reactive({
  projectId: undefined,
  projectName: '',
  description: '',
  startDate: '',
  endDate: '',
  managerId: '',
  managerName: '',
  status: 'planning',
  completionRate: 0
});
 
// 表单校验规则
const rules = {
  projectName: [
    { required: true, message: '项目名称不能为空', trigger: 'blur' },
    { min: 2, max: 50, message: '项目名称长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  startDate: [
    { required: true, message: '开始日期不能为空', trigger: 'change' }
  ],
  endDate: [
    { required: true, message: '结束日期不能为空', trigger: 'change' }
  ],
  managerId: [
    { required: true, message: '负责人不能为空', trigger: 'blur' }
  ]
};
 
// 对话框状态
const open = ref(false);
const title = ref('');
const projectFormRef = ref();
const queryRef = ref();
 
// 选中状态
const multiple = computed(() => {
  return selectedRowKeys.value.length === 0;
});
const single = computed(() => {
  return selectedRowKeys.value.length !== 1;
});
const selectedRowKeys = ref([]);
 
// 获取项目列表
const getList = async () => {
  loading.value = true;
    try {
    const { data } = await listProject(queryParams);
    projectList.value = data.records;
    total.value = data.total;
  } catch (error) {
    ElMessage.error('获取项目列表失败');
    console.error('获取项目列表失败:', error);
  } finally {
    loading.value = false;
  }
};
 
// 搜索
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};
 
// 重置
const resetQuery = () => {
  if (queryRef.value) {
    queryRef.value.resetFields();
  }
  handleQuery();
};
 
// 选中行变化
const handleSelectionChange = (selection) => {
  selectedRowKeys.value = selection.map(item => item.projectId);
};
 
// 新增项目 
const handleAdd = () => {
  resetForm();
  open.value = true;
  title.value = '新增项目';
};
 
// 编辑项目 
const handleUpdate = async (row) => {
  resetForm();
  const projectId = row.projectId || selectedRowKeys.value[0];
    try {
    // const { data } = await getProject(projectId);
    Object.assign(form, row);
    open.value = true;
    title.value = '编辑项目';
  } catch (error) {
    ElMessage.error('获取项目详情失败');
    console.error('获取项目详情失败:', error);
  }
};
 
// 删除项目
const handleDelete = async (row) => {
  // const projectIds = row.projectId ? [row.projectId] : selectedRowKeys.value;
  const projectNames = row.projectName ? [row.projectName] : 
    projectList.value.filter(item => projectIds.includes(item.projectId)).map(item => item.projectName);
  
  const confirmMessage = `确定要删除项目 "${projectNames.join('、')}" 吗?`;
  await ElMessageBox.confirm(confirmMessage, '确认操作', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).catch(() => {
    throw new Error('取消删除');
  });
  
  try {
    // if (projectIds.length === 1) {
      await delProject(row.projectId);
    // } else {
    //   await delProjectBatch(projectIds);
    // }
    ElMessage.success('删除成功');
    getList();
  } catch (error) {
    if (error.message !== '取消删除') {
      ElMessage.error('删除失败');
      console.error('删除项目失败:', error);
    }
  }
  // try {
  //   await ElMessageBox.confirm(confirmMessage, '确认操作', {
  //     confirmButtonText: '确定',
  //     cancelButtonText: '取消',
  //     type: 'warning'
  //   });
    
  //   // 模拟网络延迟
  //   await new Promise(resolve => setTimeout(resolve, 300));
   
    
  //   ElMessage.success('删除成功');
  //   getList();
  // } catch (error) {
  //   if (error !== 'cancel') {
  //     console.error('删除项目失败:', error);
  //   }
  // }
};
 
// 查看项目详情
const handleView = (row) => {
  const projectId = row.projectId;
  // 跳转到项目详情页面
  router.push({
    path: `/oaSystem/projectManagement/projectDetail/${projectId}`,
    query: { projectName: row.projectName }
  });
};
 
// 导出项目
const handleExport = async () => {
    let ids = [];
    if (selectedRowKeys.value.length > 0) {
      ids = selectedRowKeys.value; // 导出选中的项目
    } else {
      ids = projectList.value.map(item => item.projectId); // 导出所有项目
    }
  ElMessageBox.confirm("选中的内容将被导出,是否确认导出?", "导出", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.download(`/oA/project/export/${ids.join(',')}`, {}, "项目数据.xlsx");
      ElMessage.success("导出成功");
      ids = [];
    })
    .catch(() => {
      proxy.$modal.msg("已取消");
    });
};
// 提交表单
const submitForm = async () => {
  try {
    await projectFormRef.value.validate();
 
    if (form.projectId) {
      await updateProject(form);
      ElMessage.success('修改项目成功');
    } else {
      console.log("form",form);
      await addProject(form);
      ElMessage.success('新增项目成功');
    }
    open.value = false;
    getList();
  } catch (error) {
    console.error('提交表单失败:', error);
  }
};
 
// 取消
const cancel = () => {
  open.value = false;
  resetForm();
};
 
// 重置表单
const resetForm = () => {
  form.projectId = undefined;
  form.projectName = '';
  form.description = '';
  form.startDate = '';
  form.endDate = '';
  form.managerId = '';
  form.managerName = '';
  form.status = 'planning';
  form.completionRate = 0;
  if (projectFormRef.value) {
    projectFormRef.value.resetFields();
  }
};
 
// 获取状态标签类型
const getStatusType = (status) => {
  const statusTypeMap = {
    planning: 'info',
    inProgress: 'primary',
    completed: 'success',
    paused: 'warning'
  };
  return statusTypeMap[status] || 'default';
};
 
// 获取状态文本
const getStatusText = (status) => {
  const statusTextMap = {
    planning: '规划中',
    inProgress: '进行中',
    completed: '已完成',
    paused: '已暂停'
  };
  return statusTextMap[status] || status;
};
 
// 初始化
onMounted(() => {
  getList();
});
</script>
 
<style scoped>
.app-container {
  padding: 20px;
}
</style>