const api = require('../../utils/api') const emptyForm = { attendancePointName: '', deviceNo: '', deviceType: '冲压设备', remark: '', } const deviceTypeOptions = ['冲压设备', '清洗设备'] Page({ data: { keyword: '', rows: [], page: 1, pageInput: '1', totalPages: 1, form: { ...emptyForm }, formVisible: false, formTitle: '新增设备', attendancePoints: [], attendancePointLabels: [], attendancePointIndex: 0, filterAttendancePointLabels: ['全部考勤点'], filterAttendancePointIndex: 0, filterAttendancePointName: '', deviceTypeOptions, refreshing: false, }, onShow() { this.init() }, async init() { await this.loadAttendancePoints() await this.load() }, async loadAttendancePoints() { try { const points = await api.listAccessibleAttendancePoints() const attendancePoints = points || [] this.setData({ attendancePoints, attendancePointLabels: attendancePoints.map(item => item.name), filterAttendancePointLabels: ['全部考勤点'].concat(attendancePoints.map(item => item.name)), }) if (!this.data.form.attendancePointName && attendancePoints.length) { this.setData({ 'form.attendancePointName': attendancePoints[0].name, attendancePointIndex: 0, }) } } catch (error) { wx.showToast({ title: error.message || '加载考勤点失败', icon: 'none' }) } }, async load(page = this.data.page) { try { const result = await api.listEquipment({ keyword: this.data.keyword, attendancePointName: this.data.filterAttendancePointName, page, pageSize: 8, }) 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() } } }, onKeywordInput(e) { this.setData({ keyword: e.detail.value, page: 1 }) this.load(1) }, onFilterAttendancePointChange(e) { const filterAttendancePointIndex = Number(e.detail.value) const point = this.data.attendancePoints[filterAttendancePointIndex - 1] this.setData({ filterAttendancePointIndex, filterAttendancePointName: point ? point.name : '', page: 1, }) this.load(1) }, onInput(e) { const field = e.currentTarget.dataset.field this.setData({ [`form.${field}`]: e.detail.value, }) }, onDeviceTypeChange(e) { const index = Number(e.detail.value) this.setData({ 'form.deviceType': deviceTypeOptions[index] || '冲压设备', }) }, onAttendancePointChange(e) { const attendancePointIndex = Number(e.detail.value) const point = this.data.attendancePoints[attendancePointIndex] this.setData({ attendancePointIndex, 'form.attendancePointName': point ? point.name : '', }) }, noop() {}, openCreate() { const firstPoint = this.data.attendancePoints[0] this.setData({ form: { ...emptyForm, attendancePointName: firstPoint ? firstPoint.name : '' }, attendancePointIndex: 0, formTitle: '新增设备', formVisible: true, }) }, edit(e) { const index = e.currentTarget.dataset.index const equipment = this.data.rows[index] const attendancePointIndex = Math.max(0, this.data.attendancePoints.findIndex(item => item.name === equipment.attendancePointName)) this.setData({ form: { ...equipment, originalAttendancePointName: equipment.attendancePointName, originalDeviceNo: equipment.deviceNo, }, attendancePointIndex, formTitle: '编辑设备', formVisible: true, }) }, closeForm() { this.setData({ formVisible: false, form: { ...emptyForm, attendancePointName: this.data.attendancePoints[0] ? this.data.attendancePoints[0].name : '' }, }) }, clearForm() { const firstPoint = this.data.attendancePoints[0] this.setData({ form: { ...emptyForm, attendancePointName: firstPoint ? firstPoint.name : '' }, attendancePointIndex: 0, }) }, async save() { try { if (!this.data.form.attendancePointName) { wx.showToast({ title: '请选择考勤点', icon: 'none' }) return } await api.saveEquipment(this.data.form) wx.showToast({ title: '已保存', icon: 'success' }) this.setData({ form: { ...emptyForm }, formVisible: false }) await this.load(1) } catch (error) { wx.showToast({ title: error.message || '保存失败', icon: 'none' }) } }, remove(e) { const index = e.currentTarget.dataset.index const equipment = this.data.rows[index] wx.showModal({ title: '删除设备', content: equipment.deviceNo, confirmText: '删除', confirmColor: '#d92d20', success: async res => { if (!res.confirm) { return } try { await api.deleteEquipment(equipment) await this.load(1) } catch (error) { wx.showToast({ title: error.message || '删除失败', icon: 'none' }) } }, }) }, 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) }, })