const { fetchPrepAmendPending, fetchPrepAmendReviewed, fetchPrepAmendDetail, confirmPrepAmend, rejectPrepAmend, } = require('../../utils/request'); const { ensureMobileRole } = require('../../utils/roleGuard'); const { beginPanelShow, endPanelShow } = require('../../utils/panelShowGate'); const { formatChinaDateTime } = require('../../utils/chinaDate'); const AMEND_ROLES = ['admin', 'operator', 'purchaser']; function qtyPriceText(qty, unit, price) { return `${qty ?? '-'} ${unit || ''} · ¥${price ?? '-'}`; } function numFieldChanged(before, proposed) { const a = Number(before); const b = Number(proposed); if (!Number.isFinite(a) && !Number.isFinite(b)) return false; if (!Number.isFinite(a) || !Number.isFinite(b)) return true; return a !== b; } function textFieldChanged(before, proposed) { return String(before || '').trim() !== String(proposed ?? '').trim(); } function mapPendingRow(row) { return { ...row, displayTime: row.requested_at ? formatChinaDateTime(row.requested_at) : '', beforeText: qtyPriceText(row.before_qty, row.before_unit, row.before_price), proposedText: qtyPriceText(row.proposed_qty, row.proposed_unit, row.proposed_price), editQty: row.proposed_qty != null ? String(row.proposed_qty) : '', editUnit: row.proposed_unit || row.before_unit || '个', editPrice: row.proposed_price != null ? String(row.proposed_price) : '', qtyChanged: numFieldChanged(row.before_qty, row.proposed_qty), unitChanged: textFieldChanged(row.before_unit, row.proposed_unit || row.before_unit), priceChanged: numFieldChanged(row.before_price, row.proposed_price), beforeQtyLabel: row.before_qty != null ? String(row.before_qty) : '-', beforeUnitLabel: String(row.before_unit || '').trim() || '-', beforePriceLabel: row.before_price != null ? String(row.before_price) : '-', }; } function mapReviewedRow(row) { const status = Number(row.status); const qty = row.final_qty != null ? row.final_qty : row.proposed_qty; const unit = row.final_unit || row.proposed_unit || row.before_unit; const price = row.final_price != null ? row.final_price : row.proposed_price; return { ...row, displayTime: row.reviewed_at ? formatChinaDateTime(row.reviewed_at) : '', beforeText: qtyPriceText(row.before_qty, row.before_unit, row.before_price), resultText: qtyPriceText(qty, unit, price), statusLabel: status === 20 ? '已确认' : status === 30 ? '已驳回' : '已审', statusTone: status === 20 ? 'ok' : status === 30 ? 'reject' : '', }; } Component({ options: { addGlobalClass: true, }, properties: { embedded: { type: Boolean, value: false, }, initialId: { type: String, value: '', }, homeStatusGroup: { type: String, value: 'pending', }, }, observers: { homeStatusGroup(next) { if (!this.properties.embedded) return; const tab = String(next || 'pending') === 'reviewed' ? 'reviewed' : 'pending'; if (tab === this.data.listTab) return; this.setData({ listTab: tab }, () => { if (this._panelReady) this.loadList(); }); }, }, lifetimes: { attached() { this.onPanelShow(); }, }, pageLifetimes: { show() { this.onPanelShow(); }, }, data: { loading: true, listTab: 'pending', pendingCount: 0, reviewedCount: 0, rows: [], mode: 'list', detail: null, editPrice: '', editQty: '', editUnit: '', remark: '', saving: false, savingId: '', }, methods: { async onPanelShow() { if (!beginPanelShow(this)) return; try { const ok = await ensureMobileRole(AMEND_ROLES); if (!ok) { this.setData({ loading: false }); return; } const id = String(this.properties.initialId || '').trim(); if (id && !this._openedInitialId) { this._openedInitialId = id; } if (this.properties.embedded) { const fromHome = String(this.properties.homeStatusGroup || 'pending') === 'reviewed' ? 'reviewed' : 'pending'; if (fromHome !== this.data.listTab) { this.setData({ listTab: fromHome }); } } await this.loadList(); this._panelReady = true; } finally { endPanelShow(this); } }, async loadList() { const tab = this.data.listTab === 'reviewed' ? 'reviewed' : 'pending'; this.setData({ loading: true, mode: 'list', detail: null }); try { if (tab === 'reviewed') { const result = await fetchPrepAmendReviewed(); const rows = (result.data || []).map(mapReviewedRow); this.setData({ rows, reviewedCount: rows.length }, () => this.emitCounts()); fetchPrepAmendPending() .then((pending) => { const n = (pending.data || []).length; this.setData({ pendingCount: n }, () => { this.triggerEvent('badgechange', { count: n }); this.emitCounts(); }); }) .catch(() => {}); } else { const result = await fetchPrepAmendPending(); const rows = (result.data || []).map(mapPendingRow); this.setData({ rows, pendingCount: rows.length }, () => { this.triggerEvent('badgechange', { count: rows.length }); this.emitCounts(); }); fetchPrepAmendReviewed() .then((reviewed) => { this.setData({ reviewedCount: (reviewed.data || []).length }, () => this.emitCounts()); }) .catch(() => {}); } } catch (error) { wx.showToast({ title: error.message || '加载失败', icon: 'none' }); } finally { this.setData({ loading: false }); } }, emitCounts() { this.triggerEvent('progresschange', { pendingCount: this.data.pendingCount, reviewedCount: this.data.reviewedCount, }); }, onSetListTab(e) { const tab = String(e.currentTarget.dataset.tab || '').trim(); if (tab !== 'pending' && tab !== 'reviewed') return; if (tab === this.data.listTab) return; this.setData({ listTab: tab }, () => this.loadList()); }, onTapRow(e) { const id = e.currentTarget.dataset.id; if (id) this.openDetail(id); }, findRow(id) { return (this.data.rows || []).find((row) => String(row.id) === String(id)) || null; }, onRowFieldInput(e) { const id = e.currentTarget.dataset.id; const field = e.currentTarget.dataset.field; if (!id || !field) return; const value = e.detail.value; this.setData({ rows: this.data.rows.map((row) => String(row.id) === String(id) ? { ...row, [field]: value } : row ), }); }, async onConfirmRow(e) { const id = e.currentTarget.dataset.id; const row = this.findRow(id); if (!row || this.data.saving || this.data.listTab === 'reviewed') return; const purchasePrice = Number(row.editPrice); const prepareQty = Number(row.editQty); if (!Number.isFinite(purchasePrice) || purchasePrice < 0) { wx.showToast({ title: '请填写有效单价', icon: 'none' }); return; } if (!Number.isFinite(prepareQty) || prepareQty <= 0) { wx.showToast({ title: '请填写有效数量', icon: 'none' }); return; } this.setData({ saving: true, savingId: id }); try { const result = await confirmPrepAmend(row.id, { purchase_price: purchasePrice, prepare_qty: prepareQty, unit: String(row.editUnit || '').trim() || '个', }); const needsPrice = result && result.data && result.data.closed_loop && result.data.closed_loop.needsCustomerPrice; wx.showToast({ title: needsPrice ? '已确认;请补客户单价' : '已确认并闭环', icon: needsPrice ? 'none' : 'success', duration: needsPrice ? 2800 : 1500, }); setTimeout(() => this.loadList(), 400); } catch (error) { wx.showToast({ title: error.message || '确认失败', icon: 'none' }); } finally { this.setData({ saving: false, savingId: '' }); } }, async onRejectRow(e) { const id = e.currentTarget.dataset.id; const row = this.findRow(id); if (!row || this.data.saving || this.data.listTab === 'reviewed') return; const that = this; wx.showModal({ title: '驳回申请', editable: true, placeholderText: '驳回原因(可选)', success: async (res) => { if (!res.confirm) return; that.setData({ saving: true, savingId: id }); try { await rejectPrepAmend(row.id, { remark: res.content || undefined, }); wx.showToast({ title: '已驳回', icon: 'success' }); setTimeout(() => that.loadList(), 400); } catch (error) { wx.showToast({ title: error.message || '驳回失败', icon: 'none' }); } finally { that.setData({ saving: false, savingId: '' }); } }, }); }, async openDetail(id) { this.setData({ loading: true, mode: 'detail' }); try { const result = await fetchPrepAmendDetail(id); const detail = result.data; if (!detail) { wx.showToast({ title: '申请不存在', icon: 'none' }); this.loadList(); return; } this.setData({ detail: { ...detail, beforeQtyText: detail.before_qty == null || detail.before_qty === '' ? '-' : String(detail.before_qty), beforePriceText: detail.before_price == null || detail.before_price === '' ? '-' : String(detail.before_price), proposedQtyText: detail.proposed_qty == null || detail.proposed_qty === '' ? '-' : String(detail.proposed_qty), proposedPriceText: detail.proposed_price == null || detail.proposed_price === '' ? '-' : String(detail.proposed_price), }, editPrice: detail.proposed_price != null ? String(detail.proposed_price) : '', editQty: detail.proposed_qty != null ? String(detail.proposed_qty) : '', editUnit: detail.proposed_unit || detail.before_unit || '个', remark: '', }); } catch (error) { wx.showToast({ title: error.message || '加载失败', icon: 'none' }); this.loadList(); } finally { this.setData({ loading: false }); } }, onEditInput(e) { const field = e.currentTarget.dataset.field; this.setData({ [field]: e.detail.value }); }, backToList() { this.loadList(); }, async onConfirm() { if (this.data.saving || !this.data.detail) return; const purchasePrice = Number(this.data.editPrice); const prepareQty = Number(this.data.editQty); if (!Number.isFinite(purchasePrice) || purchasePrice < 0) { wx.showToast({ title: '请填写有效单价', icon: 'none' }); return; } if (!Number.isFinite(prepareQty) || prepareQty <= 0) { wx.showToast({ title: '请填写有效数量', icon: 'none' }); return; } this.setData({ saving: true }); try { const result = await confirmPrepAmend(this.data.detail.id, { purchase_price: purchasePrice, prepare_qty: prepareQty, unit: String(this.data.editUnit || '').trim() || '个', remark: this.data.remark || undefined, }); const needsPrice = result && result.data && result.data.closed_loop && result.data.closed_loop.needsCustomerPrice; wx.showToast({ title: needsPrice ? '已确认;请补客户单价' : '已确认并闭环', icon: needsPrice ? 'none' : 'success', duration: needsPrice ? 2800 : 1500, }); setTimeout(() => this.loadList(), 400); } catch (error) { wx.showToast({ title: error.message || '确认失败', icon: 'none' }); } finally { this.setData({ saving: false }); } }, async onReject() { if (this.data.saving || !this.data.detail) return; const that = this; wx.showModal({ title: '驳回申请', editable: true, placeholderText: '驳回原因(可选)', success: async (res) => { if (!res.confirm) return; that.setData({ saving: true }); try { await rejectPrepAmend(that.data.detail.id, { remark: res.content || that.data.remark || undefined, }); wx.showToast({ title: '已驳回', icon: 'success' }); setTimeout(() => that.loadList(), 400); } catch (error) { wx.showToast({ title: error.message || '驳回失败', icon: 'none' }); } finally { that.setData({ saving: false }); } }, }); }, }, });