import {Component, OnInit, EventEmitter} from '@angular/core'; import {DialogRef, ModalComponent, CloseGuard} from 'ngx-modialog'; import {BSModalContext} from 'ngx-modialog/plugins/bootstrap'; import UtilService from "@common/services/util/index"; declare var $: any; declare var moment: any; export class CusDateTimeContext extends BSModalContext { constructor(private args: any = {}) { super(); /* * 点击背景是否隐藏模态框 * @value true 不隐藏 * @value false 隐藏 * */ this.isBlocking = true; /* * 模态框位置 * @value modal-top 居顶 * */ this.dialogClass = 'modal-dialog modal-top cusDateTime'; } } @Component({ selector: 'cusDateTime', templateUrl: './template.html', styleUrls: ['./style.less'] }) export default class CusDateTime implements OnInit, ModalComponent { private data: any = {}; private otherData: any = { prices: {} }; private day: any = {}; context: any = {}; constructor(public dialog: DialogRef, private utilService: UtilService) { this.context = dialog.context; } ngOnInit(): void { this.data = $.extend(true, {}, this.context.args.item); this.otherData.createTime = moment(this.data.createdAt).hour(0).minute(0).seconds(0); this.day = this.otherData.createTime.clone(); this.otherData.startTime = this.otherData.createTime.clone().add(this.data.startTime, 'days'); this.otherData.endTime = this.otherData.createTime.clone().add(this.data.startTime + this.data.serveDays, 'days'); this.initDays(this.day); } initDays(dayMoment) { this.otherData.yearMonth = dayMoment.format('YYYY-MM'); const daysArr = [[], [], [], [], [], []]; // 6*7的日历数组 const currentWeekday = moment(dayMoment).date(1).weekday() || 7; // 获取当月1日为星期几 const lastMonthDays = moment(dayMoment).subtract(1, 'months').daysInMonth(); // 获取上月天数 const currentMonthDays = moment(dayMoment).daysInMonth(); // 获取当月天数 const getData = (i, j) => { const currentNumber = i * 7 + (j + 1); if (i == 0 && j < currentWeekday - 1) { const day = lastMonthDays - currentWeekday + 2 + j; const date = dayMoment.clone().subtract(1, 'months').date(day); return { day, date, input: date.isBetween(this.otherData.startTime, this.otherData.endTime) || date.isSame(this.otherData.startTime), type: 'prevMonth', price: this.otherData.prices[date.format('YYYY-MM-DD')] } } const currentDay = currentNumber - (currentWeekday - 1); if (currentDay <= currentMonthDays) { const day = currentNumber - (currentWeekday - 1); const date = dayMoment.clone().date(day); return { day, date, input: date.isBetween(this.otherData.startTime, this.otherData.endTime) || date.isSame(this.otherData.startTime), type: 'currentMonth', price: this.otherData.prices[date.format('YYYY-MM-DD')] } } const day = currentDay - currentMonthDays; const date = dayMoment.clone().add('1', 'months').date(day); return { day, date, input: date.isBetween(this.otherData.startTime, this.otherData.endTime) || date.isSame(this.otherData.startTime), type: 'nextMonth', price: this.otherData.prices[date.format('YYYY-MM-DD')] } }; for (let i = 0; i < 6; i++) { for (let j = 0; j < 7; j++) { daysArr[i][j] = getData(i, j); } } this.otherData.days = daysArr; } changePrice(value, day) { day.price = value; this.otherData.prices[day.date.format('YYYY-MM-DD')] = value; } nextMonth() { this.day = this.day.add(1, 'months'); this.initDays(this.day); } prevMonth() { this.day = this.day.subtract(1, 'months'); this.initDays(this.day); } submit() { const date = Object.keys(this.otherData.prices).map(item => { return { date: item, price: this.otherData.prices[item] } }); this.otherData.days.map(week => { week.map(day => { if (day.input && day.price) { } }) }); this.context.args.callback(date); this.dialog.close(); } }