const { deviceMaintenanceApi } = require("../../utils/api"); const dayjs = require("dayjs"); Page({ data: { sub_device_id: null, subDeviceInfo: null, maintenanceList: [], loading: false, maintenanceTypes: [ { value: '常规保养', label: '常规保养' }, { value: '故障维修', label: '故障维修' }, { value: '定期检查', label: '定期检查' }, { value: '设备升级', label: '设备升级' }, ], }, onLoad(options) { if (options.sub_device_id) { this.setData({ sub_device_id: options.sub_device_id }); this.loadSubDeviceInfo(); this.fetchMaintenanceList(); } else { wx.showToast({ title: '缺少设备ID', icon: 'error', complete: () => wx.navigateBack(), }); } }, onShow() { if (this.data.sub_device_id) { this.fetchMaintenanceList(); } }, // 加载子设备信息 loadSubDeviceInfo() { const app = getApp(); const subDevice = app.globalData.subDevices?.find( d => d.id === parseInt(this.data.sub_device_id) ); if (subDevice) { this.setData({ subDeviceInfo: subDevice, deviceStatus: subDevice.status || 'normal', selectedStatus: subDevice.status || 'normal' }); } }, // 获取保养记录列表 fetchMaintenanceList() { this.setData({ loading: true }); deviceMaintenanceApi.getMaintenanceBySubDeviceId(this.data.sub_device_id) .then(res => { const formattedList = res.data.list.map(item => { item.start_time_formatted = dayjs(item.start_time).format('YYYY-MM-DD HH:mm'); item.end_time_formatted = item.end_time ? dayjs(item.end_time).format('YYYY-MM-DD HH:mm') : ''; item.maintenance_by = item.operator?.nickname || '-'; item.notes = item.notes || '无'; item.device_name = item.subDevice?.device?.name || '未知设备'; item.sub_device_name = item.subDevice?.name || '未知子设备'; return item; }); this.setData({ maintenanceList: formattedList, loading: false }); }) .catch(err => { wx.showToast({ title: '获取记录失败', icon: 'error', }); this.setData({ loading: false }); }); }, // 添加保养记录 addMaintenance() { wx.navigateTo({ url: `/pages/device_maintenance/create/index?sub_device_id=${this.data.sub_device_id}` }); }, // 显示编辑对话框 - 改为跳转到编辑页面 showEditDialog(e) { const record = e.currentTarget.dataset.record; wx.navigateTo({ url: `/pages/device_maintenance/create/index?sub_device_id=${this.data.sub_device_id}&maintenance_id=${record.id}` }); }, // 删除保养记录 deleteMaintenance(e) { const record = e.currentTarget.dataset.record; wx.showModal({ title: '确认删除', content: '确定要删除此保养记录吗?此操作不可恢复!', success: (res) => { if (res.confirm) { deviceMaintenanceApi.deleteMaintenance(record.id) .then(() => { wx.showToast({ title: '删除成功', icon: 'success' }); this.fetchMaintenanceList(); }) .catch(err => { wx.showToast({ title: err.response?.data?.msg || '删除失败', icon: 'error', }); }); } } }); }, // 滑动单元格操作 onActionClick(e) { const record = e.currentTarget.dataset.item; const { text } = e.detail; // 构造一个与旧函数兼容的事件对象 const mockEvent = { currentTarget: { dataset: { record: record } } }; if (text === '编辑') { this.showEditDialog(mockEvent); } else if (text === '删除') { this.deleteMaintenance(mockEvent); } }, // 返回上一页 onBack() { wx.navigateBack(); }, });