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
<template>
  <view class="demand-page">
    <PageHeader title="采购需求"
                @back="goBack" />
 
    <!-- 筛选区域 -->
    <view class="search-section">
      <view class="search-bar">
        <view class="search-input">
          <up-input class="search-text"
                    v-model="queryParams.productName"
                    placeholder="请输入产品名称"
                    clearable
                    @change="handleSearch" />
        </view>
      </view>
      <view class="search-bar">
        <view class="search-input">
          <up-input class="search-text"
                    v-model="queryParams.productModel"
                    placeholder="请输入规格型号"
                    clearable
                    @change="handleSearch" />
        </view>
        <view class="filter-button"
              @click="showStatusSheet = true">
          <text class="filter-text">{{ statusLabel }}</text>
          <up-icon name="arrow-down"
                   size="14"
                   color="#999999"></up-icon>
        </view>
      </view>
    </view>
 
    <!-- 列表区域 -->
    <view v-if="list.length > 0"
          class="ledger-list">
      <view v-for="item in list"
            :key="item.id"
            class="ledger-item">
        <view class="item-header">
          <view class="item-left">
            <view class="document-icon">
              <up-icon name="shopping-cart"
                       size="16"
                       color="#ffffff"></up-icon>
            </view>
            <text class="item-id">{{ item.sourceBomNo || "无来源BOM" }}</text>
          </view>
          <up-tag :text="statusText(item.status)"
                  :type="statusType(item.status)"
                  size="mini" />
        </view>
        <up-divider></up-divider>
        <view class="item-details">
          <view class="detail-row">
            <text class="detail-label">产品名称</text>
            <text class="detail-value">{{ item.productName || "-" }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">规格型号</text>
            <text class="detail-value">{{ item.productModel || "-" }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">单位</text>
            <text class="detail-value">{{ item.unit || "-" }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">缺口需求量</text>
            <text class="detail-value danger">{{ num(item.demandQuantity) }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">库存储备快照</text>
            <text class="detail-value">{{ num(item.stockQuantity) }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">来源BOM编号</text>
            <text class="detail-value">{{ item.sourceBomNo || "-" }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">创建时间</text>
            <text class="detail-value">{{ item.createTime || "-" }}</text>
          </view>
        </view>
        <view class="action-buttons">
          <up-button v-if="isPending(item)"
                     class="action-btn"
                     size="small"
                     type="primary"
                     @click="handleChangeStatus(item, 1)">标记已转采购</up-button>
          <up-button v-if="isPending(item)"
                     class="action-btn"
                     size="small"
                     type="warning"
                     @click="handleChangeStatus(item, 2)">取消</up-button>
          <up-button class="action-btn"
                     size="small"
                     type="error"
                     plain
                     :disabled="!isPending(item)"
                     @click="handleDelete(item)">删除</up-button>
        </view>
      </view>
      <up-loadmore :status="pageStatus" />
    </view>
    <view v-else
          class="no-data">
      <up-empty text="暂无采购需求"
                mode="data"></up-empty>
    </view>
 
    <!-- 状态筛选 -->
    <up-action-sheet :show="showStatusSheet"
                     :actions="statusOptions"
                     title="选择状态"
                     @select="handleStatusSelect"
                     @close="showStatusSheet = false" />
  </view>
</template>
 
<script setup>
  import { reactive, ref } from "vue";
  import { onReachBottom, onShow } from "@dcloudio/uni-app";
  import { listPage, changeStatus, del } from "@/api/procurementManagement/procurementDemand";
  import PageHeader from "@/components/PageHeader.vue";
 
  const STATUS_MAP = {
    0: { text: "待处理", type: "warning" },
    1: { text: "已转采购", type: "success" },
    2: { text: "已取消", type: "info" },
  };
 
  const queryParams = reactive({
    productName: "",
    productModel: "",
    status: "",
  });
  const list = ref([]);
  const pageStatus = ref("loadmore");
  const page = reactive({
    current: 1,
    size: 10,
    total: 0,
  });
 
  const showStatusSheet = ref(false);
  const statusOptions = [
    { name: "全部", value: "" },
    { name: "待处理", value: 0 },
    { name: "已转采购", value: 1 },
    { name: "已取消", value: 2 },
  ];
  const statusLabel = ref("全部状态");
 
  // decimal 可能是字符串,统一转数字兜底
  const num = val => Number(val) || 0;
 
  const statusText = status => (STATUS_MAP[Number(status)] || STATUS_MAP[0]).text;
  const statusType = status => (STATUS_MAP[Number(status)] || STATUS_MAP[0]).type;
  // 仅待处理可操作(删除也由后端兜底校验)
  const isPending = item => Number(item.status) === 0;
 
  const goBack = () => {
    uni.navigateBack();
  };
 
  const handleSearch = () => {
    page.current = 1;
    pageStatus.value = "loadmore";
    list.value = [];
    getList();
  };
 
  const getList = () => {
    if (pageStatus.value === "loading" || pageStatus.value === "nomore") return;
 
    pageStatus.value = "loading";
    listPage({
      current: page.current,
      size: page.size,
      productName: queryParams.productName,
      productModel: queryParams.productModel,
      status: queryParams.status,
    })
      .then(res => {
        const records = res?.data?.records || res?.records || [];
        const total = res?.data?.total || res?.total || 0;
 
        if (page.current === 1) {
          list.value = records;
        } else {
          list.value = [...list.value, ...records];
        }
 
        page.total = total;
        if (list.value.length >= total) {
          pageStatus.value = "nomore";
        } else {
          pageStatus.value = "loadmore";
          page.current++;
        }
      })
      .catch(() => {
        // 错误提示由 request 统一处理
        pageStatus.value = "loadmore";
      });
  };
 
  const handleStatusSelect = action => {
    queryParams.status = action.value;
    statusLabel.value = action.value === "" ? "全部状态" : action.name;
    showStatusSheet.value = false;
    handleSearch();
  };
 
  const handleChangeStatus = (item, status) => {
    const actionText = status === 1 ? "标记为已转采购" : "取消";
    uni.showModal({
      title: "提示",
      content: `确定将该采购需求${actionText}吗?`,
      success: res => {
        if (!res.confirm) return;
        uni.showLoading({ title: "处理中...", mask: true });
        changeStatus({ ids: item.id, status })
          .then(() => {
            uni.hideLoading();
            uni.showToast({ title: "操作成功", icon: "none" });
            handleSearch();
          })
          .catch(() => {
            uni.hideLoading();
          });
      },
    });
  };
 
  const handleDelete = item => {
    uni.showModal({
      title: "删除确认",
      content: "确定删除该采购需求吗?",
      success: res => {
        if (!res.confirm) return;
        uni.showLoading({ title: "处理中...", mask: true });
        del([item.id])
          .then(() => {
            uni.hideLoading();
            uni.showToast({ title: "删除成功", icon: "none" });
            handleSearch();
          })
          .catch(() => {
            // 非待处理状态后端会返回提示,此处不再重复弹窗
            uni.hideLoading();
          });
      },
    });
  };
 
  onReachBottom(() => {
    getList();
  });
 
  onShow(() => {
    handleSearch();
  });
</script>
 
<style scoped lang="scss">
  @import "@/styles/procurement-common.scss";
 
  .demand-page {
    min-height: 100vh;
    background: #f8f9fa;
  }
 
  .search-bar + .search-bar {
    margin-top: 16rpx;
  }
 
  // 公共样式里 .filter-button 是 40px 见方的图标按钮,这里要放状态文案,需按内容撑开
  .filter-button {
    width: auto;
    min-width: 40px;
    padding: 0 12px;
    flex-shrink: 0;
    background: #f5f5f5;
  }
 
  .filter-text {
    font-size: 24rpx;
    color: #333;
    margin-right: 4rpx;
    white-space: nowrap;
  }
 
  .no-data {
    padding-top: 100rpx;
    text-align: center;
    color: #999;
    font-size: 28rpx;
  }
 
  .action-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 15rpx;
    padding: 0 30rpx 30rpx;
    flex-wrap: wrap;
  }
 
  .action-btn {
    width: calc(33.33% - 10rpx);
    margin: 0 !important;
    margin-bottom: 15rpx !important;
  }
</style>