const { ensureMobileRole } = require('../../utils/roleGuard'); const { beginPanelShow, endPanelShow } = require('../../utils/panelShowGate'); const { fetchSupplierBids, fetchSupplierBidDetail, submitSupplierBidQuotes, } = require('../../utils/request'); function statusMeta(row) { const inv = row.status; const round = row.round_status; if (inv === 'expired') return { statusText: '已出局', statusClass: 'expired' }; if (inv === 'quoted') { if (round === 'awarded') return { statusText: '已中标/已开标', statusClass: 'done' }; return { statusText: '已报价', statusClass: 'quoted' }; } if (round === 'open') return { statusText: '待报价', statusClass: 'open' }; return { statusText: round || inv, statusClass: 'done' }; } function formatDeadline(v) { if (!v) return '—'; const d = new Date(v); if (Number.isNaN(d.getTime())) return String(v); const pad = (n) => String(n).padStart(2, '0'); return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`; } Component({ options: { addGlobalClass: true }, properties: { embedded: { type: Boolean, value: false }, }, data: { tab: 'open', loading: false, error: '', enabled: true, allRows: [], list: [], detail: null, priceMap: {}, submitting: false, }, lifetimes: { attached() { // 首页 wx:if 切模块时页面已 show,pageLifetimes.show 不会再触发 this.onPanelShow(); }, detached() { this.setData({ detail: null, priceMap: {} }); }, }, pageLifetimes: { show() { this.onPanelShow(); }, hide() { this.setData({ detail: null, priceMap: {} }); }, }, methods: { async onPanelShow() { if (!beginPanelShow(this)) return; try { const ok = await ensureMobileRole('supplier', { message: '请切换到供应商工作台后再查看竞价', }); if (!ok) return; await this.load(); } finally { endPanelShow(this); } }, onTab(e) { const tab = e.currentTarget.dataset.tab; if (!tab || tab === this.data.tab) return; this.setData({ tab }, () => this.applyFilter()); }, applyFilter() { const { tab, allRows } = this.data; const list = tab === 'open' ? allRows.filter((r) => r.status === 'pending' && r.round_status === 'open') : allRows.filter((r) => !(r.status === 'pending' && r.round_status === 'open')); this.setData({ list }); }, async load() { this.setData({ loading: true, error: '' }); try { const result = await fetchSupplierBids(); const settings = result.settings || {}; const rows = (result.data || []).map((row) => { const meta = statusMeta(row); return { ...row, ...meta, deadlineText: formatDeadline(row.respond_deadline || row.round_respond_deadline), }; }); this.setData( { loading: false, enabled: settings.enabled !== false, allRows: rows, }, () => { this.applyFilter(); const count = rows.filter((row) => row.status === 'pending' && row.round_status === 'open').length; this.triggerEvent('badgechange', { count }); } ); } catch (err) { this.setData({ loading: false, error: (err && err.message) || '加载失败', allRows: [], list: [], }); } }, async openDetail(e) { const id = e.currentTarget.dataset.id; if (!id) return; wx.showLoading({ title: '加载中' }); try { const result = await fetchSupplierBidDetail(id); const data = result.data || {}; const invitation = data.invitation || {}; const invStatus = invitation.status; const canQuote = data.round && data.round.status === 'open' && invStatus !== 'expired'; const priceMap = {}; (data.my_quotes || []).forEach((q) => { priceMap[q.sku_code] = String(q.unit_price); }); (data.items || []).forEach((item) => { if (priceMap[item.sku_code] == null) priceMap[item.sku_code] = ''; }); this.setData({ detail: { ...data, invitation: { ...invitation, statusText: statusMeta({ status: invStatus, round_status: data.round && data.round.status, }).statusText, deadlineText: formatDeadline(invitation.respond_deadline), }, canQuote, awards: data.awards || [], }, priceMap, }); } catch (err) { wx.showToast({ title: (err && err.message) || '加载失败', icon: 'none' }); } finally { wx.hideLoading(); } }, closeDetail() { this.setData({ detail: null, priceMap: {} }); }, onPriceInput(e) { const sku = e.currentTarget.dataset.sku; const value = e.detail.value; this.setData({ [`priceMap.${sku}`]: value }); }, async submitQuotes() { const detail = this.data.detail; if (!detail || !detail.canQuote) return; const quotes = Object.keys(this.data.priceMap) .map((sku_code) => ({ sku_code, unit_price: Number(this.data.priceMap[sku_code]), })) .filter((q) => Number.isFinite(q.unit_price) && q.unit_price > 0); if (!quotes.length) { wx.showToast({ title: '请至少填写一条报价', icon: 'none' }); return; } this.setData({ submitting: true }); try { await submitSupplierBidQuotes(detail.round.id, { quotes }); wx.showToast({ title: '报价已提交', icon: 'success' }); this.setData({ detail: null }); await this.load(); } catch (err) { wx.showToast({ title: (err && err.message) || '提交失败', icon: 'none' }); } finally { this.setData({ submitting: false }); } }, }, });