zhangwencui
10 天以前 263b4a60670671cef5445b102e670c40ba34a162
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
<template>
  <view class="account-detail">
    <PageHeader title="下发" @back="goBack" />
    <up-form ref="formRef" :model="form" :rules="rules" label-width="110">
      <up-form-item label="产品名称">
        <up-input v-model="form.productName" disabled placeholder="自动填充" />
      </up-form-item>
      <up-form-item label="规格型号">
        <up-input v-model="form.model" disabled placeholder="自动填充" />
      </up-form-item>
      <up-form-item label="计划完成时间" prop="planCompleteTime" required>
        <up-input v-model="form.planCompleteTime" placeholder="请选择" readonly @click="showPlanDatePicker = true" />
        <template #right>
          <up-icon name="arrow-right" @click="showPlanDatePicker = true"></up-icon>
        </template>
      </up-form-item>
      <up-form-item label="生产数量" prop="totalAssignedQuantity" required>
        <up-input v-model="form.totalAssignedQuantity" type="number" placeholder="请输入" clearable />
      </up-form-item>
      <up-form-item label="可下发数量">
        <up-input :model-value="String(maxQuantity)" disabled placeholder="自动计算" />
      </up-form-item>
    </up-form>
 
    <FooterButtons :loading="loading" confirmText="确定下发" @cancel="goBack" @confirm="handleSubmit" />
 
    <up-datetime-picker
      :show="showPlanDatePicker"
      v-model="planDateValue"
      mode="date"
      @confirm="onPlanDateConfirm"
      @cancel="showPlanDatePicker = false"
    />
  </view>
</template>
 
<script setup>
  import { ref } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import FooterButtons from "@/components/FooterButtons.vue";
  import PageHeader from "@/components/PageHeader.vue";
  import { formatDateToYMD } from "@/utils/ruoyi";
  import { productionPlanCombine } from "@/api/productionManagement/productionPlan";
 
  const formRef = ref();
  const loading = ref(false);
 
  const maxQuantity = ref(0);
  const showPlanDatePicker = ref(false);
  const planDateValue = ref(Date.now());
 
  const form = ref({
    productName: "",
    model: "",
    totalAssignedQuantity: "",
    planCompleteTime: "",
    productId: undefined,
    ids: [],
  });
 
  const rules = {
    planCompleteTime: [{ required: true, message: "请选择计划完成时间", trigger: "change" }],
    totalAssignedQuantity: [{ required: true, message: "请输入生产数量", trigger: "blur" }],
  };
 
  const goBack = () => {
    uni.navigateBack();
  };
 
  const onPlanDateConfirm = e => {
    const value = e?.value ?? planDateValue.value;
    form.value.planCompleteTime = formatDateToYMD(value);
    showPlanDatePicker.value = false;
  };
 
  const handleSubmit = async () => {
    const valid = await formRef.value.validate().catch(() => false);
    if (!valid) return;
 
    const qty = Number(form.value.totalAssignedQuantity);
    if (!qty || qty <= 0) {
      uni.showToast({ title: "生产数量必须大于0", icon: "none" });
      return;
    }
    if (qty > Number(maxQuantity.value || 0)) {
      uni.showToast({ title: "生产数量不能大于可下发数量", icon: "none" });
      return;
    }
    if (!Array.isArray(form.value.ids) || form.value.ids.length === 0) {
      uni.showToast({ title: "请选择要下发的计划", icon: "none" });
      return;
    }
 
    loading.value = true;
    const payload = {
      ...form.value,
      totalAssignedQuantity: Number(qty.toFixed(4)),
    };
    productionPlanCombine(payload)
      .then(res => {
        if (res?.code && Number(res.code) !== 200) {
          const msg = res?.msg || res?.message || "下发失败";
          uni.showToast({ title: String(msg), icon: "none" });
          return;
        }
        uni.showToast({ title: "下发成功", icon: "success" });
        uni.$emit("mainProductionPlan:refresh");
        goBack();
      })
      .catch(err => {
        const msg = err?.msg || err?.message || err?.data?.msg || err?.data?.message;
        uni.showToast({ title: msg ? String(msg) : "下发失败", icon: "none" });
      })
      .finally(() => {
        loading.value = false;
      });
  };
 
  onLoad(options => {
    if (!options?.data) return;
    try {
      const payload = JSON.parse(decodeURIComponent(options.data));
      form.value.productName = payload.productName || "";
      form.value.model = payload.model || "";
      form.value.productId = payload.productId ?? undefined;
      form.value.ids = Array.isArray(payload.ids) ? payload.ids : [];
      form.value.planCompleteTime = payload.planCompleteTime || "";
      form.value.totalAssignedQuantity = String(payload.totalAssignedQuantity ?? "");
      maxQuantity.value = Number(payload.maxQuantity ?? 0);
      if (!form.value.planCompleteTime) {
        form.value.planCompleteTime = formatDateToYMD(Date.now());
      }
    } catch {
      uni.showToast({ title: "数据加载失败", icon: "none" });
    }
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
</style>