<template>
	<view class="content">
		<view class="box-top">
			<image src="../../static/img/index_bg.jpg" class="index_bg"></image>
		</view>
		<view class="box-notice">
			<u-notice-bar @click="openInfo" :text="list" direction="column" mode="link" color="#9E8F8B"></u-notice-bar>
		</view>
		<view class="box-body">
			<view class="box-body-title">
				<image src="../../static/img/index_name.png" class="box-body-title-img"></image>
				<view class="box-body-sub-title">开馆时间：周二～周日 9:00-17:00 周一闭馆</view>
			</view>
			<view style="margin-top: 56rpx;">
				<view class="box-row">
					<view class="box-col" @click="openBook">
						<image src="../../static/img/index_icon_1.png" class="box-body-icon"></image>
					</view>
					<view class="box-col" @click="openActivity">
						<image src="../../static/img/index_icon_2.png" class="box-body-icon"></image>
					</view>
					<view class="box-col" @click="openPark">
						<image src="../../static/img/index_icon_3.png" class="box-body-icon"></image>
					</view>
					</u-row>
				</view>
			</view>
		</view>
		<view class="box-footer"
			style="position: fixed;bottom: -60rpx;left: 0;text-align: center;width: 100%;z-index: -1;">
			<image src="../../static/img/footer_logo.png" class="footer-img"></image>
		</view>

		<u-popup :show="showBook" mode="bottom" @close="closeBook" :round="10" closeable>
			<view class="mpopup">
				<view class="mpopup-title">
					参观预约
				</view>
				<view class="mpopup-content">
					<view class="book-box">
						<view class="book-box-card">
							<view class="book-box-title">
								日期
							</view>
							<view class="book-box-row">
								<view class="book-box-row-date">
									<block v-for="(item,index) in listDatePrice" :key="index">
										<view class="book-box-row-dateitem" :class="{'book-box-row-dateitem-on':item.checked,
										'book-box-row-dateitem-none':item.is_open==0}" @click="handleDate(index)">
											<view class="book-box-row-dateitem-status">
												<u-icon name="checkmark" color="#fff" size="20rpx" v-if="item.checked">
												</u-icon>
												<text v-else>{{item.is_open==1?'余票':"售罄"}}</text>
											</view>
											<view>{{item.week}}</view>
											<view>{{item.datef}}</view>
										</view>
									</block>
								</view>
								<view class="book-box-row-select" @click="openCalendar">
									<view>
										<view>指定</view>
										<view>日期</view>
									</view>
									<view class="book-box-row-select-more">
										<u-icon name="arrow-right" color="#EF9525" size="20rpx"></u-icon>
									</view>
								</view>
							</view>
						</view>
						<view class="book-box-card">
							<view class="book-box-title">
								参观时间
							</view>

							<view class="book-box-timerow">
								<view class="book-box-row-timeitem" v-for="(item,index) in currentDate.rules"
									:class="{'book-box-row-timeitem-on':item.checked}" @click="handleSelectTime(index)">
									<view class="book-box-row-timeitem-status" v-if="item.checked">
										<u-icon name="checkmark" color="#fff" size="20rpx"></u-icon>
									</view>
									<view class="book-box-row-timeitem-txt">{{item.start_time+'-'+item.end_time}}</view>
									<view class="book-box-row-timeitem-txt">{{item.total}}张</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="book-box-selectInfo">
					<view class="book-box-selectInfo-left">已选：5月6日 09:00-10:00</view>
					<view class="book-box-selectInfo-right">
						<text class="book-box-selectInfo-righttxt">预约须知</text>
						<u-icon name="arrow-right" color="#EF9525" size="20rpx"></u-icon>
					</view>
				</view>
				<view class="mpopup-footer">
					<view style="margin-right: 14rpx;flex: 1;">
						<u-button type="primary" @click="tobook('team')">团体预约</u-button>
					</view>
					<view style="margin-left: 14rpx;flex: 1;">
						<u-button type="primary" @click="tobook('user')">个人预约</u-button>
					</view>
				</view>
			</view>
		</u-popup>
		<u-calendar ref="calendar" :show-confirm="false" :show="showCalendar" :minDate="minDate" :maxDate="maxDate"
			color="#EF9525" round="20" defaultDate="2022-02-15" @confirm="handleSelectDate" @close="closecalendar">
		</u-calendar>
		<u-popup :show="showInfo" mode="center" @close="closeInfo" @open="openInfo" :round="10" closeable>
			<view class="mpopup">
				<view class="mpopup-title">
					通知
				</view>
				<view class="mpopup-content">
					<text>人生若只如初见，何事秋风悲画扇</text>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			const d = new Date()
			const year = d.getFullYear()
			let month = d.getMonth() + 1
			month = month < 10 ? `0${month}` : month
			const date = d.getDate()
			return {
				showInfo: false,
				showBook: false,
				minDate: `${year}-${month}-${date + 1}`,
				maxDate: `${year}-${month}-${date + 7}`,
				showCalendar: false,
				list: [
					'寒雨连江夜入吴',
					'平明送客楚山孤',
					'洛阳亲友如相问',
					'一片冰心在玉壶'
				],
				info: {},
				listDatePrice: [],
				currentDate: {},
				currentIndex: 0,
				listCalendar: []
			}
		},
		onLoad() {
			this.loadInfo()
		},
		onReady() {
			// 如果需要兼容微信小程序的话，需要用此写法
			this.$refs.calendar.setFormatter(this.formatter)
		},
		methods: {
			closecalendar() {
				this.showCalendar = false;
			},
			handleSelectDate(e) {
				console.log(e)
				if (e.length != 0 && e[0] != this.currentDate.date) {
					this.load3Day(e[0]);
					this.showCalendar = false;
				}
			},
			openCalendar() {
				var that = this;

				var edate = this.$moment().add('days', 7).format("yyyy-MM-DD");
				var ndate = this.$moment().add('days', 1).format("yyyy-MM-DD")
				this.loadCalendar(ndate, edate, function(res) {
					that.listCalendar = res;
					that.showCalendar = true;
				});
			},
			formatter(day) {
				for (var m of this.listCalendar) {
					let date = this.$moment(day.date).format("yyyy-MM-DD");
					if (date == m.date) {
						if (m.is_open == 1)
							day.bottomInfo = '余票'
						else {
							day.bottomInfo = '售罄'
						}
					}
				}
				return day
			},
			handleDate(index) {
				var nt = this.$moment().format("yyyy-MM-DD");
				let list = this.listDatePrice;
				let cdate = list[index];
				if (nt == cdate.date) {
					uni.showToast({
						icon: "none",
						title: "请提前一天预约"
					})
					return;
				}
				for (var m of list) {
					m.checked = false;
				}
				cdate.checked = true;
				let i = 0;
				for (var m of cdate.rules) {
					m.checked = false;
					m.checked = i == 0
					i++;
				}

				this.currentDate = cdate;
				this.listDatePrice = list;
				this.$forceUpdate();
			},
			handleSelectTime(index) {
				for (var m of this.currentDate.rules) {
					m.checked = false;
				}
				this.currentDate.rules[index].checked = true;
				this.$forceUpdate();
			},
			openActivity() {
				uni.navigateTo({
					url: "../activity/index"
				})
			},
			openPark() {
				uni.navigateTo({
					url: "/pages/park/index"
				})
			},
			openInfo() {
				this.showInfo = true;
			},
			closeInfo() {
				this.showInfo = false;
			},
			openBook() {
				var that = this;
				var sdate = this.$moment().format("yyyy-MM-DD");
				this.showBook = true;
				this.load3Day(sdate)
			},
			closeBook() {
				this.showBook = false;
			},
			tobook(type) {
				if (type == "user") {
					uni.navigateTo({
						url: "/pages/visit/book"
					})
				} else {
					uni.navigateTo({
						url: "/pages/visit/teambook"
					})
				}
			},
			loadInfo() {
				var that = this;
				this.util.request({
					api: '/api/mobile/visit/introduce',
					utilSuccess: function(res) {
						console.log(res)
					},
					utilFail: function(res) {

					}
				})

			},
			load3Day(sdate) { //加载7天数据
				var that = this;
				that.listDatePrice = [];
				var edate = this.$moment(sdate).add('days', 2).format("yyyy-MM-DD");
				var ndate = this.$moment(sdate).add('days', 1).format("yyyy-MM-DD")
				var nt = this.$moment().format("yyyy-MM-DD");
				var selectDate = (nt == sdate) ? ndate : sdate;
				this.loadCalendar(sdate, edate, function(res) {
					that.listDatePrice = res;
					var i = 0;
					for (var m of that.listDatePrice) {
						m.checked = false;
						m.datef = that.$moment(m.date).format("MM月DD日");
						var week = that.$moment(m.date).format("dddd");
						if (m.date == nt)
							m.week = "今天";
						else if (m.date == ndate) {
							m.week = "明天";
						} else if (m.date == edate) {
							m.week = "后天";
						} else {
							m.week = week;
						}
						if (m.date == selectDate) {
							m.checked = true;
							that.currentDate = m;
							that.currentIndex = i;
							let idx = 0;
							for (var mod of m.rules) {
								mod.checked = false;
								mod.checked = idx == 0
								idx++;
							}
						}
						i++;
					}
				})
			},
			loadCalendar(sdate, edate, cb) {
				var that = this;
				this.util.request({
					api: '/api/mobile/visit/get-calendar',
					data: {
						start_date: sdate,
						end_date: edate
					},
					utilSuccess: function(res) {
						cb(res);
					},
					utilFail: function(res) {

					}
				})
			}
		}
	}
</script>

<style>
	@import url("@/static/css/bookbox.css");





	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.box-row {
		display: flex;
		justify-content: space-around;
		padding: 0rpx 37rpx;
	}

	.box-top {
		width: 100%;
	}

	.index_bg {
		width: 100%;
		height: 613rpx;
	}

	.box-notice {
		width: 100%;
		background-color: #F9F7F3;
		height: 63rpx;
		margin-top: -20rpx;
		border-top-left-radius: 15rpx !important;
		border-top-right-radius: 15rpx !important;
	}

	.u-notice-bar {
		border-top-left-radius: 15rpx !important;
		border-top-right-radius: 15rpx !important;
	}

	.box-body {
		width: 100%;
		padding: 82rpx 0;
	}

	.box-body-title {
		padding: 0rpx 37rpx;
	}

	.box-body-title-img {
		height: 36rpx;
		width: 329rpx;

	}

	.box-body-sub-title {
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #8A8A8A;
		margin-top: 20rpx;
	}

	.box-body-icon {
		width: 217rpx;
		height: 230rpx;
	}

	.footer-img {
		width: 421rpx;
		height: 364rpx;
	}
</style>
