张诺
23 小时以前 7619d415522ab3dc3299d6a2a9f5c9964a692d3f
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
<template>
  <el-table :data="tableData" :border="border" style="width: 100%">    
    <el-table-column label="煤种" min-width="120">
      <template #default="{ row, $index }">
        <el-select 
          clearable 
          :model-value="getCoalNameById(row.coal) || row.coal"
          placeholder="请选择煤种"
          @change="(value) => handleCoalSelectChange(row, value)"
          filterable
          :key="`coal-select-${$index}-${weekList.length}`"
        >
          <el-option
            v-for="(item, index) of weekList"
            :key="`option-${index}-${item.key}`"
            :label="item.value"
            :value="item.value"
          />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column label="生产数量" min-width="120">
      <template #default="{ row, $index }">
        <el-input
          v-model="row.productionQuantity"
          placeholder="请输入生产数量"
          type="number"
          @input="handleInput('productionQuantity', $index, $event)"
        />
      </template>
    </el-table-column>
 
    <el-table-column label="人工成本" min-width="120">
      <template #default="{ row, $index }">
        <el-input
          v-model="row.laborCost"
          placeholder="请输入人工成本"
          type="number"
          @input="handleInput('laborCost', $index, $event)"
        >
          <template #suffix>
            <i style="font-style: normal">元</i>
          </template>
        </el-input>
      </template>
    </el-table-column>
 
    <el-table-column label="能耗成本" min-width="120">
      <template #default="{ row, $index }">
        <el-input
          v-model="row.energyConsumptionCost"
          placeholder="请输入能耗成本"
          type="number"
          @input="handleInput('energyConsumptionCost', $index, $event)"
        >
          <template #suffix>
            <i style="font-style: normal">元</i>
          </template>
        </el-input>
      </template>
    </el-table-column>
 
    <el-table-column label="设备折旧" min-width="120">
      <template #default="{ row, $index }">
        <el-input
          v-model="row.equipmentDepreciation"
          placeholder="请输入设备折旧"
          type="number"
          @input="handleInput('equipmentDepreciation', $index, $event)"
        >
          <template #suffix>
            <i style="font-style: normal">元</i>
          </template>
        </el-input>
      </template>
    </el-table-column>
 
    <el-table-column label="采购单价" min-width="120">
      <template #default="{ row, $index }">
        <el-input
          v-model="row.purchasePrice"
          placeholder="请输入采购单价"
          type="number"
          @input="handleInput('purchasePrice', $index, $event)"
        >
          <template #suffix>
            <i style="font-style: normal">元</i>
          </template>
        </el-input>
      </template>
    </el-table-column>
 
    <el-table-column label="总成本" min-width="120">
      <template #default="{ row, $index }">
        <el-input
          disabled
          v-model="row.totalCost"
          placeholder="总成本"
          type="number"
          :readonly="autoCalculate"
          @input="handleInput('totalCost', $index, $event)"
        >
          <template #suffix>
            <i style="font-style: normal">元</i>
          </template>
        </el-input>
      </template>
    </el-table-column>
    <el-table-column label="生产人" min-width="120">
      <template #default="{ row, $index }">
        <el-select
          clearable
          :model-value="getUserNameById(row.producer) || row.producer"
          placeholder="请选择生产人"
          @change="(value) => handleUserSelectChange(row, value)"
          filterable
          :key="`producer-select-${$index}-${userList.length}`"
        >
          <el-option
            v-for="(item, index) of userList"
            :key="`option-${index}-${item.key}`"
            :label="item.value"
            :value="item.value"
          />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column
      v-if="showOperations"
      label="操作"
      width="120"
      fixed="right"
    >
      <template #default="{ $index }">
        <el-button
          type="danger"
          size="small"
          @click="handleDelete($index)"
          :icon="Delete"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>
 
<script setup name="ProductionDetailsTable">
import { ref, computed, watch, onMounted, nextTick } from "vue";
import { Delete } from "@element-plus/icons-vue";
import { getCoalFieldList } from "@/api/basicInformation/coalQualityMaintenance";
import { userListAll } from "@/api/publicApi";
const props = defineProps({
  modelValue: {
    type: Array,
    default: () => [],
  },
  border: {
    type: Boolean,
    default: false,
  },
  showOperations: {
    type: Boolean,
    default: true,
  },
  autoCalculate: {
    type: Boolean,
    default: true,
  },
});
 
const emit = defineEmits(["update:modelValue", "input-change", "delete-row"]);
 
// 使用 v-model 进行双向绑定
const tableData = computed({
  get() {
    return props.modelValue;
  },
  set(value) {
    emit("update:modelValue", value);
  },
});
 
// 处理输入变化
const handleInput = (field, index, value) => {
  const newData = [...tableData.value];
  newData[index][field] = value;
 
  // 如果开启自动计算总成本
  if (
    props.autoCalculate &&
    [
      "laborCost",
      "energyCost",
      "equipmentDepreciation",
      "purchasePrice",
    ].includes(field)
  ) {
    calculateTotalCost(newData[index]);
  }
 
  tableData.value = newData;
  emit("input-change", { field, index, value, row: newData[index] });
};
 
