const employeesApi = require('../../api/employees') const systemParameterConfigApi = require('../../api/system-parameter-config') const EMPLOYEE_COLORS = ['#7AA9C2', '#8EB99B', '#BDA7D8', '#D7B17D', '#D98D8D'] const EMPTY_FIELD_TEXT = '未填写' function displayText(value) { const text = String(value || '').trim() return text || EMPTY_FIELD_TEXT } function buildRoleText(data) { return Number(data.type) === 2 ? '店长' : '店员' } Page({ data: { id: '', employee: {}, loadError: '', statusUpdating: false, statusConfirmVisible: false, pendingStatusValue: 1, pendingStatusText: '', deleteConfirmVisible: false, deletingEmployee: false, resetConfirmVisible: false, resettingPassword: false, moreInfoVisible: false, dangerExpanded: false }, onLoad(options) { this.setData({ id: options.id || '' }) }, onShow() { if (this.data.id) { this.loadDetail() } }, async loadDetail() { try { const data = await employeesApi.detail(this.data.id) if (!data) { throw new Error('员工资料不存在') } const name = data.nickname || data.name || data.artistName || '美甲师' const monthOrders = data.monthOrders || data.orderCount || 0 const monthAmount = data.monthAmount || data.turnover || 0 const avgAmount = data.avgAmount === undefined || data.avgAmount === null ? (monthOrders > 0 ? Math.round(monthAmount / monthOrders) : 0) : data.avgAmount this.setData({ loadError: '', employee: { ...data, name, shortName: name.slice(-1), color: data.color || EMPLOYEE_COLORS[0], roleText: buildRoleText(data), accountText: displayText(data.account), phoneText: displayText(data.phone), contactPhoneText: displayText(data.contactPhone), wechatIdText: displayText(data.wechatId), identityText: displayText(data.identity), monthOrders, monthAmount, avgAmount, currentStatusText: displayText(data.currentStatusText), participateProductionText: Number(data.participateProduction) === 1 ? '是' : '否' } }) } catch (error) { this.setData({ employee: {}, loadError: error.message || '美甲师详情加载失败' }) } }, go(event) { wx.navigateTo({ url: `${event.currentTarget.dataset.url}?id=${this.data.id}` }) }, toggleMoreInfo() { this.setData({ moreInfoVisible: !this.data.moreInfoVisible }) }, toggleDangerZone() { this.setData({ dangerExpanded: !this.data.dangerExpanded }) }, requestDeleteEmployee() { if (this.data.deletingEmployee) { return } this.setData({ deleteConfirmVisible: true }) }, closeDeleteConfirm() { if (this.data.deletingEmployee) { return } this.setData({ deleteConfirmVisible: false }) }, stopTap() { }, requestToggleStatus() { if (this.data.statusUpdating) { return } const currentStatusCode = String(this.data.employee.currentStatusCode || '').trim() const nextStatus = currentStatusCode === 'DISABLED' ? 1 : 0 const pendingStatusText = nextStatus === 1 ? '恢复正常' : '停用账号' this.setData({ statusConfirmVisible: true, pendingStatusValue: nextStatus, pendingStatusText }) }, closeStatusConfirm() { if (this.data.statusUpdating) { return } this.setData({ statusConfirmVisible: false, pendingStatusValue: 1, pendingStatusText: '' }) }, async confirmToggleStatus() { if (this.data.statusUpdating || !this.data.id) { return } this.setData({ statusUpdating: true }) try { const success = await employeesApi.updateStatus(this.data.id, this.data.pendingStatusValue) if (!success) { throw new Error('员工状态更新失败') } this.setData({ statusConfirmVisible: false, pendingStatusValue: 1, pendingStatusText: '' }) wx.showToast({ title: '状态已更新', icon: 'success' }) await this.loadDetail() } catch (error) { wx.showToast({ title: error.message || '员工状态更新失败', icon: 'none' }) } finally { this.setData({ statusUpdating: false }) } }, requestResetPassword() { if (this.data.resettingPassword) { return } this.setData({ resetConfirmVisible: true }) }, closeResetConfirm() { if (this.data.resettingPassword) { return } this.setData({ resetConfirmVisible: false }) }, async confirmResetPassword() { if (this.data.resettingPassword || !this.data.id) { return } this.setData({ resettingPassword: true }) try { const success = await employeesApi.resetPassword(this.data.id) if (!success) { throw new Error('密码重置失败') } const result = await systemParameterConfigApi.getResetDefaultPassword() const password = String(result && result.password || '').trim() if (!password) { throw new Error('默认密码为空') } this.setData({ resetConfirmVisible: false }) wx.showModal({ title: '密码已重置', content: `系统默认密码:${password}`, confirmText: '复制密码', cancelText: '关闭', success: (modalResult) => { if (modalResult.confirm) { wx.setClipboardData({ data: password, success: () => wx.showToast({ title: '密码已复制', icon: 'success' }) }) } } }) } catch (error) { wx.showToast({ title: error.message || '密码重置失败', icon: 'none' }) } finally { this.setData({ resettingPassword: false }) } }, async confirmDeleteEmployee() { if (this.data.deletingEmployee) { return } if (!this.data.id) { wx.showToast({ title: '缺少员工 ID,无法删除', icon: 'none' }) return } this.setData({ deletingEmployee: true }) try { const success = await employeesApi.deleteUser(this.data.id) if (!success) { throw new Error('员工不存在或已被删除') } this.setData({ deleteConfirmVisible: false }) wx.showToast({ title: '员工已删除', icon: 'success' }) setTimeout(() => { wx.navigateBack() }, 600) } catch (error) { wx.showToast({ title: error.message || '员工删除失败', icon: 'none' }) } finally { this.setData({ deletingEmployee: false }) } }, back() { wx.navigateBack() } })