const planningApi = require('../../api/planning') const dateUtil = require('../../utils/date') const { markPlanningRefresh } = require('../../utils/planning-refresh') Page({ data: { id: '', artists: [], artistItems: [], selectedArtistId: '', selectedArtist: {}, artistSheetVisible: false, artistKeyword: '', date: dateUtil.formatDate(new Date()), time: '13:00', loadError: '', loading: false }, onLoad(options) { this.setData({ id: options.id || '' }) this.loadArtists() }, async loadArtists(preferredArtistId, fallbackEmployee) { try { const calendar = await planningApi.calendar({ startDate: this.data.date, endDate: this.data.date }) let artists = Array.isArray(calendar.artists) ? calendar.artists : [] if (fallbackEmployee && preferredArtistId && !artists.some(item => String(item.id) === String(preferredArtistId))) { artists = artists.concat({ id: preferredArtistId, name: fallbackEmployee.name || fallbackEmployee.title, avatar: fallbackEmployee.avatar || '' }) } const selectedArtistId = preferredArtistId || this.data.selectedArtistId const selectedArtist = artists.find(item => String(item.id) === String(selectedArtistId)) || {} this.setData({ artists: artists.map(item => ({ ...item, availableStatus: 'free', availableText: '可选择' })), artistItems: this.filterArtistItems(artists, this.data.artistKeyword), selectedArtistId: selectedArtist.id || '', selectedArtist, loadError: '' }) } catch (error) { this.setData({ artists: [], artistItems: [], selectedArtistId: '', selectedArtist: {}, loadError: error.message || '美甲师列表加载失败' }) } }, filterArtistItems(artists, keywordValue) { const keyword = String(keywordValue || '').trim().toLowerCase() return (artists || []) .filter(item => !keyword || String(item.name || '').toLowerCase().includes(keyword)) .map(item => ({ ...item, title: item.name || '未命名员工', subtitle: item.statusText || '可用于改派', meta: item.workStartTime && item.workEndTime ? `${item.workStartTime.slice(0, 5)}-${item.workEndTime.slice(0, 5)}` : '', avatar: item.avatar || item.avatarUrl || '/assets/images/default-user-avatar.png' })) }, openArtistSheet() { this.setData({ artistSheetVisible: true, artistItems: this.filterArtistItems(this.data.artists, this.data.artistKeyword) }) }, closeArtistSheet() { this.setData({ artistSheetVisible: false }) }, onArtistSearch(event) { const artistKeyword = event.detail.value this.setData({ artistKeyword, artistItems: this.filterArtistItems(this.data.artists, artistKeyword) }) }, selectArtist(event) { const selectedArtist = event.detail.item || {} this.setData({ selectedArtistId: selectedArtist.id, selectedArtist, artistSheetVisible: false }) }, retryArtists() { this.loadArtists() }, addEmployeeFromSelector() { this.setData({ artistSheetVisible: false }) wx.navigateTo({ url: '/pages/employee-create/index?picker=1', success: result => { result.eventChannel.on('employeeCreated', employee => this.loadArtists(employee.id, employee)) } }) }, async onDateChange(event) { this.setData({ date: event.detail.value }) await this.loadArtists(this.data.selectedArtistId) }, onTimeChange(event) { this.setData({ time: event.detail.value }) }, async submit() { if (!this.data.selectedArtistId) { wx.showToast({ title: '请选择美甲师', icon: 'none' }) return } this.setData({ loading: true }) try { await planningApi.moveAppointment(this.data.id, { artistId: Number(this.data.selectedArtistId), appointmentDate: this.data.date, appointmentTime: this.data.time }) markPlanningRefresh() wx.showToast({ title: '已改派' }) setTimeout(() => wx.navigateBack(), 500) } catch (error) { wx.showToast({ title: error.message || '改派失败', icon: 'none' }) } finally { this.setData({ loading: false }) } }, back() { wx.navigateBack() } })