// 计算总成本
const calculateTotalCost = (row) => {
  const laborCost = parseFloat(row.laborCost) || 0;
  const energyCost = parseFloat(row.energyCost) || 0;
  const equipmentDepreciation = parseFloat(row.equipmentDepreciation) || 0;
  const purchasePrice = parseFloat(row.purchasePrice) || 0;
 
  row.totalCost = (
    laborCost +
    energyCost +
    equipmentDepreciation +
    purchasePrice
  ).toFixed(2);
};
 
// 删除行
const handleDelete = (index) => {
  const newData = [...tableData.value];
  newData.splice(index, 1);
  tableData.value = newData;
  emit("delete-row", index);
};
 
// 处理煤种选择变化
 
// 处理煤种选择变化(新方法:名称选择转ID)
const handleCoalSelectChange = (row, selectedName) => {
  // 根据选择的名称找到对应的ID
  const coalItem = weekList.value.find(item => item.value === selectedName);
  if (coalItem) {
    row.coal = coalItem.key; // 设置为ID
  } else {
    row.coal = ''; // 如果没找到,清空
  }
};
 
// 根据ID获取煤种名称(用于显示)
const getCoalNameById = (id) => {
  const coal = weekList.value.find(item => item.key == id);
  return coal ? coal.value : id;
};
 
const weekList = ref([]);
 
 
// 监听表格数据变化,确保显示正确
watch(() => props.modelValue, (newValue) => {
  if (newValue && weekList.value.length > 0) {
    // 当数据加载完成且weekList已获取时,确保显示正确
  }
}, { deep: true });
 
// 监听weekList变化,当下拉数据加载完成后处理显示
watch(weekList, (newList) => {
  if (newList.length > 0 && tableData.value.length > 0) {
    // 强制触发表格重新渲染以确保el-select正确显示
    nextTick(() => {
      // 触发一个微小的数据变化来强制重新渲染
      const tempData = [...tableData.value];
      tableData.value = tempData;
    });
  }
}, { deep: true });
 
onMounted(async()=>{
  let res = await getCoalFieldList()
  res.data.forEach(item => {
    let obj = {};
    obj.value = item.fieldName;
    obj.key = item.id;
    weekList.value.push(obj); 
  });
  let ress = await userListAll();
  ress.data.forEach(item => {
    let obj = {};
    obj.value = item.nickName;
    obj.key = item.userId;
    userList.value.push(obj); 
  });
  // 通知父组件weekList已加载完成
  nextTick(() => {
  });
})
const dropdownList = ref([]);
// 获取下拉数据
const getDropdownData = async () => {
  let res = await getCoalFieldList();
  if (res.code === 200) {
    dropdownList.value = res.data.map((item) => ({
      value: item.fieldName,
      key: item.id,
    }));
  } else {
    ElMessage.error("获取下拉数据失败");
  }
};
const userList = ref([]);
const getUserList = (async()=>{
  let res = await userListAll();
  if (res.code === 200) {
    userList.value = res.data.map((item) => ({
      value: item.nickName,
      key: item.userId,
    }));
  } else {
    ElMessage.error("获取用户列表失败");
  }
})
// 监听表格数据变化,确保显示正确
watch(() => props.modelValue, (newValue) => {
  if (newValue && userList.value.length > 0) {
    // 当数据加载完成且weekList已获取时,确保显示正确
  }
}, { deep: true });
 
// 监听userList变化,当下拉数据加载完成后处理显示
watch(userList, (newList) => {
  if (newList.length > 0 && tableData.value.length > 0) {
    // 强制触发表格重新渲染以确保el-select正确显示
    nextTick(() => {
      // 触发一个微小的数据变化来强制重新渲染
      const tempData = [...tableData.value];
      tableData.value = tempData;
    });
  }
}, { deep: true });
 
const getUserNameById = (id) => {
  const producer = userList.value.find(item => item.key == id);
  return producer ? producer.value : id;
};
// 处理用户选择变化(新方法:名称选择转ID)
const handleUserSelectChange = (row, selectedName) => {
  // 根据选择的名称找到对应的ID
  const userItem = userList.value.find(item => item.value === selectedName);
  if (userItem) {
    row.producer = userItem.key; // 设置为ID
  } else {
    row.producer = ''; // 如果没找到,清空
  }
};
// 暴露方法给父组件使用
defineExpose({
  calculateTotalCost,
  getDropdownData,
  getCoalNameById, // 暴露获取煤种名称的方法
  weekList, // 暴露weekList让父组件可以访问
  addRow: (rowData = {}) => {
    const defaultRow = {
      coal: "",
      calorificValue: "",
      productionQuantity: "",
      laborCost: "",
      energyCost: "",
      equipmentDepreciation: "",
      purchasePrice: "",
      totalCost: "",
      producer: "",
      ...rowData,
    };
    tableData.value = [...tableData.value, defaultRow];
  },
  clearData: () => {
    tableData.value = [];
  },
  // 添加一个方法来等待weekList加载完成
  waitForWeekList: () => {
    return new Promise((resolve) => {
      if (weekList.value.length > 0) {
        resolve();
      } else {
        const unwatch = watch(weekList, (newList) => {
          if (newList.length > 0) {
            unwatch();
            resolve();
          }
        });
      }
    });
  },
  // 强制刷新表格显示
  forceRefresh: () => {
    nextTick(() => {
      const tempData = [...tableData.value];
      tableData.value = tempData;
    });
  }
});
</script>
 
<style scoped>
:deep(.el-input__inner) {
  text-align: center;
}
 
:deep(.el-table .el-table__cell) {
  padding: 8px 0;
}
</style>