Files
JayuCloud/JC-Salon-WeChat/pages/artist-transfer/index.js
T

167 lines
5.4 KiB
JavaScript

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',
durationMinutes: '',
isUnassigned: false,
loadError: '',
loading: false
},
async onLoad(options) {
this.setData({
id: options.id || ''
})
const loaded = await this.loadAppointment()
if (!loaded) return
await this.loadArtists()
},
async loadAppointment() {
if (!this.data.id) return false
try {
const appointment = await planningApi.appointment(this.data.id)
const startTime = String(appointment.startTime || '')
this.setData({
date: appointment.date || startTime.slice(0, 10) || this.data.date,
time: startTime.slice(11, 16) || this.data.time,
durationMinutes: appointment.durationMinutes || '',
isUnassigned: appointment.assignmentStatus === 'UNASSIGNED' || !appointment.artistId,
loadError: ''
})
return true
} catch (error) {
this.setData({ loadError: error.message || '预约详情加载失败' })
return false
}
},
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&date=${encodeURIComponent(this.data.date || '')}&time=${encodeURIComponent(this.data.time || '')}&durationMinutes=${encodeURIComponent(this.data.durationMinutes || '')}`,
success: result => {
result.eventChannel.on('employeeCreated', employee => {
this.loadArtists(employee.autoSelect === false ? '' : 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,
durationMinutes: Number(this.data.durationMinutes) || undefined
})
markPlanningRefresh()
wx.showToast({ title: this.data.isUnassigned ? '已分配' : '已改派' })
setTimeout(() => wx.navigateBack(), 500)
} catch (error) {
wx.showToast({ title: error.message || '改派失败', icon: 'none' })
} finally {
this.setData({ loading: false })
}
},
back() {
wx.navigateBack()
}
})