import scheduleservice from '../../../service/scheduleservice'; import moment from '../../../utils/moment.min'; import { animation1 } from '../../../utils/animation'; import $f from "../../../service/baserequest"; Component({ properties: {}, data: { icon_calendar: $f.baseIconUrl + 'res/calendar.jpg', shcedules: [], expand: false, unfinishedCount: 0, }, lifetimes: { attached: function () { this.getTodaySchedules(); }, }, methods: { initAnimation() { this.animation = animation1({ content: this, selector: '.schedule-card__item', size: this.data.schedules.length, margin: 1, }); }, getTodaySchedules() { let startTime = moment().format('YYYY-MM-DD'); let endTime = startTime; const that = this; scheduleservice.getSchedules( { startTime, endTime, type: 1 }, { success(res) { const schedules = []; let unfinishedCount = 0; res.data.forEach(r => { r.time = moment(r.time.split('T')[0]).format('YYYY-MM-DD dddd'); schedules.push(r); if (!r.finished) unfinishedCount++; }); schedules.sort((a, b) => { return a.finished - b.finished; }); that.setData({ schedules, unfinishedCount }); that.initAnimation(); }, fail(e) { console.log(e); }, }, ); }, onCardClick() { if (this.data.expand) { this.setData({ expand: !this.data.expand }); this.animation.collapse(); } else { this.animation.expand(() => { this.setData({ expand: !this.data.expand }); }); } }, onComplete(e) { const { index } = e.currentTarget.dataset; const s = this.data.schedules[index]; this.setData({ [`schedules[${index}].loading`]: true, }); const that = this; scheduleservice.completeSchedule( { id: s.id }, { success(res) { that.setData({ [`schedules[${index}].loading`]: false, [`schedules[${index}].recordId`]: res.data.srId, }); that.completeAnimation(index); }, fail(e) { that.setData({ [`schedules[${index}].loading`]: false, }); }, complete() {}, }, ); }, onRevoke(e) { const { index } = e.currentTarget.dataset; const s = this.data.schedules[index]; this.setData({ [`schedules[${index}].loading`]: true, }); const that = this; scheduleservice.revokeSchedule( { recordId: s.recordId }, { success(res) { that.setData({ [`schedules[${index}].loading`]: false, }); that.completeAnimation(index); }, fail(e) { that.setData({ [`schedules[${index}].loading`]: false, }); }, complete() {}, }, ); }, completeAnimation(index) { const { schedules } = this.data; const s = schedules[index]; schedules.splice(index, 1); let nextIndex = index; if (s.finished) { schedules.unshift(s); nextIndex = 0; } else { schedules.push(s); nextIndex = schedules.length - 1; } s.finished = !s.finished; this.checkFinishedCount(s.finished); this.animation.slideOut(index, () => { this.setData({ schedules, }); this.animation.slideIn(nextIndex, () => {}); }); }, checkFinishedCount(finished) { let { unfinishedCount } = this.data; unfinishedCount += finished ? -1 : 1; this.setData({ unfinishedCount }); }, }, });