gaoluyang
19 小时以前 19aa78a505d468739a61054f07d0840477192b2d
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
<template>
  <div>
    <el-dialog
        v-model="isShow"
        :title="isEdit ? '编辑工艺路线' : '创建工艺路线'"
        width="900px"
        @close="closeModal"
    >
      <el-form label-width="140px" :model="formState" label-position="top" ref="formRef">
        <el-form-item label="工艺路线编号">
          <el-input v-model="formState.processRouteCode" placeholder="请输入,忽略将自动生成" clearable />
        </el-form-item>
 
        <el-form-item
            label="工艺路线名称"
            prop="processRouteName"
            :rules="[
                {
                required: true,
                message: '请输入工艺路线名称',
                trigger: 'blur',
              }
            ]"
        >
          <el-input v-model="formState.processRouteName" placeholder="请填写" clearable />
        </el-form-item>
 
        <div class="section-title">工序列表</div>
        
        <div class="table-actions">
          <el-button type="primary" link @click="addRow">
            <el-icon><Plus /></el-icon> 添加一行
          </el-button>
        </div>
 
        <el-table
          ref="tableRef"
          :data="formState.processRouteItems"
          border
          style="width: 100%"
          class="process-table"
          row-key="tempId"
        >
          <el-table-column label="拖拽" width="60" align="center">
            <template #default>
              <el-icon class="drag-handle"><Rank /></el-icon>
            </template>
          </el-table-column>
          <el-table-column label="工序" prop="processId" min-width="200">
            <template #header>
              <span class="required">工序</span>
            </template>
            <template #default="scope">
              <el-select 
                v-model="scope.row.processId" 
                placeholder="请选择" 
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="item in processOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="是否质检" prop="isQuality" width="100" align="center">
            <template #default="scope">
              <el-switch v-model="scope.row.isQuality" />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="80" align="center" fixed="right">
            <template #default="scope">
              <el-button type="danger" link @click="deleteRow(scope.$index)">
                <el-icon><Delete /></el-icon>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
 
        <el-empty v-if="formState.processRouteItems.length === 0" description="暂无数据" />
      </el-form>
 
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确认</el-button>
          <el-button @click="closeModal">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import {ref, computed, getCurrentInstance, onMounted, nextTick, watch, onUnmounted} from "vue";
import {add, update} from "@/api/productionManagement/processRoute.js";
import {findProcessRouteItemList} from "@/api/productionManagement/processRouteItem.js";
import {processList} from "@/api/productionManagement/productionProcess.js";
import {Plus, Delete, Rank} from '@element-plus/icons-vue';
import Sortable from 'sortablejs';
 
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  }
});
 
const emit = defineEmits(['update:visible', 'completed']);
 
const isEdit = computed(() => {
  return formState.value && formState.value.id;
});
 
const isShow = computed({
  get() {
    return props.visible;
  },
  set(val) {
    emit('update:visible', val);
  },
});
 
const processOptions = ref([]);
 
let { proxy } = getCurrentInstance();
 
const tableRef = ref(null);
let sortable = null;
 
let tempIdCounter = 0;
 
const formState = ref({
  id: undefined,
  processRouteCode: '',
  processRouteName: '',
  processRouteItems: [],
});
 
const initSortable = () => {
  if (sortable) {
    sortable.destroy();
    sortable = null;
  }
  
  nextTick(() => {
    if (tableRef.value) {
      const table = tableRef.value.$el.querySelector('.el-table__body-wrapper tbody');
      if (table) {
        sortable = Sortable.create(table, {
          animation: 150,
          handle: '.drag-handle',
          ghostClass: 'sortable-ghost',
          onEnd: (evt) => {
            const { oldIndex, newIndex } = evt;
            if (oldIndex !== undefined && newIndex !== undefined && oldIndex !== newIndex) {
              const item = formState.value.processRouteItems.splice(oldIndex, 1)[0];
              formState.value.processRouteItems.splice(newIndex, 0, item);
            }
          }
        });
      }
    }
  });
};
 
const getProcessList = () => {
  processList({}).then(res => {
    processOptions.value = res.data || [];
  }).catch(err => {
    console.error("获取工序列表失败:", err);
  });
};
 
const closeModal = () => {
  formState.value = {
    id: undefined,
    processRouteCode: '',
    processRouteName: '',
    processRouteItems: [],
  };
  isShow.value = false;
};
 
