spring
2025-11-19 c13c2bb15b501c74fe4270b0ae763e5c9422ee39
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
<template>
  <wd-form ref="form" :model="model" class="relative form_box">
    <wd-cell-group :border="true">
      <wd-input
        v-model="model.actuallyLength"
        label="生产长度(m)"
        label-width="100px"
        prop="actuallyLength"
        clearable
        placeholder="请输入生产长度"
        type="digit"
      >
        <template #label>
          <span style="color: #f56c6c">生产长度(m)</span>
        </template>
      </wd-input>
      <wd-input
        v-model="model.tare"
        label="盘具皮重(kg)"
        label-width="100px"
        prop="tare"
        :disabled="!isFirstReport"
        :clearable="isFirstReport"
        :placeholder="isFirstReport ? '请输入盘具皮重' : '盘具皮重自动带出'"
        type="digit"
      >
        <template #label>
          <span style="color: #f56c6c">盘具皮重(kg)</span>
        </template>
      </wd-input>
    </wd-cell-group>
  </wd-form>
</template>
 
<script lang="ts" setup>
import { computed, watch } from "vue";
import useFormData from "@/hooks/useFormData";
import { useToast, dayjs } from "wot-design-uni";
import TwistApi from "@/api/product/twist";
 
// 定义 props
const props = defineProps<{
  firstTareValue?: number;
  teamId?: string | number | null;
  isFirstReport?: boolean; // 是否是第一条报工
}>();
 
// 计算是否是第一条报工
const isFirstReport = computed(() => props.isFirstReport ?? true);
 
const paramsId = ref();
const toast = useToast();
const { form: model, resetForm } = useFormData({
  actuallyLength: undefined, // 生产长度(m)
  tare: undefined, // 盘具皮重(kg)
});
 
// 主表数据(从父组件传入)
const mainTableData = ref<any>({});
 
// 设置主表数据
const setMainTableData = (data: any) => {
  mainTableData.value = data;
};
 
// 监听 firstTareValue 变化,如果不是第一条,自动填充
watch(
  () => props.firstTareValue,
  (newVal) => {
    if (!isFirstReport.value && newVal !== undefined) {
      model.tare = newVal;
    }
  },
  { immediate: true }
);
 
const submit = async () => {
  // 获取第一条数据的皮重值,用于后续新增的报工
  const firstTareValue = props.firstTareValue;
 
  // 如果主表数据未获取,尝试重新获取
  if (!mainTableData.value.model) {
    try {
      const { data } = await TwistApi.getTwistDetailById({
        id: paramsId.value,
      });
      mainTableData.value = {
        model: data.model,
        totalLength: data.totalLength,
        systemNo: data.systemNo,
      };
    } catch (error) {
      console.error("获取主表数据失败:", error);
      toast.error("获取规格型号数据失败,请重试");
      return false;
    }
  }
 
  // 再次检查主表数据
  if (!mainTableData.value.model) {
    toast.error("规格型号数据未获取,请重试");
    return false;
  }
 
  const submitData = {
    teamId: props.teamId || null,
    wireId: paramsId.value,
    type: "绞线",
    actuallyLength: model.actuallyLength,
    tare: model.tare || firstTareValue,
    // 从主表获取的字段
    model: mainTableData.value.model, // 规格型号
    oneLength: mainTableData.value.totalLength,
    systemNo: mainTableData.value.systemNo,
    monofilamentNumber: undefined, // 批次号(后端自动生成)
    // 生产日期自动设置为当前时间
    productTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  };
 
  // 调试日志
  console.log("提交数据:", submitData);
  console.log("主表数据:", mainTableData.value);
 
  const { code } = await TwistApi.addTwistOutput(submitData);
  if (code == 200) {
    toast.success("提交成功");
    resetForm();
    return true;
  } else {
    toast.error("提交失败");
    return false;
  }
};
 
// 获取表单数据
const getFormData = () => {
  return { ...model };
};
 
onLoad((options: any) => {
  paramsId.value = options.id;
});
 
defineExpose({
  submit,
  getFormData,
  setMainTableData,
});
</script>
 
<style lang="scss" scoped>
.submit_btn {
  position: absolute;
  bottom: 0;
  width: 100%;
}
</style>