ZN
5 天以前 e756e3cc5ddd0ddb42c5f00d6bb3eee76ba73e6f
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
<template>
  <el-dialog
    :title="title"
    v-model="visible"
    width="1000px"
    append-to-body
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="0">
      <!-- 顶部基础信息 -->
      <div class="base-info-row">
        <div class="info-item">
          <span class="item-label required">名称</span>
          <el-input
            v-model="form.name"
            placeholder="请输入名称"
            maxlength="10"
            show-word-limit
            style="width: 220px"
          />
        </div>
        <div class="info-item">
          <span class="item-label">备注</span>
          <el-input
            v-model="form.description"
            placeholder="请输入备注"
            maxlength="20"
            show-word-limit
            style="width: 220px"
          />
        </div>
        <div class="info-item">
          <span class="item-label">附件</span>
          <el-upload
            v-if="isEdit"
            :action="uploadUrl"
            :headers="uploadHeaders"
            :on-success="handleUploadSuccess"
            :on-remove="handleRemove"
            v-model:file-list="uploadFileList"
            :limit="3"
            name="files"
            multiple
          >
            <el-button type="primary">上传附件</el-button>
          </el-upload>
          <span v-else class="text-gray-400 text-sm">请先保存后再上传附件</span>
        </div>
      </div>
 
      <!-- 步骤配置表格 -->
       <p class="top-tip">请按照顺序配置项目阶段,拖拽<b>步骤</b>排序即可</p>
      <div class="step-table-container">
        <el-table 
          :data="form.savePlanNodeList" 
          border 
          style="width: 100%" 
          row-key="id"
          class="drag-table"
        >
          <el-table-column label="步骤" width="80" align="center" class-name="drag-handle">
            <template #default="scope">
              <div class="step-index" style="cursor: move;">
                {{ scope.$index + 1 }}
              </div>
            </template>
          </el-table-column>
          
          <el-table-column label="阶段名称" min-width="150">
            <template #header>
              <span class="required-star">*</span> 阶段名称
            </template>
            <template #default="scope">
              <el-form-item
                :prop="'savePlanNodeList.' + scope.$index + '.name'"
                :rules="[{ required: true, message: '请输入阶段名称', trigger: 'blur' }]"
              >
                <el-input v-model="scope.row.name" placeholder="请输入" />
              </el-form-item>
            </template>
          </el-table-column>
 
          <el-table-column label="负责人" width="180">
            <template #header>
              <span class="required-star">*</span> 负责人
            </template>
            <template #default="scope">
              <el-form-item
                :prop="'savePlanNodeList.' + scope.$index + '.leaderId'"
                :rules="[{ required: true, message: '请选择负责人', trigger: 'change' }]"
              >
                <el-select
                  v-model="scope.row.leaderId"
                  placeholder="测试"
                  @change="(val) => handleLeaderChange(val, scope.row)"
                >
                  <el-option
                    v-for="item in userOptions"
                    :key="item.userId"
                    :label="item.nickName"
                    :value="item.userId"
                  />
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
 
          <el-table-column label="预计工期 (天)" width="150">
            <template #header>
              预计工期 (天)
              <el-tooltip content="完成该阶段预计需要的天数" placement="top">
                <el-icon class="info-icon"><QuestionFilled /></el-icon>
              </el-tooltip>
            </template>
            <template #default="scope">
              <el-input-number
                v-model="scope.row.estimatedDuration"
                :min="0"
                controls-position="right"
                style="width: 100%"
              />
            </template>
          </el-table-column>
 
          <el-table-column label="工时单价" width="120">
            <template #default="scope">
              <el-input v-model="scope.row.hourlyRate" placeholder="请输入" />
            </template>
          </el-table-column>
 
          <el-table-column label="作业内容" min-width="150">
            <template #default="scope">
              <el-input v-model="scope.row.workContent" placeholder="请输入" />
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="150">
            <template #default="scope">
              <el-button type="danger" size="mini" @click="removeStep(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
 
        <div class="add-row-btn" @click="addStep">
          <el-icon><Plus /></el-icon> 新增一行
        </div>
      </div>
    </el-form>
 
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>
 
<script setup>
import { ref, watch, onMounted, nextTick } from 'vue';
import { Plus, QuestionFilled } from '@element-plus/icons-vue';
import { userListNoPageByTenantId } from '@/api/system/user';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getToken } from '@/utils/auth';
import Sortable from 'sortablejs';
 
const props = defineProps({
  modelValue: Boolean,
  title: String,
  data: Object
});
 
const emit = defineEmits(['update:modelValue', 'submit']);
 
const visible = ref(false);
const formRef = ref(null);
const userOptions = ref([]);
const isEdit = ref(false);
const uploadHeaders = { Authorization: "Bearer " + getToken() };
// 上传地址
const uploadUrl = import.meta.env.VITE_APP_BASE_API + "/basic/customer-follow/upload";
let sortable = null;
 
const form = ref({
  id: undefined,
  name: '',
  description: '',
  attachmentIds: [],
  savePlanNodeList: []
});
const uploadFileList = ref([]);
 
const rules = {
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
};
 
