const planningApi = require('../../api/planning') const customersApi = require('../../api/customers') const { markPlanningRefresh } = require('../../utils/planning-refresh') const { showErrorToast, toUserMessage } = require('../../utils/toast') const { showActionModal } = require('../../utils/action-modal') function displayValue(value, fallback) { if (value === null || value === undefined) return fallback const text = String(value).trim() return !text || text === 'null' || text === 'undefined' ? fallback : text } function formatAmount(value) { const amount = displayValue(value, '') return amount ? `¥${amount}` : '未记录' } function isPaid(appointment) { return String((appointment && appointment.paymentStatus) || '').toUpperCase() === 'PAID' } const PAY_METHODS = [ { label: '现金', value: 'CASH' }, { label: '微信', value: 'WECHAT' }, { label: '支付宝', value: 'ALIPAY' }, { label: '线下其他', value: 'OFFLINE' } ] Page({ data: { id: '', appointment: {}, coverImages: [], coverIndex: 0, avatarText: '', timeText: '', isUnassigned: false, showTransfer: false, showEdit: false, showConfirm: false, showCollect: false, showComplete: false, showCancel: false, updating: false, paying: false, dialing: false, loadError: '' }, onLoad(options) { this.setData({ id: options.id || '' }) this.loadDetail() }, onShow() { if (this._needReload && this.data.id) { this._needReload = false this.loadDetail() } }, async loadDetail() { try { const appointment = await planningApi.appointment(this.data.id) this.applyAppointment(appointment) } catch (error) { this.setData({ appointment: {}, avatarText: '', timeText: '', loadError: toUserMessage(error, '预约详情加载失败') }) } }, applyAppointment(appointment) { const safeAppointment = appointment || {} const status = Number(safeAppointment.status) const historicalBackfill = Boolean(safeAppointment.historicalBackfill) const isUnassigned = safeAppointment.assignmentStatus === 'UNASSIGNED' || !safeAppointment.artistId const startTime = displayValue(safeAppointment.startTime, '') const endTime = displayValue(safeAppointment.endTime, '') const timeText = historicalBackfill && !startTime ? '时间未记录' : startTime ? `${startTime.slice(0, 16)}${endTime ? ` - ${endTime.slice(11, 16)}` : ''}` : displayValue(safeAppointment.date, '时间未记录') const normalizedAppointment = { ...safeAppointment, customerNameText: displayValue(safeAppointment.customerName, '散客'), customerPhoneText: displayValue(safeAppointment.customerPhone, '未留手机号'), orderNoText: displayValue(safeAppointment.orderNo, '未记录'), productTitleText: displayValue(safeAppointment.productTitle, '未记录'), artistNameText: isUnassigned ? '待分配' : displayValue(safeAppointment.artistName, '未记录'), durationText: Number(safeAppointment.durationMinutes) > 0 ? `${safeAppointment.durationMinutes} 分钟` : '未记录', amountText: formatAmount(safeAppointment.amount), statusText: displayValue(safeAppointment.statusText, '状态未知'), remarkText: displayValue(safeAppointment.remark, '暂无备注'), cancelReasonText: displayValue(safeAppointment.cancelReason, ''), paymentStatusText: isPaid(safeAppointment) ? `已收款${safeAppointment.paidAmount !== null && safeAppointment.paidAmount !== undefined ? ` ¥${safeAppointment.paidAmount}` : ''}` : '未收款' } const coverImages = Array.isArray(safeAppointment.coverImages) ? safeAppointment.coverImages.filter(item => Boolean(item)) : [] const paid = isPaid(safeAppointment) const canOperateService = !isUnassigned && (status === 1 || status === 4) this.setData({ appointment: normalizedAppointment, coverImages, coverIndex: 0, isUnassigned, avatarText: normalizedAppointment.customerNameText.slice(0, 1), timeText, showTransfer: Boolean(safeAppointment.canEdit) && !historicalBackfill, showEdit: Boolean(safeAppointment.canEdit) && !historicalBackfill && (status === 0 || status === 1), showConfirm: Boolean(safeAppointment.canEdit) && !historicalBackfill && !isUnassigned && status === 0, showCollect: canOperateService && !paid, showComplete: canOperateService && paid, showCancel: Boolean(safeAppointment.canCancel), loadError: '' }) }, onCoverChange(event) { this.setData({ coverIndex: Number(event.detail.current) || 0 }) }, onCoverImageError(event) { const index = Number(event.currentTarget.dataset.index) if (!Number.isFinite(index)) return if (!this._coverFailedIndexes) this._coverFailedIndexes = {} this._coverFailedIndexes[index] = true }, previewCoverImage(event) { const index = Number(event.currentTarget.dataset.index) const failed = this._coverFailedIndexes || {} const images = (this.data.coverImages || []).filter((url, itemIndex) => url && !failed[itemIndex]) if (!images.length) { wx.showToast({ title: '暂无可预览的图片', icon: 'none' }) return } const target = (this.data.coverImages || [])[Number.isFinite(index) ? index : 0] wx.previewImage({ current: target && images.indexOf(target) >= 0 ? target : images[0], urls: images, fail: () => wx.showToast({ title: '图片预览失败', icon: 'none' }) }) }, async updateStatus(status) { if (this.data.updating || !this.data.appointment.orderId) return if (status !== 3 && this.data.isUnassigned) { wx.showToast({ title: '请先分配美甲师', icon: 'none' }) return } this.setData({ updating: true }) try { await planningApi.updateStatus(this.data.appointment.orderId, status, this._cancelReason || '') markPlanningRefresh() wx.showToast({ title: '已更新' }) this.loadDetail() } catch (error) { showErrorToast(error, '操作失败') } finally { this.setData({ updating: false }) } }, confirm() { if (!this.data.showConfirm || this.data.updating) return this.updateStatus(1) }, complete() { if (!this.data.showComplete || this.data.updating) return if (!isPaid(this.data.appointment)) { wx.showToast({ title: '请先完成收款', icon: 'none' }) return } this.updateStatus(2) }, choosePayMethod() { return new Promise(resolve => { wx.showActionSheet({ itemList: PAY_METHODS.map(item => item.label), success: result => resolve(PAY_METHODS[result.tapIndex] || PAY_METHODS[0]), fail: () => resolve(null) }) }) }, async collect() { if (!this.data.showCollect || this.data.paying || this.data.updating) return const appointment = this.data.appointment || {} const orderId = appointment.orderId if (!orderId) return const method = await this.choosePayMethod() if (!method) return const defaultAmount = appointment.payAmount !== null && appointment.payAmount !== undefined ? appointment.payAmount : appointment.amount const input = await showActionModal(this, { title: '登记收款', content: `收款方式:${method.label}`, editable: true, inputType: 'digit', inputValue: defaultAmount === null || defaultAmount === undefined ? '' : defaultAmount, placeholderText: '请输入实收金额', cancelText: '取消', confirmText: '确认收款' }) if (!input.confirm) return const text = String(input.content || '').trim() const paidAmount = Number(text) if (!text || !Number.isFinite(paidAmount) || paidAmount < 0) { wx.showToast({ title: '请输入正确的收款金额', icon: 'none' }) return } this.setData({ paying: true }) try { await planningApi.registerOfflinePayment(orderId, { payMethod: method.value, paidAmount }) markPlanningRefresh() const result = await showActionModal(this, { title: '收款已登记', content: `实收 ¥${paidAmount},是否同时完成服务?`, cancelText: '暂不完成', confirmText: '同时完成服务' }) if (result.confirm) { await planningApi.updateStatus(orderId, 2, '') markPlanningRefresh() wx.showToast({ title: '已完成服务' }) } else { wx.showToast({ title: '收款已登记', icon: 'success' }) } this.loadDetail() } catch (error) { showErrorToast(error, '收款登记失败') } finally { this.setData({ paying: false }) } }, goEdit() { if (!this.data.showEdit) return this._needReload = true wx.navigateTo({ url: `/pages/appointment-form/index?mode=edit&id=${this.data.appointment.orderItemId || this.data.id}` }) }, async cancel() { if (!this.data.showCancel || this.data.updating) return this._cancelReason = '' const result = await showActionModal(this, { title: '取消预约', editable: true, placeholderText: '可填写取消原因', cancelText: '取消', confirmText: '确认取消', confirmType: 'danger' }) if (!result.confirm) return this._cancelReason = result.content || '' this.updateStatus(3) }, async dialCustomer() { const appointment = this.data.appointment || {} if (this.data.dialing) return if (!appointment.customerPhoneAvailable || !appointment.orderId) { wx.showToast({ title: '该客户暂无可拨打的手机号', icon: 'none' }) return } this.setData({ dialing: true }) try { const result = await customersApi.dialCustomer({ customerId: appointment.customerId, appointmentId: appointment.orderItemId, orderId: appointment.orderId }) if (!result || !result.dialNumber) { throw new Error('未获取到可拨打号码') } wx.makePhoneCall({ phoneNumber: result.dialNumber }) } catch (error) { showErrorToast(error, '取号失败') } finally { this.setData({ dialing: false }) } }, goTransfer() { if (!this.data.showTransfer) return wx.navigateTo({ url: `/pages/artist-transfer/index?id=${this.data.id}` }) }, back() { wx.navigateBack() } })