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
<template>
  <wd-form ref="form" :model="model" class="relative form_box">
    <wd-cell-group :border="true">
      <wd-picker
        v-model="diskMaterialValue"
        :columns="diskMaterialOptions"
        label="盘具类型"
        label-width="100px"
        prop="diskMaterial"
        placeholder="请选择盘具类型"
        clearable
        @confirm="handleDiskMaterialChange"
      />
      <wd-input
        v-model="model.model"
        label="尺寸"
        label-width="100px"
        prop="model"
        clearable
        placeholder="请输入尺寸"
      />
      <wd-input
        v-model="model.amount"
        label="数量"
        label-width="100px"
        prop="amount"
        clearable
        placeholder="请输入数量"
      />
      <wd-picker
        v-model="model.unit"
        :columns="unitOptions"
        label="单位"
        label-width="100px"
        prop="unit"
        placeholder="请选择单位"
        clearable
        @confirm="handleUnitChange"
      />
      <wd-picker
        v-model="model.supplier"
        :columns="supplierOptions"
        label="厂家"
        label-width="100px"
        prop="supplier"
        placeholder="请选择厂家"
        clearable
        @confirm="handleSupplierChange"
      />
    </wd-cell-group>
    <wd-toast />
  </wd-form>
</template>
 
<script setup lang="ts">
import { onMounted, watch } from "vue";
import useFormData from "@/hooks/useFormData";
import TwistApi from "@/api/product/twist";
import ManageApi from "@/api/product/manage";
import { useToast } from "wot-design-uni";
 
const props = defineProps({
  mode: {
    type: String,
    default: "add",
  },
  editData: {
    type: Object,
    default: null,
  },
});
 
const emits = defineEmits(["refresh"]);
const paramsId = ref();
const toast = useToast();
const { form: model } = useFormData({
  diskMaterial: undefined, // 盘具类型
  model: undefined, // 尺寸
  amount: undefined, // 数量
  unit: "只", // 单位,默认值为"只"
  supplier: undefined,
  type: "盘具",
});
 
// 用于编辑时存储完整列表数据和当前编辑ID
const allListData = ref<any[]>([]);
const editId = ref<number>();
 
// 盘具类型字典数据
const diskMaterialOptions = ref<Array<{ label: string; value: string }>>([]);
const diskMaterialValue = ref("");
 
// 单位字典数据
const unitOptions = ref<Array<{ label: string; value: string }>>([]);
 
// 厂家字典数据
const supplierOptions = ref<Array<{ label: string; value: string }>>([]);
 
// 加载盘具类型字典数据
const loadDiskMaterialDict = async () => {
  try {
    const res = await ManageApi.dictAPI("disk_material");
    if (res.data && Array.isArray(res.data)) {
      diskMaterialOptions.value = res.data.map((item: any) => ({
        label: item.dictLabel || "",
        value: item.dictValue || "",
      }));
    }
  } catch (error) {
    console.error("加载盘具类型字典失败:", error);
  }
};
 
// 加载单位字典数据
const loadUnitDict = async () => {
  try {
    const res = await ManageApi.dictAPI("technical_weight_unit");
    if (res.data && Array.isArray(res.data)) {
      unitOptions.value = res.data.map((item: any) => ({
        label: item.dictLabel || "",
        value: item.dictValue || "",
      }));
      // 设置默认值为"只",如果字典中有"只"选项
      const defaultOption = unitOptions.value.find(
        (item) => item.label === "只" || item.value === "只"
      );
      if (defaultOption && !model.unit) {
        model.unit = defaultOption.value;
      }
    }
  } catch (error) {
    // 加载字典失败,静默处理
  }
};
 
// 加载厂家字典数据
const loadSupplierDict = async () => {
  try {
    const res = await ManageApi.dictAPI("factory");
    if (res.data && Array.isArray(res.data)) {
      supplierOptions.value = res.data.map((item: any) => ({
        label: item.dictLabel || "",
        value: item.dictValue || "",
      }));
    }
  } catch (error) {
    // 加载字典失败,静默处理
  }
};
 
