<template>
	<view class="wn-calendar">
		
		<view class="head">
			<image @click="onChange('prev')"
				src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAY5JREFUaEPtmEtugzAQhsc3aW5SbhI2SSOVCJ+g6QmoQqWWbspRcpNyELArQojUVqmw52EswYaNH/PNP57xWEHkn4rcflgAQiu4KBC9AsXxYw2qa6CFRutdIw2ECqGirA4K4GkwWp1s26XSECiAl7KyPzxuobGdSSQhaAF6GmEIegBhCB6AS1xZMInOdifOg80KcBbDqlTvNzUXBDvAEFHwrLPtgQNCBIATQgxghIDW1JRpVhSAA0IcgLpqBwKgu3oEBKCp2mEBCKp2eICxardm5ZOdZgMwiOF+9ZgVQN9T5NkmcanY8wKw0OT77SpaAJ+L32wU8In/czVxkev32D8tpedivsaHB+jbT2VSTNMTTgEC4wMq4J4ub0VnAAXojA+gAK3xsgDK1vnjQ+qZqG5OEwmhqJt6TI6foharAtzGs54BCeN5AIgK1JTwwQMcqy9QcHfdTPhlGg/w+v4JVq3HpxLXZmSql/8bhzrE/cJF+Xbf/zEXMgwIGgCzOcXcBYDCi5g1FgUw3qOYG70C39PtD0Aq3TXKAAAAAElFTkSuQmCC"
				class="arrow left" mode="aspectFit"></image>
			<text class="t">{{headText}}</text>
			<image @click="onChange('next')"
				src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAY5JREFUaEPtmEtugzAQhsc3aW5SbhI2SSOVCJ+g6QmoQqWWbspRcpNyELArQojUVqmw52EswYaNH/PNP57xWEHkn4rcflgAQiu4KBC9AsXxYw2qa6CFRutdIw2ECqGirA4K4GkwWp1s26XSECiAl7KyPzxuobGdSSQhaAF6GmEIegBhCB6AS1xZMInOdifOg80KcBbDqlTvNzUXBDvAEFHwrLPtgQNCBIATQgxghIDW1JRpVhSAA0IcgLpqBwKgu3oEBKCp2mEBCKp2eICxardm5ZOdZgMwiOF+9ZgVQN9T5NkmcanY8wKw0OT77SpaAJ+L32wU8In/czVxkev32D8tpedivsaHB+jbT2VSTNMTTgEC4wMq4J4ub0VnAAXojA+gAK3xsgDK1vnjQ+qZqG5OEwmhqJt6TI6foharAtzGs54BCeN5AIgK1JTwwQMcqy9QcHfdTPhlGg/w+v4JVq3HpxLXZmSql/8bhzrE/cJF+Xbf/zEXMgwIGgCzOcXcBYDCi5g1FgUw3qOYG70C39PtD0Aq3TXKAAAAAElFTkSuQmCC"
				class="arrow" mode="aspectFit"></image>
		</view>
		
		<view class="labels">
			<view class="label-item" v-for="(label) in labels" :key="label">
				<text class="t">{{label}}</text>
			</view>
		</view>
		
		<view :class="['row', isBorder ? 'border' : '']" v-for="row in rows">
			<view @click="onChoose(item)" :class="[
				'day-item', 
				judgeDate(item) ? 'show' : '', 
				active === item.date ? 'active' : ''
			]" v-for="(item, ind) in days.slice(row*7, (row+1)*7)" :key="item.date">
				<text :class="['t', labelClass(item) ? 'labelFont' : '']">{{showLabel(item)}}</text>
				<text v-if="showPrice(item)" class="data">
					<text class="unit" v-if="item.data.text != '免费'">￥</text>
					{{item.data.text}}
				</text>
			</view>
			
		</view>
	</view>
</template>

