<template>
	<!-- 禁止滚动穿透 -->
	<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>
	<view class="main">
		<view class="one" v-if="info.file.length>0">
			<swiper class="swiper" circular :indicator-dots="true" indicator-color="#F5F5F5"
				indicator-active-color="#ffffff" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item class="list" v-for="(item,index) in info.file" :key="index">
					<image class="image" :src="item.url" mode="">
					</image>
				</swiper-item>
			</swiper>
		</view>
		<view class="bottom">
			<view class="two">
				<view class="two_1">
					<view class="name textOne">{{info.name||'暂无'}}</view>
					<view class="collect">
						<uni-fav :checked="info.is_collect" @click="onClick(info)" />
					</view>
				</view>
				<view class="two_2">入住时间：{{info.start_time||'暂无'}}</view>
				<view class="two_2">退房时间：{{info.end_time||'暂无'}}</view>
			</view>
			<view class="thr">
				<view class="thr_1">
					<view class="left">地址：</view>
					<view class="right"><text :user-select='true'>{{info.address||'暂无'}}</text></view>
				</view>
				<view class="thr_1">
					<view class="left">总客房数：</view>
					<view class="right">
						<text>{{info.num||'暂无'}}间</text>
					</view>
				</view>
				<view class="thr_1">
					<view class="left">联系电话：</view>
					<view class="right"><text :user-select='true'>{{info.phone||'暂无'}}</text></view>
				</view>
				<view class="thr_2" v-if="couponList.length>0" @tap="toCoupon">
					<view class="left">优惠卷</view>
					<view class="center">
						<view class="center_1" v-for="(item,index) in couponList" :key="index">
							<text
								v-if="item.discount_type=='min'&&item.discount_config.limit=='0'">立减{{item.discount_config.min}}</text>
							<text
								v-if="item.discount_type=='min'&&item.discount_config.limit!='0'">满{{item.discount_config.limit}}减{{item.discount_config.min}}</text>
						</view>
					</view>
					<view class="right">领卷<text class="iconfont icon-dayuhao"></text></view>
				</view>
			</view>
			<view class="four">
				<uni-collapse>
					<uni-collapse-item title="酒店民宿简介">
						<view class="content">
							<view class="four" v-if="info.prompt">
								<view class="four_1">接待提示</view>
								<rich-text :nodes="formatRichText(info.prompt)"></rich-text>
							</view>
							<view class="four" v-if="info.children">
								<view class="four_1">儿童及加床</view>
								<rich-text :nodes="formatRichText(info.children)"></rich-text>
							</view>
							<view class="four">
								<view class="four_1">简介</view>
								<rich-text :nodes="formatRichText(info.brief)"></rich-text>
							</view>
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
			<view class="five">
				<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
					<view class="list-scroll-view">
						<view class="list" v-for="(item, index) in list" :key="index" @tap="toInfo(item)">
							<image class="image" :src="item.file&&item.file.length>0?item.file[0].url:''"
								mode="aspectFill">
							</image>
							<view class="list_1">
								<view class="name textOne">{{ item.name ||'暂无'}}</view>
								<view class="other other_1">￥{{ item.money ||'暂无'}}</view>
								<view v-if="item.num==0" class="other other_2"><text>当天剩余房量：</text>售罄</view>
								<view v-else class="other other_2"><text>当天剩余房量：</text>
									{{ item.num||'暂无' }} 间
								</view>
								<view class="button">
									<button v-if="item.num==0" size="mini" type="primary">售罄</button>
									<button v-else @tap.stop="toBuy(item)" size="mini" type="primary">预订</button>
								</view>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>{{config.bottom_title}}</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<!-- 领取优惠卷 -->
		<uni-popup ref="coupon" background-color="#fff" type="bottom" :is-mask-click="false" @change="change">
			<view class="coupon">
				<view class="close">
					<text>抵用券</text>
					<text @click="toCouponClose" class="iconfont icon-shanchu"></text>
				</view>
				<view class="info_1">
					<scroll-view scroll-y="true" class="scroll-view">
						<view class="list-scroll-view">
							<view class="list" v-for="(item, index) in discountList" :key="index">
								<view class="list_1">
									<view class="left">
										<view class="left_1">
											￥<text>{{item.discount_config.min}}</text>
										</view>
										<view class="left_2">
											<text v-if="item.discount_config.limit=='0'">无门槛</text>
											<text
												v-else>满{{item.discount_config.limit}}减{{item.discount_config.min}}</text>
										</view>
									</view>
									<view class="center">
										<view class="name textOver">{{item.name}}</view>
										<view class="content">有效日期：({{item.expire_type_label}}) {{item.expire_time}}
										</view>
									</view>
									<view class="right">
										<button v-if="item.is_receive==false" class="button" type="primary"
											@tap="toReceive(item)">立即领取</button>
										<button v-else class="button_1" type="primary" @tap="toUser(item)">去使用</button>
									</view>
								</view>
								<view class="list_2">
									<uni-collapse>
										<uni-collapse-item title="使用规则补充说明" :open="false">
											<view class="content">{{item.brief}}</view>
										</uni-collapse-item>
									</uni-collapse>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import moment from 'moment';
	export default {
		data() {
			return {
				id: '',
				user: {},
				info: {
					file: []
				},
				config: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				// 优惠卷
				couponList: [],
				// 优惠券全部
				discountList: [],
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 禁止滚动穿透
				show: false
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
		},
		onShow: async function(e) {
			const that = this;
			await that.searchOther();
			await that.searchConfig();
			await that.searchToken();
			await that.clearPage();
			await that.search();
		},
		methods: {
			// 禁止滚动穿透
			change(e) {
				const that = this;
				that.show = e.show
			},
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) that.$set(that, `user`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				if (that.id) {
					const data = {}
					if (that.user._id) data.user = that.user._id
					const res = await that.$api(`/hotel/hotel/${that.id}`, 'GET', data)
					if (res.errcode == '0') {
						that.$set(that, `info`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
					let info = {
						skip: that.skip,
						limit: that.limit,
						is_use: '0',
						hotel: res.data._id
					}
					const room = await that.$api(`/room`, 'GET', {
						...info
					})
					if (room.errcode == '0') {
						let list = [...that.list, ...room.data];
						that.$set(that, `list`, list)
						that.$set(that, `total`, room.total)
					} else {
						uni.showToast({
							title: room.errmsg,
						});
					}
				}
			},
			// 领卷
			async toCoupon() {
				const that = this;
				// 优惠券
				const res = await that.$api(`/coupon/specialQuery`, 'GET', {
					is_use: '0',
					user: that.user._id,
					type: '0'
				})
				if (res.errcode == '0') that.$set(that, `discountList`, res.data);
				that.$refs.coupon.open()
			},
			// 领取优惠券
			async toReceive(item) {
				const that = this;
				if (that.user._id) {
					const form = {
						user: that.user._id,
						type: item.type,
						coupon: item._id,
						is_use: '0'
					}
					if (item.expire_type == 'fixed') form.time = item.expire_time
					else {
						form.time = `${moment().format('YYYY-MM-DD HH:mm:ss')} 至 ${moment()
					.add(item.days, 'days')
					.format('YYYY-MM-DD HH:mm:ss')}`;
					}
					const res = await that.$api(`/userCoupon`, 'POST', form);
					if (res.errcode == '0') {
						uni.showToast({
							title: '领取成功',
							icon: 'none'
						})
						that.toCoupon()
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						})
					}
				} else {
					uni.navigateTo({
						url: `/pagesIndex/login/index`
					})
				}
			},
			// 去使用
			toUser(item) {
				uni.navigateTo({
					url: `/pagesMy/coupon/index`
				})
			},
			// 关闭弹框
			toCouponClose() {
				const that = this;
				that.$refs.coupon.close();
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 优惠券
				res = await that.$api(`/coupon/specialQuery`, 'GET', {
					is_use: '0',
					discount_type: 'min',
					user: that.user._id,
					type: '0'
				})
				if (res.errcode == '0') that.$set(that, `couponList`, res.data.slice(0, 3));
			},
			// 处理富文本
			formatRichText(html) {
				if (html) {
					// 富文本内容格式化
					return html && html.replace(/<img[^>]*>/gi, function(match, capture) {
						// 查找所有的 img 元素
						return match.replace(/style=".*"/gi, '').replace(/style='.*'/gi,
							'')
						// 删除找到的所有 img 元素中的 style 属性
					}).replace(/\<img/gi, '<img style="width:100%;"') // 对 img 元素增加 style 属性，并设置宽度为 100%
				}
			},
			// 详情
			toInfo(e) {
				uni.navigateTo({
					url: `/pagesHome/hotel/detail?id=${e.id||e._id}`
				})
			},
			// 预订
			toBuy(item) {
				const that = this;
				if (that.user && that.user._id) {
					console.log(item);
				} else {
					uni.navigateTo({
						url: `/pagesIndex/login/index`
					})
				}
			},
			// 收藏
			async onClick(item) {
				const that = this;
				if (that.user && that.user._id) {
					let res;
					if (item.is_collect == false) {
						const form = {
							user: that.user._id,
							source: item._id,
							source_type: "hotel",
							type: '1',
							create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
						}
						res = await that.$api(`/like`, 'POST', form);
					} else res = await that.$api(`/like/${item.collect}`, 'DELETE', {})
					if (res.errcode == '0') that.search()
				} else {
					uni.navigateTo({
						url: `/pagesIndex/login/index`
					})
				}
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.searchComment();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			// 触底
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 10)
				that.$set(that, `page`, 0)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.one {
			.swiper {
				height: 70vw;

				.list {
					.image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.bottom {
			position: absolute;
			top: 65vw;
			left: 0;
			right: 0;
			background-color: var(--mainColor);
			border-radius: 20px;
			padding: 2vw 0 0 0;
			display: flex;
			flex-direction: column;
			width: 100vw;
			height: 100vh;

			.two {
				padding: 4vw 2vw;

				.two_1 {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.name {
						width: 80vw;
						padding: 1vw 0;
						font-weight: bold;
						font-size: var(--font16Size);
					}

				}

				.two_2 {
					color: var(--f85Color);
					font-size: var(--font12Size);
				}

			}


			.thr {
				padding: 0 2vw;

				.thr_1 {
					display: flex;
					font-size: var(--font14Size);
					padding: 0 0 1vw 0;

					.left {
						padding: 0 1vw 0 0;
						font-weight: bold;
					}

					.right {
						color: var(--f85Color);

						.text_1 {
							font-size: var(--font14Size);
							color: var(--f3CColor);
						}
					}
				}

				.thr_2 {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 3vw 2vw;
					font-size: var(--font14Size);
					background-color: var(--f9Color);
					border-radius: 10px;
					margin: 2vw 0;

					.center {
						display: flex;
						align-items: center;
						width: 65vw;

						.center_1 {
							text {
								margin: 0 1vw 0 0;
								padding: 2px;
								font-size: var(--font12Size);
								color: var(--mainColor);
								border-radius: 5px;
								background: linear-gradient(to right, #FFA500, #FF0000);
							}
						}
					}

					.right {
						display: flex;
						align-items: center;
						font-size: var(--font14Size);
						color: var(--fF0Color);
					}
				}
			}

			.four {
				padding: 0 2vw;

				/deep/.uni-collapse-item__title-box {
					padding: 0 !important;
				}

				.four_1 {
					font-weight: bold;
					font-size: var(--font16Size);
					padding: 0 0 1vw 0;
				}
			}

			.five {
				position: relative;
				flex-grow: 1;
				background-color: var(--f9Color);
				margin: 2vw 0 0 0;

				.list {
					display: flex;
					background-color: var(--mainColor);
					border: 1px solid var(--f5Color);
					padding: 2vw;
					margin: 2vw 2vw 0 2vw;
					border-radius: 5px;

					.image {
						width: 25vw;
						height: 25vw;
						border-radius: 5px;
					}

					.list_1 {
						width: 63vw;
						padding: 0 0 0 2vw;

						.name {
							font-size: var(--font14Size);
							font-weight: bold;
							padding: 0 0 1vw 0;
						}

						.other {
							font-size: var(--font12Size);
							padding: 0 0 2px 0;

							text {
								color: #000;
							}
						}

						.other_1 {
							color: red;
						}

						.other_2 {
							color: var(--f85Color);

						}

						.button {
							text-align: right;

							button {
								font-size: var(--font12Size);
								background-color: var(--f3CColor);
							}
						}
					}
				}
			}
		}
	}

	.uni-popup {
		z-index: 9999 !important;
	}

	.coupon {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 60vh;
		background-color: var(--f9Color);

		.close {
			display: flex;
			justify-content: space-between;
			padding: 2vw;

			text:first-child {
				font-size: var(--font16Size);
				font-weight: bold;
			}
		}

		.info_1 {
			position: relative;
			display: flex;
			flex-direction: column;
			height: 54vh;
			padding: 2vw;

			.list {
				background-color: var(--mainColor);
				border: 1px solid var(--f5Color);
				padding: 2vw;
				margin: 2vw 2vw 0 2vw;
				border-radius: 5px;

				.list_1 {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.left {
						width: 20vw;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						padding: 3vw;
						color: var(--fF0Color);
						font-size: var(--font14Size);

						.left_1 {
							text {
								font-weight: bold;
								font-size: 25px;
							}
						}

						.left_2 {
							font-size: var(--font12Size);
						}
					}

					.center {
						width: 40vw;

						.name {
							font-size: var(--font14Size);
							font-weight: bold;
							margin: 0 0 1vw 0;
						}

						.content {
							color: var(--f85Color);
							font-size: var(--font12Size);
						}
					}

					.right {
						.button {
							font-size: 12px;
							border-radius: 20px;
							background: linear-gradient(to right, #FFA500, #FF0000);
						}

						.button_1 {
							font-size: 12px;
							border-radius: 20px;
							border: 1px solid var(--fF0Color);
							color: var(--fF0Color);
							background: var(--mainColor);
						}
					}
				}

				.list_2 {
					font-size: var(--font12Size);

					/deep/.uni-collapse-item__title-box {
						padding: 0 !important;
						font-size: 12px !important;
					}

					/deep/.uni-collapse-item__title-text {
						font-size: 12px !important;
					}

					.content {
						padding: 2vw;
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		width: 100%;
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}
</style>