刘磊
2025-08-25 4c308eab6106324bd40e6ad7fd9e769a5cedcedf
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
<template>
  <vol-box v-model="show" title="组盘" :width="800" :height="600">
    <template #content>
      <el-form :model="formData" label-width="100px">
        <el-form-item label="条码" style="width: 50%">
          <el-input type="text" v-model="palletCode" placeholder="请扫码条码"> </el-input>
        </el-form-item>
        
        <!-- 遍历物料列表 -->
        <el-form-item v-for="(item, index) in formData.items" :key="index" style="width: 70%">
          <el-select 
            size="mini" 
            clearable  
            filterable
            v-model="item.MaterilName" 
            placeholder="请选择物料"
            style="width: 45%; margin-right: 5%"
          >
            <!-- 避免使用与循环变量相同的item名称 -->
            <el-option 
              v-for="material in GetMatetils()" 
              :value="material.value" 
              :label="material.text"
              :key="material.value"
            ></el-option>
          </el-select>
          
          <!-- 数量应该是数字类型 -->
          <el-input 
            v-model="item.num" 
            placeholder="请输入数量" 
            style="width: 35%; margin-right: 5%"
            type="number"  
          ></el-input>
          
          <el-button v-if="index > 0" type="danger" @click="removeItem(index)">X</el-button>
        </el-form-item>
      </el-form>
      
      <el-button type="primary" @click="addItem" style="margin-top: 10px;">
        添加物料
      </el-button>
    </template>
 
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="show = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </span>
    </template>
  </vol-box>
</template>
 
<script>
import VolBox from "@/components/basic/VolBox.vue";
export default {
  components: {
    "vol-box": VolBox,
  },
  data() {
    return {
      formData: {
        // 每个item应该是一个包含MaterilName和num的对象
        items: [{ MaterilName: '', num: '' }],
      },
      palletCode: "",
      show: false,
      MatetilList: [] // 假设这里会被填充物料数据
    };
  },
  methods: {
    addItem() {
      // 保持与初始数据结构一致,添加一个对象而不是数组
      this.formData.items.push({ MaterilName: '', num: '' });
    },
    removeItem(index) {
      this.formData.items.splice(index, 1);
    },
 
    // 获取物料信息
    GetMatetils() {
      // 简化代码,直接返回列表
      return this.MatetilList;
    },
 
    submitForm() {
      // 验证条码
      if (!this.palletCode.trim()) {
        this.$message.error("请扫码");
        return;
      }
 
      // 验证至少有一个物料且数量有效
      const hasValidItem = this.formData.items.some(item => 
        item.MaterilName && item.num && Number(item.num) > 0
      );
      
      if (!hasValidItem) {
        this.$message.error("请添加有效的物料和数量");
        return;
      }
 
      var params = {
        MainData: {
          jsonData: JSON.parse(JSON.stringify(this.formData.items)),
          palletCode: this.palletCode,
        }
      };
 
      this.http.post("/api/BoxingInfo/AddBoxingInfo", params, true).then((x) => {
        if (!x.status) {
          this.$message.error(x.message);
        } else {
          this.$message.success("操作成功");
          // 重置表单
          this.palletCode = '';
          this.formData.items = [{ MaterilName: '', num: '' }];
          this.show = false;
          // 通知父组件刷新数据
          if (window.$vue && $vue.refresh) {
            $vue.refresh();
          }
        }
      }).catch(error => {
        this.$message.error("提交失败:" + (error.message || "网络错误"));
      });
    },
  },
};
</script>
 
<style scoped>
.container {
  font-family: 'Inter', system-ui, sans-serif;
}
</style>