const employeesApi = require('../../api/employees') const planningApi = require('../../api/planning') const dateUtil = require('../../utils/date') const EMPLOYEE_COLORS = ['#7AA9C2', '#8EB99B', '#BDA7D8', '#D7B17D', '#D98D8D'] function buildScheduleSummary(item) { const mode = item.scheduleModeSummary || item.scheduleMode || '未设置' const time = item.scheduleTimeSummary || item.ruleTimeSummary || '' return time ? `${mode} ${time}` : mode } function formatApplicationTime(value) { if (!value) return '' return String(value).replace('T', ' ').slice(0, 16) } function confirmModal(options) { return new Promise(resolve => { wx.showModal({ ...options, success: resolve, fail: () => resolve({ confirm: false }) }) }) } Page({ data: { employees: [], loadError: '', isManager: false, reviewExpanded: false, reviewApplications: [], reviewPendingCount: 0, reviewLoading: false, reviewError: '', reviewingId: '' }, onLoad() { const userInfo = getApp().globalData.userInfo || {} this.setData({ isManager: Number(userInfo.type) === 2 }) this.loadEmployees() if (Number(userInfo.type) === 2) this.loadReviewSummary() }, onShow() { const app = getApp() if (!this.data.isManager || !app.globalData.openEmployeeReview) return app.globalData.openEmployeeReview = false this.setData({ reviewExpanded: true }) return this.loadReviewApplications() }, async loadReviewSummary() { if (!this.data.isManager) return try { const summary = await employeesApi.reviewSummary() this.setData({ reviewPendingCount: Number(summary.pendingCount || 0) }) } catch (error) { // 审核摘要失败不影响员工列表。 } }, toggleReview() { if (!this.data.isManager) return const reviewExpanded = !this.data.reviewExpanded this.setData({ reviewExpanded }) if (reviewExpanded) this.loadReviewApplications() }, async loadReviewApplications() { if (!this.data.isManager || this.data.reviewLoading) return this.setData({ reviewLoading: true, reviewError: '' }) try { const data = await employeesApi.reviewApplications() const applications = (Array.isArray(data) ? data : []).map(item => ({ ...item, applicantNickname: item.applicantNickname || '待审核员工', applicantAvatar: item.applicantAvatar || '', applicantMaskedPhone: item.applicantMaskedPhone || '', applicationTimeText: formatApplicationTime(item.createTime) })) this.setData({ reviewApplications: applications, reviewPendingCount: applications.length }) } catch (error) { this.setData({ reviewError: error.message || '审核申请加载失败' }) } finally { this.setData({ reviewLoading: false }) } }, async approveApplication(event) { const id = event.currentTarget.dataset.id if (!id || this.data.reviewingId) return const result = await confirmModal({ title: '通过员工申请', content: '确认允许该员工加入当前门店吗?' }) if (!result.confirm) return await this.submitReview(id, () => employeesApi.approveApplication(id), '申请已通过') }, async rejectApplication(event) { const id = event.currentTarget.dataset.id if (!id || this.data.reviewingId) return const result = await confirmModal({ title: '驳回员工申请', content: '', editable: true, placeholderText: '请输入驳回原因', confirmText: '确认驳回' }) if (!result.confirm) return const reason = String(result.content || '').trim() if (!reason) { wx.showToast({ title: '请输入驳回原因', icon: 'none' }) return } await this.submitReview(id, () => employeesApi.rejectApplication(id, reason), '申请已驳回') }, async submitReview(id, action, successMessage) { this.setData({ reviewingId: String(id) }) try { await action() wx.showToast({ title: successMessage, icon: 'success' }) await Promise.all([this.loadReviewApplications(), this.loadEmployees()]) } catch (error) { wx.showToast({ title: error.message || '申请状态已变化,请刷新重试', icon: 'none' }) await this.loadReviewApplications() } finally { this.setData({ reviewingId: '' }) } }, async loadEmployees() { try { const today = dateUtil.formatDate(new Date()) const [data, calendar] = await Promise.all([ employeesApi.list({ page: 1, size: 50 }), planningApi.calendar({ startDate: today, endDate: today }) ]) const list = data.list || data || [] const appointments = calendar.appointments || [] const artists = calendar.artists || [] this.setData({ loadError: '', employees: list.map((item, index) => ({ id: item.userId || item.id, name: item.nickname || item.name || item.artistName || `美甲师${index + 1}`, shortName: (item.nickname || item.name || '师').slice(-1), avatar: item.avatar || item.avatarUrl || '', color: EMPLOYEE_COLORS[index % EMPLOYEE_COLORS.length], monthOrders: item.monthOrders || item.orderCount || 0, monthAmount: item.monthAmount || item.turnover || 0, scheduleSummary: buildScheduleSummary(item), todaySchedule: (() => { const artist = artists.find(entry => String(entry.id) === String(item.userId || item.id)) return artist && artist.workStartTime && artist.workEndTime ? `${artist.workStartTime.slice(0, 5)}-${artist.workEndTime.slice(0, 5)}` : '今日未排班' })(), todayAvailable: (() => { const artist = artists.find(entry => String(entry.id) === String(item.userId || item.id)) return !!artist && Number(artist.status) !== 0 })(), todayOrders: appointments.filter(entry => String(entry.artistId) === String(item.userId || item.id)).length })) }) } catch (error) { this.setData({ employees: [], loadError: error.message || '美甲师列表加载失败' }) } }, openDetail(event) { wx.navigateTo({ url: `/pages/employee-detail/index?id=${event.currentTarget.dataset.id}` }) }, addEmployee() { wx.navigateTo({ url: '/pages/employee-create/index', success: result => { result.eventChannel.on('employeeCreated', () => this.loadEmployees()) } }) } })