const setFormData = async () => {
  if (isEdit.value) {
    formState.value = {
      id: props.record.id,
      processRouteCode: props.record.processRouteCode || '',
      processRouteName: props.record.processRouteName || '',
      processRouteItems: (props.record.processRouteItems || []).map((item, index) => ({
        tempId: item.id || `temp_${tempIdCounter++}`,
        processId: item.processId,
        id: item.id,
        isQuality: item.isQuality !== undefined ? item.isQuality : false,
        dragSort: index + 1,
      })),
    };
  } else {
    formState.value = {
      id: undefined,
      processRouteCode: '',
      processRouteName: '',
      processRouteItems: [],
    };
  }
}
 
const addRow = () => {
  formState.value.processRouteItems.push({
    tempId: `temp_${tempIdCounter++}`,
    processId: undefined,
    isQuality: false,
  });
  nextTick(() => {
    initSortable();
  });
};
 
const deleteRow = (index) => {
  formState.value.processRouteItems.splice(index, 1);
  nextTick(() => {
    initSortable();
  });
};
 
const handleSubmit = () => {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (formState.value.processRouteItems.length === 0) {
        proxy.$modal.msgError("请至少添加一个工序");
        return;
      }
      
      for (let i = 0; i < formState.value.processRouteItems.length; i++) {
        const row = formState.value.processRouteItems[i];
        if (!row.processId) {
          proxy.$modal.msgError(`第${i + 1}行:请选择工序`);
          return;
        }
      }
 
      const submitData = {
        id: formState.value.id,
        processRouteCode: formState.value.processRouteCode,
        processRouteName: formState.value.processRouteName,
        processRouteItems: formState.value.processRouteItems.map((item, index) => ({
          id: item.id,
          routeId: formState.value.id,
          processId: item.processId,
          isQuality: item.isQuality,
          dragSort: index + 1,
        })),
      };
 
      const apiCall = isEdit.value ? update(submitData) : add(submitData);
 
      apiCall.then(res => {
        isShow.value = false;
        emit('completed');
        proxy.$modal.msgSuccess(isEdit.value ? "编辑成功" : "新增成功");
      });
    }
  });
};
 
// 监听 visible 变化
watch(() => props.visible, (visible) => {
  if (visible) {
    nextTick(() => {
      initSortable();
    });
  }
});
 
const setData = async (row) => {
  if (row) {
    formState.value = {
      id: row.id,
      processRouteCode: row.processRouteCode || '',
      processRouteName: row.processRouteName || '',
      processRouteItems: [],
    };
    
    const res = await findProcessRouteItemList({ routeId: row.id });
    if (res.data && Array.isArray(res.data)) {
      formState.value.processRouteItems = res.data.map((item, index) => ({
        tempId: item.id || `temp_${tempIdCounter++}`,
        processId: item.processId,
        id: item.id,
        isQuality: item.isQuality !== undefined ? item.isQuality : false,
        dragSort: index + 1,
      }));
    }
    
    nextTick(() => {
      initSortable();
    });
  } else {
    formState.value = {
      id: undefined,
      processRouteCode: '',
      processRouteName: '',
      processRouteItems: [],
    };
    
    nextTick(() => {
      initSortable();
    });
  }
};
 
onMounted(() => {
  getProcessList();
});
 
onUnmounted(() => {
  if (sortable) {
    sortable.destroy();
    sortable = null;
  }
});
 
defineExpose({
  closeModal,
  handleSubmit,
  isShow,
  setData,
});
</script>
 
<style scoped>
.section-title {
  font-size: 14px;
  font-weight: bold;
  margin: 20px 0 10px 0;
  color: #333;
}
 
.table-actions {
  display: flex;
  gap: 16px;
  margin-bottom: 10px;
}
 
.process-table {
  margin-bottom: 20px;
}
 
.required::before {
  content: '*';
  color: #f56c6c;
  margin-right: 4px;
}
 
:deep(.el-dialog__body) {
  padding-top: 10px;
}
 
.sortable-ghost {
  opacity: 0.4;
  background-color: #f5f7fa;
}
 
.drag-handle {
  cursor: move;
  font-size: 18px;
  color: #909399;
}
 
.drag-handle:hover {
  color: #409eff;
}
</style>