100 lines
2.6 KiB
JavaScript
100 lines
2.6 KiB
JavaScript
const employeesApi = require('../../api/employees')
|
|
const dateUtil = require('../../utils/date')
|
|
const { markPlanningRefresh } = require('../../utils/planning-refresh')
|
|
|
|
function formatExceptionSummary(item) {
|
|
const date = String(item.workDate || '').slice(0, 10)
|
|
const remark = String(item.remark || '').trim()
|
|
return {
|
|
id: item.id || `${date}-${remark || 'leave'}`,
|
|
date,
|
|
text: remark || '全天请假'
|
|
}
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
artistId: '',
|
|
records: [],
|
|
leaveForm: {
|
|
workDate: dateUtil.formatDate(new Date()),
|
|
remark: ''
|
|
},
|
|
loading: false,
|
|
saving: false,
|
|
loadError: ''
|
|
},
|
|
|
|
onLoad(options) {
|
|
this.setData({ artistId: options.id || '' })
|
|
this.loadRecords()
|
|
},
|
|
|
|
async loadRecords() {
|
|
if (!this.data.artistId) {
|
|
this.setData({ records: [], loadError: '员工ID不能为空' })
|
|
return
|
|
}
|
|
this.setData({ loading: true, loadError: '' })
|
|
try {
|
|
const setting = await employeesApi.scheduleRule({ artistId: this.data.artistId })
|
|
const exceptions = Array.isArray(setting.exceptions) ? setting.exceptions : []
|
|
this.setData({
|
|
records: exceptions
|
|
.filter(item => item.exceptionType === 'REST')
|
|
.map(formatExceptionSummary)
|
|
})
|
|
} catch (error) {
|
|
this.setData({
|
|
records: [],
|
|
loadError: error.message || '工时记录加载失败'
|
|
})
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
onLeaveDateChange(event) {
|
|
this.setData({ 'leaveForm.workDate': event.detail.value })
|
|
},
|
|
|
|
onLeaveRemarkInput(event) {
|
|
this.setData({ 'leaveForm.remark': event.detail.value })
|
|
},
|
|
|
|
async add() {
|
|
const form = this.data.leaveForm
|
|
if (!this.data.artistId) {
|
|
wx.showToast({ title: '员工ID不能为空', icon: 'none' })
|
|
return
|
|
}
|
|
if (!form.workDate) {
|
|
wx.showToast({ title: '请选择请假日期', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ saving: true })
|
|
try {
|
|
await employeesApi.saveScheduleException({
|
|
artistId: this.data.artistId,
|
|
workDate: form.workDate,
|
|
exceptionType: 'REST',
|
|
startTime: null,
|
|
endTime: null,
|
|
remark: String(form.remark || '').trim()
|
|
})
|
|
markPlanningRefresh()
|
|
wx.showToast({ title: '请假已新增', icon: 'success' })
|
|
this.setData({ 'leaveForm.remark': '' })
|
|
await this.loadRecords()
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '新增请假失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ saving: false })
|
|
}
|
|
},
|
|
|
|
back() {
|
|
wx.navigateBack()
|
|
}
|
|
})
|