// 监听弹窗显示/隐藏
watch(() => props.modelValue, (val) => {
  visible.value = val;
  if (val) {
    if (props.data) {
      // 编辑模式 - 回显数据
      isEdit.value = true;
      form.value = {
        id: props.data.id,
        name: props.data.name,
        description: props.data.description,
        attachmentIds: Array.isArray(props.data.attachmentIds)
          ? props.data.attachmentIds
          : (props.data.attachmentList || []).map(f => f.id).filter(Boolean),
        savePlanNodeList: []
      };
      
      // 回显步骤节点
      if (props.data.planNodeList && props.data.planNodeList.length > 0) {
        form.value.savePlanNodeList = props.data.planNodeList.map(node => ({
          id: node.id,
          projectManagementPlanId: node.projectManagementPlanId,
          sort: node.sort,
          name: node.name,
          leaderId: node.leaderId,
          leaderName: node.leaderName,
          estimatedDuration: node.estimatedDuration,
          hourlyRate: node.hourlyRate,
          workContent: node.workContent
        }));
      } else {
        form.value.savePlanNodeList = [createDefaultNode()];
      }
    } else {
      // 新增模式
      isEdit.value = false;
      resetForm();
    }
    // 初始化拖拽
    nextTick(() => {
      initSortable();
    });
  }
});
 
watch(visible, (val) => {
  emit('update:modelValue', val);
});
 
/** 初始化拖拽 */
function initSortable() {
  const el = document.querySelector('.drag-table .el-table__body-wrapper tbody');
  if (!el) return;
  
  if (sortable) {
    sortable.destroy();
  }
 
  sortable = Sortable.create(el, {
    handle: '.drag-handle',
    animation: 150,
    onEnd: ({ newIndex, oldIndex }) => {
      const targetRow = form.value.savePlanNodeList.splice(oldIndex, 1)[0];
      form.value.savePlanNodeList.splice(newIndex, 0, targetRow);
    }
  });
}
 
/** 创建默认节点对象 */
function createDefaultNode() {
  return {
    name: '',
    leaderId: null,
    leaderName: null,
    estimatedDuration: null,
    hourlyRate: null,
    workContent: null
  };
}
 
/** 重置表单 */
function resetForm() {
  form.value = {
    id: undefined,
    name: '',
    description: '',
    attachmentIds: [],
    savePlanNodeList: [createDefaultNode()]
  };
  uploadFileList.value = [];
  if (formRef.value) {
    formRef.value.resetFields();
  }
}
 
/** 获取用户列表 */
async function getUserList() {
  try {
    const res = await userListNoPageByTenantId();
    if (res.code === 200) {
      userOptions.value = res.data || [];
    }
  } catch (error) {
    console.error('获取用户列表失败:', error);
  }
}
 
/** 处理负责人变化 */
function handleLeaderChange(val, row) {
  const user = userOptions.value.find(u => u.userId === val);
  if (user) {
    row.leaderName = user.nickName;
  }
}
 
/** 处理文件上传成功 */
function handleUploadSuccess(response, file, fileList) {
  if (response.code === 200) {
    const newFile = response.data;
    const list = Array.isArray(newFile) ? newFile : [newFile];
    list.forEach(element => {
      const id = element?.id;
      if (id && !form.value.attachmentIds.includes(id)) {
        form.value.attachmentIds.push(id);
      }
    });
  } else {
    ElMessage.error(response.msg || '上传失败');
  }
}
 
/** 处理文件移除 */
function handleRemove(file) {
  const removedId = file?.id || file?.response?.data?.id;
  if (!removedId) return;
  form.value.attachmentIds = form.value.attachmentIds.filter(id => id !== removedId);
}
 
/** 添加步骤 */
function addStep() {
  form.value.savePlanNodeList.push(createDefaultNode());
}
 
/** 移除步骤 */
function removeStep(index) {
  if (form.value.savePlanNodeList.length <= 1) {
    ElMessage.warning('至少保留一个步骤');
    return;
  }
  
  ElMessageBox.confirm('是否确认删除该步骤?', '系统提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    form.value.savePlanNodeList.splice(index, 1);
  }).catch(() => {});
}
 
/** 移动步骤 */
function moveStep(index, direction) {
  const targetIndex = index + direction;
  if (targetIndex < 0 || targetIndex >= form.value.savePlanNodeList.length) return;
  
  const list = form.value.savePlanNodeList;
  const temp = list[index];
  list[index] = list[targetIndex];
  list[targetIndex] = temp;
}
 
/** 提交表单 */
async function submitForm() {
  if (!formRef.value) return;
  
  try {
    const valid = await formRef.value.validate();
    if (valid) {
      // 提交前自动填充 sort 字段,按当前数组顺序排序
      form.value.savePlanNodeList.forEach((node, index) => {
        node.sort = index;
      });
      emit('submit', form.value);
    }
  } catch (error) {
    console.error('表单验证失败:', error);
  }
}
 
/** 关闭弹窗 */
function handleClose() {
  resetForm();
}
 
onMounted(() => {
  getUserList();
});
</script>
 
<style scoped lang="scss">
.base-info-row {
  display: flex;
  gap: 40px;
  margin-bottom: 25px;
  padding: 0 10px;
 
  .info-item {
    display: flex;
    align-items: center;
    gap: 12px;
 
    .item-label {
      font-size: 14px;
      color: #606266;
      white-space: nowrap;
 
      &.required::before {
        content: '*';
        color: #f56c6c;
        margin-right: 4px;
      }
    }
  }
}
 
.step-table-container {
  padding: 0 10px;
 
  :deep(.el-form-item) {
    margin-bottom: 0;
  }
 
  .required-star {
    color: #f56c6c;
    margin-right: 4px;
  }
 
  .info-icon {
    font-size: 14px;
    color: #909399;
    margin-left: 4px;
    cursor: pointer;
  }
 
  .add-row-btn {
    margin-top: 15px;
    height: 40px;
    border: 1px dashed #dcdfe6;
    border-radius: 4px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #409eff;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.3s;
 
    &:hover {
      border-color: #409eff;
      background-color: #f0f7ff;
    }
  }
}
 
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  padding-top: 10px;
}
.top-tip {
  
  font-size: 14px;
  color: #606266;
  margin:0 0 10px 10px;
}
</style>