gaoluyang
3 天以前 0333d66e4b397c161c6a44ce1e2a121c2cc41082
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
<template>
  <view class="process-edit">
    <PageHeader :title="pageTitle"
                @back="goBack" />
    <up-form ref="formRef"
             :model="form"
             :rules="rules"
             :errorType="['none']"
             label-width="110">
      <up-form-item label="工序编码"
                    prop="no">
        <up-input v-model="form.no"
                  placeholder="请输入工序编码"
                  clearable />
      </up-form-item>
      <up-form-item label="工序名称"
                    prop="name"
                    required>
        <up-input v-model="form.name"
                  placeholder="请输入工序名称"
                  clearable />
      </up-form-item>
      <up-form-item label="工资定额"
                    prop="salaryQuota">
        <up-input v-model="form.salaryQuota"
                  type="number"
                  placeholder="请输入工资定额"
                  clearable />
      </up-form-item>
      <up-form-item label="计费类型"
                    prop="type">
        <up-input v-model="typeText"
                  placeholder="请选择计费类型"
                  readonly
                  @click="showTypeSheet = true" />
        <template #right>
          <up-icon name="arrow-right"
                   @click="showTypeSheet = true"></up-icon>
        </template>
      </up-form-item>
      <up-form-item label="是否质检"
                    prop="isQuality">
        <view style="display: flex; justify-content: flex-end; width: 100%;">
          <up-switch v-model="form.isQuality" />
        </view>
      </up-form-item>
      <up-form-item label="是否生产"
                    prop="isProduction">
        <view style="display: flex; justify-content: flex-end; width: 100%;">
          <up-switch v-model="form.isProduction" />
        </view>
      </up-form-item>
      <up-form-item label="关联设备"
                    prop="deviceLedgerId">
        <up-input v-model="deviceText"
                  placeholder="请选择关联设备"
                  readonly
                  @click="showDeviceSheet = true" />
        <template #right>
          <up-icon name="arrow-right"
                   @click="showDeviceSheet = true"></up-icon>
        </template>
      </up-form-item>
      <up-form-item label="工序描述"
                    prop="remark">
        <up-textarea v-model="form.remark"
                     placeholder="请输入工序描述"
                     autoHeight />
      </up-form-item>
    </up-form>
    <FooterButtons :loading="loading"
                   :confirmText="processId ? '保存' : '新增'"
                   @cancel="goBack"
                   @confirm="handleSubmit" />
    <!-- 计费类型选择 -->
    <up-action-sheet :show="showTypeSheet"
                     title="选择计费类型"
                     :actions="typeActions"
                     @select="onSelectType"
                     @close="showTypeSheet = false" />
    <!-- 设备选择 -->
    <up-action-sheet :show="showDeviceSheet"
                     title="选择关联设备"
                     :actions="deviceActions"
                     @select="onSelectDevice"
                     @close="showDeviceSheet = false" />
  </view>
</template>
 
<script setup>
  import { reactive, ref, computed, onMounted } from "vue";
  import { onLoad, onReady } from "@dcloudio/uni-app";
  import FooterButtons from "@/components/FooterButtons.vue";
  import {
    add,
    update,
    getDeviceLedger,
  } from "@/api/productionManagement/processManagement";
 
  const formRef = ref(null);
  const loading = ref(false);
  const processId = ref(null);
  const pageTitle = computed(() => (processId.value ? "编辑工序" : "新增工序"));
 
  const form = ref({
    no: "",
    name: "",
    salaryQuota: "",
    isQuality: false,
    isProduction: false,
    remark: "",
    deviceLedgerId: null,
    type: 0,
  });
 
  const rules = {
    name: [{ required: true, message: "请输入工序名称" }],
    salaryQuota: [
      {
        validator: (rule, value, callback) => {
          if (value !== "" && value !== null && (isNaN(value) || value < 0)) {
            callback(new Error("工资定额必须是非负数字"));
          } else {
            callback();
          }
        },
      },
    ],
  };
 
  const showTypeSheet = ref(false);
  const typeActions = [
    { name: "计时", value: 0 },
    { name: "计件", value: 1 },
  ];
  const typeText = computed(() => {
    const action = typeActions.find(a => a.value === form.value.type);
    return action ? action.name : "";
  });
 
  const showDeviceSheet = ref(false);
  const deviceActions = ref([]);
  const deviceText = ref("");
 
  const onSelectType = e => {
    form.value.type = e.value;
    showTypeSheet.value = false;
  };
 
  const onSelectDevice = e => {
    form.value.deviceLedgerId = e.id;
    deviceText.value = e.name;
    showDeviceSheet.value = false;
  };
 
  const loadDevices = async () => {
    try {
      const { data } = await getDeviceLedger();
      deviceActions.value = (data || []).map(item => ({
        name: item.deviceName,
        id: item.id,
      }));
      if (form.value.deviceLedgerId) {
        const device = deviceActions.value.find(
          d => d.id === Number(form.value.deviceLedgerId)
        );
        if (device) deviceText.value = device.name;
      }
    } catch (error) {
      console.error("加载设备失败", error);
    }
  };
 
  const goBack = () => {
    uni.navigateBack();
  };
 
  const handleSubmit = () => {
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const promise = processId.value ? update(form.value) : add(form.value);
        promise
          .then(() => {
            uni.showToast({ title: processId.value ? "保存成功" : "新增成功" });
            setTimeout(() => {
              goBack();
            }, 1500);
          })
          .catch(err => {
            uni.showToast({ title: err.msg || "提交失败", icon: "error" });
          })
          .finally(() => {
            loading.value = false;
          });
      })
      .catch(errors => {
        if (errors && errors.length > 0) {
          uni.showToast({
            title: errors[0].message,
            icon: "none",
          });
        }
      });
  };
 
  onLoad(option => {
    if (option.item) {
      const item = JSON.parse(decodeURIComponent(option.item));
      processId.value = item.id;
      Object.assign(form.value, item);
      // 处理类型转换,确保是数字
      form.value.type = Number(form.value.type);
      form.value.isQuality = !!form.value.isQuality;
      form.value.isProduction = !!form.value.isProduction;
    }
  });
 
  onReady(() => {
    formRef.value.setRules(rules);
  });
 
  onMounted(() => {
    loadDevices();
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
 
  .process-edit {
    min-height: 100vh;
    background: #f5f5f5;
  }
</style>