const { fetchMobileRoles, fetchPurchaserSupplierStatus, completePurchaserSupplierInspectionBatch, } = require('../../utils/request'); const { ensureMobileRole } = require('../../utils/roleGuard'); const { beginPanelShow, endPanelShow } = require('../../utils/panelShowGate'); function formatLocalDate(date = new Date()) { const y = date.getFullYear(); const m = String(date.getMonth() + 1).padStart(2, '0'); const d = String(date.getDate()).padStart(2, '0'); return `${y}-${m}-${d}`; } function shiftLocalDate(days) { const date = new Date(); date.setDate(date.getDate() + days); return formatLocalDate(date); } function statusClass(status) { if (status === 30) return 'done'; if (status === 40) return 'stockout'; if (status === 20) return 'progress'; return 'pending'; } Component({ options: { addGlobalClass: true, }, properties: { embedded: { type: Boolean, value: false, }, }, lifetimes: { attached() { this.onPanelShow(); }, }, pageLifetimes: { show() { this.onPanelShow(); }, }, data: { groups: [], loading: false, submitting: false, error: '', viewMode: 'recent', deliveryDate: '', rangeFrom: '', rangeTo: '', rangeLabel: '', featureEnabled: false, oneClickEnabled: false, }, methods: { async onPanelShow() { if (!beginPanelShow(this)) return; try { const ok = await ensureMobileRole('purchaser'); if (!ok) return; await this.bootstrap(); } finally { endPanelShow(this); } }, initRecentRange() { const rangeFrom = shiftLocalDate(-1); const rangeTo = shiftLocalDate(1); this.setData({ rangeFrom, rangeTo, rangeLabel: `${rangeFrom} 至 ${rangeTo}`, deliveryDate: formatLocalDate(), }); }, async bootstrap() { if (!this.data.rangeFrom || !this.data.rangeTo) { this.initRecentRange(); } try { const rolesResult = await fetchMobileRoles(); const features = rolesResult?.purchaser_features || {}; const enabled = features.supplier_status_enabled === true || features.one_click_supplier_inspection_enabled === true; const oneClickEnabled = features.one_click_supplier_inspection_enabled === true; this.setData({ featureEnabled: enabled, oneClickEnabled }); if (!enabled) { this.setData({ error: '系统未启用此功能', groups: [] }); return; } await this.loadRows(); } catch (err) { this.setData({ error: err.message || '加载失败', groups: [], featureEnabled: false }); } }, buildRequestParams() { const { viewMode, deliveryDate, rangeFrom, rangeTo } = this.data; if (viewMode === 'pending') { return { all_pending: 1 }; } if (viewMode === 'date') { return { delivery_date: deliveryDate || formatLocalDate() }; } return { date_from: rangeFrom, date_to: rangeTo }; }, buildBatchPayload(extra = {}) { const params = this.buildRequestParams(); return { ...params, ...extra }; }, async loadRows() { if (!this.data.featureEnabled) return; this.setData({ loading: true, error: '' }); try { const result = await fetchPurchaserSupplierStatus(this.buildRequestParams()); const oneClickEnabled = result.one_click_supplier_inspection_enabled === true || this.data.oneClickEnabled; const groups = (result.data || []).map((group) => ({ ...group, collapsed: false, items: (group.items || []).map((item) => ({ ...item, statusClass: statusClass(Number(item.prepare_status)), })), })); this.setData({ groups, oneClickEnabled }); } catch (err) { this.setData({ error: err.message || '加载失败', groups: [] }); } finally { this.setData({ loading: false }); } }, showRecentRange() { this.initRecentRange(); this.setData({ viewMode: 'recent' }, () => this.loadRows()); }, showAllPending() { this.setData({ viewMode: 'pending' }, () => this.loadRows()); }, showSingleDate() { this.setData({ viewMode: 'date', deliveryDate: this.data.deliveryDate || formatLocalDate() }); }, onDeliveryDateChange(e) { this.setData({ viewMode: 'date', deliveryDate: e.detail.value }, () => this.loadRows()); }, toggleGroup(e) { const code = e.currentTarget.dataset.code; this.setData({ groups: this.data.groups.map((group) => group.supplier_code === code ? { ...group, collapsed: !group.collapsed } : group ), }); }, async runOneClickInspection(payload) { if (!this.data.oneClickEnabled || this.data.submitting) return; this.setData({ submitting: true }); try { const result = await completePurchaserSupplierInspectionBatch(payload); wx.showToast({ title: result.message || `已验收 ${result.completed || 0} 项`, icon: result.failed && result.failed.length ? 'none' : 'success', duration: 2500, }); await this.loadRows(); } catch (err) { wx.showToast({ title: err.message || '一键验收失败', icon: 'none' }); } finally { this.setData({ submitting: false }); } }, confirmBatchInspectSupplier(e) { const supplierCode = e.currentTarget.dataset.code; const supplierName = e.currentTarget.dataset.name || '该供应商'; const count = Number(e.currentTarget.dataset.count || 0); if (!supplierCode || count <= 0) return; wx.showModal({ title: '一键验收', content: `确认将 ${supplierName} 在当前筛选范围内 ${count} 项已供货商品按备货数量验收通过?`, success: (res) => { if (!res.confirm) return; this.runOneClickInspection(this.buildBatchPayload({ supplier_code: supplierCode })); }, }); }, confirmInspectItem(e) { const preparationId = e.currentTarget.dataset.id; const itemName = e.currentTarget.dataset.name || '该商品'; if (!preparationId) return; wx.showModal({ title: '一键验收', content: `确认将「${itemName}」按备货数量验收通过?`, success: (res) => { if (!res.confirm) return; this.runOneClickInspection({ preparation_ids: [preparationId] }); }, }); }, }, });