<script>
	import calendar from './calendar'
	export default {
		name: "w-calendar",
		emits: ['choose', 'change'],
		props: {
			data: {
				type: Array,
				default () {
					return []
				}
			},
			colors: {
				type: Array,
				default () {
					return ['#2979ff', '#18bc37', '#f3a73f', '#e43d33', '#8f939c']
				}
			},
			format: {
				type: String,
				default: ''
			},
			isLess: {
				type: Boolean,
				default: false
			}, 
			isBorder: {
				type: Boolean,
				default: true
			},
			isEn: {
				type: Boolean,
				default: false
			},
			activeDate: {
				type: String,
				default: ''
			},
            beforeDay: {
                type: Number,
                default: 0
            },
            isOrder: {
                type: Number || String,
                default: 0
            }
		},
		computed: {
			headText() {
				if (this.format === '/') {
					return `${this.year}/${this.month}`
				} else {
					if (this.isEn) {
						return `${calendar.getEn(this.month)}, ${this.year}`
					} else {
						return `${this.year}年${this.month}月`
					}
				}
			}
		},
		data() {
			return {
				labels: this.isEn ? calendar.labels_en : calendar.labels_zh,
				active: '',
				rows: [0, 1, 2, 3, 4, 5, 6],
				days: [],
				year: '',
				month: '',
				nowYear: '',
				nowMnth: '',
				nowDate: '',
				nowDay: ''
			};
		},
		created() {
			this.refresh()
		},
		mounted() {
		},
		methods: {
			judgeDate(item) {
				if (!item.data?.text) return false
				return true
			},
			refresh() {
				this.$nextTick(() => {
					const {days, year, month} = calendar.getDays(this.year, this.month, this.data, this.isLess)
					this.days = days
					this.year = year
					this.month = month + 1
					// 获取当前时间
					const today = new Date();
					// 获取当前时间(today)的年份
					this.nowYear = today.getFullYear();
					// 获取月份
					this.nowMnth = today.getMonth() + 1;
					// 获取当前日
					this.nowDate = today.getDate();
					// 得到年月日
					this.nowDay = this.nowYear + '/' + this.nowMnth + '/' + this.nowDate
					this.active = Number(this.activeDate.split('-')[0]) + '/' + Number(this.activeDate.split('-')[1]) + '/' + Number(this.activeDate.split('-')[2])
				})
			},
			labelClass(item) {
				var date = new Date(item.date)
				var now = new Date(this.nowDay);
				if (date.getTime() === now.getTime()) {
					return true
				} else if (date.getTime() === now.getTime() + 24 * 60 * 60 * 1000) {
					return true
				} else {
					return false
				}
			},
			showLabel(item) {
				var date = new Date(item.date)
				var now = new Date(this.nowDay);
				if (date.getTime() === now.getTime()) {
					return '今天'
				} else if (date.getTime() === now.getTime() + 24 * 60 * 60 * 1000) {
					return '明天'
				} else {
					return item.label
				}
			},
			showPrice(item) {
                let day = this.nowDay;
                if (this.isOrder == '1') {
                    day = this.getNumDay(this.beforeDay).substr(0, 10).split('-').join('/')
                }
				var date = new Date(item.date)
                var now = new Date(day)
				if (date.getTime() >= now.getTime()) {
					// console.log("传入时间大于或等于现在的时间")
					if (item.data && item.data.text) {
						return true;
					} else {
						return false;
					}
				} else {
					// console.log("传入时间小于现在的时间")
					return false;
				}
			},
            getNumDay (num) {
                const currentDate = new Date();
                currentDate.setDate(currentDate.getDate() + num);
                return currentDate.toISOString();
            },
			onChange(type) {
				if (type === 'prev' && this.year === this.nowYear && this.month === this.nowMnth) {
					return
				}
				this.month = type === 'prev' ? (this.month - 1) : (this.month + 1)
				this.refresh()
				this.$nextTick(() => {
					this.$emit('change', {
						year: this.year,
						month: this.month
					})
				})
			},
			onChoose(item) {
				if (!this.showPrice(item) || !item.data.text) return;
				this.active = item.date
				this.$nextTick(() => {
					this.$emit('choose', {
						date: item.date,
						data: item
					})
				})
			},
			change(date) {
				this.active = date
			}
		}
	}
</script>

<style scoped>
	.arrow {
		/* #ifndef APP-NVUE */
		box-sizing: border-box;
		/* #endif */
		padding: 4px;
		width: 22px;
		height: 22px;
		background: #f7f7f7;
		border-radius: 22px;
	}

	.arrow.left {
		transform: rotate(180deg);
	}

	.wn-calendar {
		min-width: 294px;
		background: white;
	}

	.head {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 8px 0px;
	}

	.head .t {
		padding: 0 42px;
		font-size: 16px;
		line-height: 32px;
		color: #3a3a3a;
	}

	.labels {
		/* #ifndef APP-NVUE */
		box-sizing: border-box;
		display: flex;
		/* #endif */
		flex-direction: row;
		height: 36px;
		align-items: center;
		background: #f7f7f7;
		border-top: 1px solid #f0f0f0;
		border-bottom: 1px solid #f0f0f0;
	}

	.label-item {
		min-width: 42px;
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		align-items: center;
		justify-content: center;
		/* #endif */
	}

	.label-item .t {
		font-size: 14px;
		text-align: center;
		line-height: 14px;
		color: #3a3a3a;
	}

	.row {
		/* #ifndef APP-NVUE */
		box-sizing: border-box;
		display: flex;
		/* #endif */
		justify-content: space-between;
		flex-direction: row;
		border-bottom: 1px solid transparent;
	}
	
	.row.border {
		border-bottom: 1px solid #f0f0f0;
	}

	.day-item {
		/* #ifndef APP-NVUE */
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		align-items: center;
		/* #endif */
		/* flex: 1; */
		justify-content: center;
		padding: 17rpx 18rpx;
		border-radius: 10rpx;
		height: 92rpx;
		width: 90rpx;
	}

	.day-item.show {}

	.day-item.active {
		background: #01BE69 !important;
	}

	.day-item.active .t {
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		font-size: 32rpx;
		color: #FFFFFF !important;
	}

	.day-item .t {
		font-family: DIN, DIN;
		font-weight: 500;
		font-size: 32rpx;
		color: #999999;
		text-align: center;
	}

	.day-item.show .t {
		color: #333;
	}

	.day-item .labelFont {
		font-size: 24rpx;
	}

	.day-item.active .labelFont {
		font-size: 24rpx;
	}

	.day-item .data {
		font-family: DIN, DIN;
		font-weight: 400;
		font-size: 22rpx;
		color: #FF5833;
		text-align: center;
	}

	.day-item .data .unit {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 18rpx;
		color: #FF5833;
		text-align: center;
	}

	.day-item.active .data {
		color: #fff;
	}

	.day-item.active .data .unit {
		color: #fff;
	}
</style>