// 处理盘具类型选择
const handleDiskMaterialChange = (val: any) => {
  model.diskMaterial = val.value;
};
 
// 处理单位选择
const handleUnitChange = (val: any) => {
  model.unit = val.value;
};
 
// 处理厂家选择
const handleSupplierChange = (val: any) => {
  model.supplier = val.value;
};
 
// 监听 model.diskMaterial 变化,同步选择器显示
watch(
  () => model.diskMaterial,
  (newValue) => {
    diskMaterialValue.value = newValue || "";
  },
  { immediate: true }
);
 
const submit = async () => {
  const { code } = await TwistApi.addStrandedWireDish([
    {
      wireId: paramsId.value,
      ...model,
    },
  ]);
  if (code == 200) {
    toast.success("新增成功");
    emits("refresh");
    return true;
  }
  return false;
};
 
// 编辑提交(也走新增接口,提交整个列表)
const submitEdit = async (list?: any[], id?: number) => {
  const currentList = list || allListData.value;
  const currentId = id || editId.value;
 
  if (!currentId) {
    toast.error("缺少记录ID");
    return false;
  }
 
  // 更新列表中对应的数据项
  const updatedList = currentList.map((item) => {
    if (item.id === currentId) {
      // 保留原有数据,然后更新修改的字段
      const updatedItem = {
        ...item, // 先保留原有的所有数据
        diskMaterial: model.diskMaterial,
        model: model.model,
        amount: model.amount,
        unit: model.unit,
        supplier: model.supplier,
        type: model.type,
      };
      return updatedItem;
    }
    return item;
  });
 
  // 提交整个列表
  const { code } = await TwistApi.addStrandedWireDish(updatedList);
 
  if (code == 200) {
    toast.success("更新成功");
    return true;
  }
  return false;
};
 
// 设置表单数据(用于编辑时回显)
const setFormData = (list: any[], currentEditId: number) => {
  // 安全检查:确保list是数组
  if (!Array.isArray(list)) {
    console.error("setFormData: list 参数不是数组", list);
    return;
  }
 
  // 存储完整列表数据
  allListData.value = list;
  editId.value = currentEditId;
 
  // 找到当前编辑项并回显到表单
  const currentItem = list.find((item) => item.id === currentEditId);
  if (currentItem) {
    model.diskMaterial = currentItem.diskMaterial;
    model.model = currentItem.model;
    model.amount = currentItem.amount;
    model.unit = currentItem.unit || "只";
    model.supplier = currentItem.supplier;
    model.type = currentItem.type || "盘具";
    // 设置盘具类型的回显值
    diskMaterialValue.value = currentItem.diskMaterial || "";
  }
};
 
onLoad((options: any) => {
  paramsId.value = options.id;
});
 
onMounted(async () => {
  await loadDiskMaterialDict();
  await loadUnitDict();
  await loadSupplierDict();
});
 
// 监听编辑数据变化,自动回显
watch(
  () => props.editData,
  (newData) => {
    if (newData && props.mode === "edit") {
      model.diskMaterial = newData.diskMaterial || "";
      model.model = newData.model || "";
      model.amount = newData.amount || "";
      model.unit = newData.unit || "只";
      model.supplier = newData.supplier || "";
      model.type = newData.type || "盘具";
      diskMaterialValue.value = newData.diskMaterial || "";
    }
  },
  { immediate: true, deep: true }
);
 
// 重置表单数据
const resetFormData = () => {
  model.diskMaterial = undefined;
  model.model = undefined;
  model.amount = undefined;
  model.unit = "只";
  model.supplier = undefined;
  model.type = "盘具";
  diskMaterialValue.value = "";
};
 
defineExpose({
  submit,
  submitEdit,
  setFormData,
  resetFormData,
});
</script>
<style lang="scss" scoped>
.submit_btn {
  position: absolute;
  bottom: 0;
  width: 100%;
}
</style>