const employeesApi = require('../../api/employees') const dateUtil = require('../../utils/date') const { showErrorToast, toUserMessage } = require('../../utils/toast') function displayDate(value) { const text = String(value || '').slice(0, 10) return text || '-' } function exceptionText(item) { const type = String(item.exceptionType || '').toUpperCase() if (type.includes('FULL')) return '全天请假' const start = String(item.startTime || '').slice(0, 5) const end = String(item.endTime || '').slice(0, 5) if (start || end) return `${start || '-'}-${end || '-'} 请假` return '请假' } Page({ data: { days: [], records: [], artists: [], artistNames: {}, loading: false, loadError: '', formVisible: false, saving: false, form: { artistIndex: 0, workDate: dateUtil.formatDate(new Date()), exceptionType: 'FULL_DAY', startTime: '09:00', endTime: '18:00', remark: '' } }, onLoad() { this.month = new Date() this.loadRecords() this.loadArtists() }, async loadArtists() { try { const data = await employeesApi.list() const list = data.list || data || [] const artists = list.map(item => ({ id: item.userId || item.id, name: item.nickname || item.name || item.artistName || '美甲师' })).filter(item => item.id) const artistNames = artists.reduce((map, item) => { map[item.id] = item.name return map }, {}) this.setData({ artists, artistNames }) } catch (error) { // 美甲师列表失败不阻塞请假记录展示。 } }, async loadRecords() { const month = this.month const startDate = `${month.getFullYear()}-${String(month.getMonth() + 1).padStart(2, '0')}-01` const daysInMonth = new Date(month.getFullYear(), month.getMonth() + 1, 0).getDate() const endDate = `${month.getFullYear()}-${String(month.getMonth() + 1).padStart(2, '0')}-${String(daysInMonth).padStart(2, '0')}` this.setData({ loading: true, loadError: '' }) try { const list = await employeesApi.scheduleExceptions({ startDate, endDate }) const exceptions = Array.isArray(list) ? list : (list && list.list) || [] const restDates = new Set(exceptions.map(item => String(item.workDate || '').slice(0, 10))) const days = Array.from({ length: daysInMonth }).map((_, index) => { const date = `${month.getFullYear()}-${String(month.getMonth() + 1).padStart(2, '0')}-${String(index + 1).padStart(2, '0')}` return { date, day: index + 1, rest: restDates.has(date) } }) const artistNames = this.data.artistNames || {} const records = exceptions .slice() .sort((left, right) => String(left.workDate).localeCompare(String(right.workDate))) .map(item => ({ key: item.id || `${item.workDate}-${item.artistId}`, date: displayDate(item.workDate), text: [exceptionText(item), artistNames[item.artistId]].filter(Boolean).join(' · ') })) this.setData({ days, records }) } catch (error) { this.setData({ records: [], loadError: toUserMessage(error, '请假记录加载失败') }) } finally { this.setData({ loading: false }) } }, add() { const artists = this.data.artists || [] if (!artists.length) { wx.showToast({ title: '暂无可请假的美甲师', icon: 'none' }) return } const app = getApp() const userInfo = app.globalData.userInfo || {} const ownIndex = artists.findIndex(item => String(item.id) === String(userInfo.id)) this.setData({ formVisible: true, form: { artistIndex: ownIndex >= 0 ? ownIndex : 0, workDate: dateUtil.formatDate(new Date()), exceptionType: 'FULL_DAY', startTime: '09:00', endTime: '18:00', remark: '' } }) }, closeForm() { if (this.data.saving) return this.setData({ formVisible: false }) }, stopTouchMove() {}, noop() {}, onArtistChange(event) { this.setData({ 'form.artistIndex': Number(event.detail.value) }) }, onDateChange(event) { this.setData({ 'form.workDate': event.detail.value }) }, selectType(event) { this.setData({ 'form.exceptionType': event.currentTarget.dataset.type }) }, onStartTimeChange(event) { this.setData({ 'form.startTime': event.detail.value }) }, onEndTimeChange(event) { this.setData({ 'form.endTime': event.detail.value }) }, onRemarkInput(event) { this.setData({ 'form.remark': event.detail.value }) }, async save() { if (this.data.saving) return const form = this.data.form const artist = this.data.artists[form.artistIndex] if (!artist) { wx.showToast({ title: '请选择美甲师', icon: 'none' }) return } if (!form.workDate) { wx.showToast({ title: '请选择日期后保存', icon: 'none' }) return } if (form.exceptionType !== 'FULL_DAY' && dateUtil.minutesOfDay(form.endTime) <= dateUtil.minutesOfDay(form.startTime)) { wx.showToast({ title: '结束时间需晚于开始时间', icon: 'none' }) return } this.setData({ saving: true }) try { await employeesApi.saveScheduleException({ artistId: artist.id, workDate: form.workDate, exceptionType: form.exceptionType, startTime: form.exceptionType === 'FULL_DAY' ? undefined : form.startTime, endTime: form.exceptionType === 'FULL_DAY' ? undefined : form.endTime, remark: form.remark || undefined }) this.setData({ formVisible: false }) wx.showToast({ title: '请假已保存' }) this.loadRecords() } catch (error) { showErrorToast(error, '请假保存失败') } finally { this.setData({ saving: false }) } }, back() { wx.navigateBack() } })