<template>
	<view>
		<view class="calendar">
			<view class="calendar_day">
				<view class="day_x" :style="{'color': (day_index == index ? '#FE3B3C' : '')}"
					v-for="(item, index) in dayArr" :key="index" @click.stop="dayList(item,index)">
					<view class="day_x_a">{{item.weeks}}</view>
					<view class="day_x_b">{{item.days}}</view>
				</view>
			</view>
			<view scroll-y class="calendar_time">
			
				<view class="time_x" :class="host_index == item?.timeStamp ? 'time_x_sty' : ''"
					v-for="(item, index) in timeHostArr[day_index]" :key="index"
					@click="nowTimes < item?.timeStamp ? hosts(item) : ''"
					:style="{'color': (nowTimes > item?.timeStamp ? '#999999' : '')}">{{item?.hours}}</view>
			</view>
		</view>
		<!-- <view class="sub" @click="sub()">
			立即预约
		</view> -->
	</view>
</template>

<script>
	export default {
		props: {
			timeArr: {
				type: Array,
				default: () => []
			},
			
			timeHostArr:{
				type: Array,
				default: () => []
			},
		
		},
		data() {
			return {
				dayArr: [],
				hostArr: [],
				day_index: 0,
				host_index: '',
				host_All: [],
				nowTimes: '',
			}
		},
		watch: {
			timeArr: {
				handler(newVal) {
					
					let dateArr = newVal.map(item => {
						let day = new Date(item)
						const daysOfWeek = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
						return {
							weeks: daysOfWeek[day.getDay()],
							days: item.slice(5)
						}
					})
					
					this.dayArr = dateArr
					
					
					
					// let timeArr = []
					// for (let i = 0; i < 4; i++) {
					// 	// let as = new Date(new Date().toLocaleDateString()).getTime() / 1000
					// 	let as = new Date(new Date().toLocaleDateString()).getTime() / 1000 + i * 60 * 60 * 24
					// 	let staTime = 9 * 60 * 60 + as
					// 	let endTime = 18 * 60 * 60 + as
					// 	let int = 15 * 60
					// 	let timeArr_s = []
						
					// 	console.log(staTime, '>>>>staTime99999');
					// 	for (staTime; staTime < endTime - int; staTime + int) {
					// 		console.log(staTime, '>>>>>staTime');
					// 		staTime = staTime + int
					// 		let hours = this.times(staTime)
					// 		let asb = {
					// 			hours,
					// 			timeStamp: staTime
					// 		}
					// 		timeArr_s.push(asb)
					// 	}
						
					// 	timeArr.push(timeArr_s)
					// }
					
					// console.log(timeArr, '>>>>>>>999>');
					// this.hostArr = timeArr
					
				},
				immediate: true
			}
		},
		mounted() {},
		methods: {
			// 点击日期
			dayList(e, index) {
				this.day_index = index
				this.$emit('getByDate', this.timeArr[index])
			},
			// 点击时间
			hosts(e) {
				this.host_All = e
				this.host_index = e.timeStamp
			},
			// 点击立即预约
			sub() {
				if (this.host_index == '') {
					this.$tool.toast('请选择时间')
				} else {
					let day = this.dayArr[this.day_index]
					let time = this.time(this.host_index)
					let comTime = {
						days: day.days,
						weeks: day.weeks,
						hours: this.host_All.hours,
						timeStamp: this.host_All.timeStamp,
						time: time
					}
					this.$emit('getTime', comTime);
				}
			},
			// 格式化时间
			times(data) {
				let date = new Date(data * 1000);
				//时间戳为10位需*1000，时间戳为13位的话不需乘1000
				let h = date.getHours();
				h = h < 10 ? ('0' + h) : h; //小时补0
				let m = date.getMinutes();
				m = m < 10 ? ('0' + m) : m; //分钟补0
				return h + ':' + m
			},
			time(data, type){
				let date = new Date(data * 1000);
				//时间戳为10位需*1000，时间戳为13位的话不需乘1000
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				let h = date.getHours();
				h = h < 10 ? ('0' + h) : h; //小时补0
				let m = date.getMinutes();
				m = m < 10 ? ('0' + m) : m; //分钟补0
				let s = date.getSeconds();
				s = s < 10 ? ('0' + s) : s; //秒补0
				if (type == 'yymmdd') {
					return y + '-' + MM + '-' + d
				} else if (type == 'hhmmss') {
					return h + ':' + m + ':' + s;
				} else {
					return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F4F4F4;
	}

	.calendar {
		width: 710rpx;
		height: 460rpx;
		background-color: #FFFFFF;
		margin: 20rpx auto 10rpx;
		border-radius: 8rpx;
	}

	.calendar_day {
		display: flex;
		width: 100%;
		height: 120rpx;

		.day_x {
			display: flex;
			flex-flow: column nowrap;
			justify-content: center;
			align-items: center;
			width: 20%;
			height: 100%;
			font-size: 30rpx;
			color: #333333;
		}
	}

	.calendar_time {
		display: flex;
		width: 100%;
		height: 448rpx;
		flex-flow: row wrap;
		align-content: flex-start;
		margin: 20rpx 0;
		overflow-y: auto;

		.time_x {
			display: flex;
			flex-flow: row;
			justify-content: center;
			align-items: center;
			width: 20%;
			height: 54rpx;
			border-radius: 26rpx;
			margin: 10rpx 0;
			font-size: 30rpx;
			color: #333333;
		}

		.time_x_sty {
			background-color: #FFE97B;
			color: #000000 !important;
		}
	}

	.sub {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 710rpx;
		height: 100rpx;
		border-radius: 50rpx;
		margin: 30rpx auto;
		color: #FFFFFF;
		font-size: 36rpx;
		background-color: #FE3B3C;
	}
</style>
