const api = require('../../utils/api') const util = require('../../utils/util') Page({ data: { startDate: '', endDate: '', rows: [], page: 1, pageInput: '1', totalPages: 1, refreshing: false, filterVoided: true, }, onLoad(options) { const defaultDate = util.today() this.setData({ startDate: options.startDate || defaultDate, endDate: options.endDate || defaultDate, }) }, onShow() { this.load() }, async load(page = this.data.page) { try { const result = await api.listDashboard({ startDate: this.data.startDate, endDate: this.data.endDate, page, pageSize: 8, includeVoided: !this.data.filterVoided, }) this.setData({ rows: result.rows, page: result.page, pageInput: String(result.page || 1), totalPages: result.totalPages, }) } catch (error) { wx.showToast({ title: error.message, icon: 'none' }) } }, async onPullDownRefresh() { this.setData({ refreshing: true }) try { await this.load(1) } finally { this.setData({ refreshing: false }) if (wx.stopPullDownRefresh) { wx.stopPullDownRefresh() } } }, onStartChange(e) { this.setData({ startDate: e.detail.value, page: 1 }) this.load(1) }, onEndChange(e) { this.setData({ endDate: e.detail.value, page: 1 }) this.load(1) }, shiftRangeDate(field, days) { const otherField = field === 'startDate' ? 'endDate' : 'startDate' const current = this.data[field] const other = this.data[otherField] const base = current || other || util.today() const updates = { [field]: util.addDays(base, days), page: 1, } if (!current && !other) { updates[otherField] = base } this.setData(updates) this.load(1) }, decreaseStartDate() { this.shiftRangeDate('startDate', -1) }, increaseEndDate() { this.shiftRangeDate('endDate', 1) }, toggleFilterVoided() { this.setData({ filterVoided: !this.data.filterVoided, page: 1 }) this.load(1) }, onFilterVoidedChange(e) { this.setData({ filterVoided: !!e.detail.value, page: 1 }) this.load(1) }, prevPage() { if (this.data.page > 1) { this.load(this.data.page - 1) } }, nextPage() { if (this.data.page < this.data.totalPages) { this.load(this.data.page + 1) } }, onPageInput(e) { this.setData({ pageInput: e.detail.value }) }, jumpToPage(e) { wx.hideKeyboard() const inputValue = e && e.detail && e.detail.value !== undefined ? e.detail.value : this.data.pageInput const target = Math.max(1, Math.min(Number(inputValue) || this.data.page, this.data.totalPages)) if (target === this.data.page) { this.setData({ pageInput: String(this.data.page || 1) }) return } this.load(target) }, async exportFile() { wx.showLoading({ title: '导出中' }) try { const filePath = await api.exportDashboard({ startDate: this.data.startDate, endDate: this.data.endDate, includeVoided: !this.data.filterVoided, }) wx.openDocument({ filePath, fileType: 'xlsx', showMenu: true, }) } catch (error) { wx.showToast({ title: error.message, icon: 'none' }) } finally { wx.hideLoading() } }, })