<template>
	<!-- 禁止滚动穿透 -->
	<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>
	<view class="main">
		<view class="one">
			<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 config.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">{{config.name||'暂无'}}</view>
				<view class="two_2">
					<view class="left">
						<view class="left_1">
							<text :class="[config.status=='0'?'text_1':'text_2']">{{config.zhStatus||'暂无'}}</text>
							<text class="time">{{config.open_time||'暂无'}}</text>
						</view>
						<view class="left_2">
							<text>有优待政策</text>
							<text>景区电话</text>
							<text>有免费停车</text>
							<text>院内交通</text>
						</view>
					</view>
					<view class="right" @tap="toBrief">简介<text class="iconfont icon-dayuhao"></text></view>
				</view>
				<view class="two_3">
					<view class="left">{{config.address||'暂无'}}</view>
					<view class="right" @tap="toMap">
						<text class="iconfont icon-ditu"></text>
						<text>地图</text>
					</view>
				</view>
				<view class="two_4" 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="thr">
				<view class="thr_1">门票</view>
				<view class="thr_2">
					<text class="text" @tap="toChoose('0')">今日出游</text>
					<text class="text" @tap="toChoose('1')">明日出游</text>
					<text class="text" @tap="toOpen">指定日期<text class="iconfont icon-dayuhao"></text></text>
				</view>
				<view class="thr_3">
					<scroll-view class="scroll-view_H" scroll-x="true">
						<text v-for="(item, index) in totalList" :key="index" @tap="toType(item)"
							:class="[is_type==item._id||is_person==item._id?'text_1':'text']">{{item.label}}</text>
					</scroll-view>
					<view @tap="toSelect">
						筛选
						<uni-icons v-if="is_select" type="top"></uni-icons>
						<uni-icons v-else type="bottom"></uni-icons>
					</view>
				</view>
				<view class="thr_4">
					<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)">
								<view class="left">
									<view class="name textOne">{{item.name||'暂无'}}</view>
									<view class="type textOne">#{{item.zhType||'暂无'}}</view>
									<view class="other textOne">
										已售 {{item.buy_num||0}} | 购买须知
										<text class="iconfont icon-dayuhao"></text>
									</view>
								</view>
								<view class="right">
									<view class="money">
										<text>￥{{item.original_price||'暂无'}}</text>
										<text>￥{{item.money||'暂无'}}起</text>
									</view>
									<view class="button">
										<button @tap.stop="toBuy(item)">预订</button>
									</view>
								</view>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>{{config.bottom_title||'到底了！'}}</text>
						</view>
					</scroll-view>
				</view>
			</view>
		</view>
		<!-- 日历选择 -->
		<uni-calendar ref="calendar" :clear-date="true" :insert="false" :lunar="true" :range="false"
			@confirm="toConfirm" />
		<!-- 筛选门票类型 -->
		<uni-popup ref="popup" type="top" background-color="#fff" @maskClick="toClose" @change="change">
			<view class="popup">
				<view class="one">
					<view class="name">出游人群</view>
					<view class="cotent">
						<view class="list" v-for="(item, index) in personList" :key="index" @tap="toScreen(item,'0')">
							<text :class="[is_person==item._id?'text_1':'text']">{{item.label}}</text>
						</view>
					</view>
				</view>
				<view class="one">
					<view class="name">服务特色</view>
					<view class="cotent">
						<view class="list" v-for="(item, index) in typeList" :key="index" @tap="toScreen(item,'1')">
							<text :class="[is_type==item._id?'text_1':'text']">{{item.label}}</text>
						</view>
					</view>
				</view>
				<view class="button">
					<button class="button_1" type="default" @tap.stop="toReset">重置</button>
					<button class="button_2" type="default" @tap.stop="toSubmit">确定</button>
				</view>
			</view>
		</uni-popup>
		<!-- 领取优惠卷 -->
		<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 {
				type: '',
				searchInfo: {},
				config: {},
				user: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 优惠卷
				couponList: [],
				// 优惠券全部
				discountList: [],
				// 字典表
				statusList: [],
				personList: [],
				typeList: [],
				totalList: [],
				// 是否筛选
				is_select: false,
				is_person: '',
				is_type: '',
				// 禁止滚动穿透
				show: false
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `type`, e && e.type || '');
			uni.setNavigationBarTitle({
				title: e && e.title || '分类'
			});

		},
		onShow: async function(e) {
			const that = this;
			await that.searchToken();
			await that.searchOther();
			await that.searchConfig();
			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) {
						const status = that.statusList.find(i => i.value == res.status)
						if (status) res.zhStatus = status.label
						that.$set(that, `config`, res);
					}
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			// 查询
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
					is_use: '0',
					status: '1'
				}
				const res = await that.$api(`/ticket/queryDate`, 'GET', {
					...info,
					...that.searchInfo
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						const type = that.typeList.find(i => i.value == val.type)
						if (type) val.zhType = type.label
					}
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.name) that.$set(that.searchInfo, `name`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 简介
			toBrief() {
				uni.navigateTo({
					url: `/pagesHome/info/index`
				})
			},
			// 地图
			toMap() {
				uni.reLaunch({
					url: `/pages/map/index`
				})
			},
			// 领卷
			async toCoupon() {
				const that = this;
				// 优惠券
				const res = await that.$api(`/coupon/specialQuery`, 'GET', {
					is_use: '0',
					user: that.user._id,
					type: '1'
				})
				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();
			},
			// 打开日历
			toOpen() {
				const that = this;
				that.$refs.calendar.open()
			},
			// 选择的日期
			toConfirm(e) {
				const that = this;
				that.$set(that.searchInfo, `date`, e.fulldate)
				that.clearPage();
				that.search();
			},
			// 选择当天还是明天
			toChoose(type) {
				const that = this;
				if (type == '0') that.$set(that.searchInfo, `date`, moment().format('YYYY-MM-DD'))
				else that.$set(that.searchInfo, `date`, moment(new Date()).add(1, 'days').format("YYYY-MM-DD"))
				that.clearPage();
				that.search();
			},
			// 选择类型
			toType(item) {
				const that = this;
				if (item.type == 'ticket_person') {
					if (item._id == that.is_person) {
						that.$set(that, `is_person`, '')
						delete that.searchInfo.person
					} else {
						that.$set(that, `is_person`, item._id)
						that.$set(that.searchInfo, `person`, item.value)
					}
				} else {
					if (item._id == that.is_type) {
						that.$set(that, `is_type`, '')
						delete that.searchInfo.type
					} else {
						that.$set(that, `is_type`, item._id)
						that.$set(that.searchInfo, `type`, item.value)
					}
				}
				that.clearPage();
				that.search();
			},
			// 筛选打开弹窗
			toSelect() {
				const that = this;
				that.$set(that, `is_select`, true)
				that.$refs.popup.open()
			},
			// 关闭弹窗
			toClose() {
				const that = this;
				that.$set(that, `is_select`, false)
				that.$refs.popup.close()
			},
			// 筛选
			toScreen(item, type) {
				const that = this;
				if (type == '0') that.$set(that, `is_person`, item._id)
				else that.$set(that, `is_type`, item._id)
			},
			// 重置
			toReset() {
				const that = this;
				that.$set(that, `is_person`, '')
				that.$set(that, `is_type`, '')
				delete that.searchInfo.person
				delete that.searchInfo.type
			},
			// 确定
			toSubmit() {
				const that = this;
				const person = that.personList.find(i => i._id == that.is_person)
				if (person) that.$set(that.searchInfo, `person`, person.value)
				const type = that.typeList.find(i => i._id == that.is_type)
				if (type) that.$set(that.searchInfo, `type`, type.value)
				that.clearPage();
				that.search();
				that.toClose();
			},
			// 详情
			toInfo(e) {
				uni.navigateTo({
					url: `/pagesHome/ticket/info?id=${e.id||e._id}`
				})
			},
			// 预订
			toBuy(item) {
				const that = this;
				if (that.user && that.user._id) {
					uni.navigateTo({
						url: `/pagesIndex/order/order?id=${item.id||item._id}`
					})
				} else {
					uni.navigateTo({
						url: `/pagesIndex/login/index`
					})
				}
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询状态
				res = await that.$api(`/dictData`, 'GET', {
					type: 'config_status',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `statusList`, res.data);
				// 出游人群
				res = await that.$api(`/dictData`, 'GET', {
					type: 'ticket_person',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `personList`, res.data);
				// 服务特色
				res = await that.$api(`/dictData`, 'GET', {
					type: 'ticket_type',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `typeList`, res.data);
				that.$set(that, `totalList`, [...that.personList, ...that.typeList]);
				// 优惠券
				res = await that.$api(`/coupon/specialQuery`, 'GET', {
					is_use: '0',
					discount_type: 'min',
					user: that.user._id,
					type: '1'
				})
				if (res.errcode == '0') that.$set(that, `couponList`, res.data.slice(0, 3));
			},
			// 分页
			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 {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 0 0 2vw 0;

			.swiper {
				height: 60vw;

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

		.bottom {
			position: absolute;
			top: 55vw;
			left: 0;
			right: 0;
			background-color: var(--f9Color);
			border-radius: 20px;
			padding: 2vw;

			.two {
				.two_1 {
					padding: 2vw;
					font-size: var(--font18Size);
					font-weight: bold;
				}

				.two_2 {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 1vw 2vw;

					.left {
						.left_1 {
							font-size: var(--font12Size);

							.time {
								font-weight: 500;
								margin: 0 1vw;
							}

							.text_1 {
								padding: 1px;
								color: var(--mainColor);
								background-color: mediumseagreen;
							}

							.text_2 {
								padding: 1px;
								color: var(--mainColor);
								background-color: red;
							}
						}

						.left_2 {
							font-size: var(--font12Size);
							margin: 2vw 0;

							text {
								margin: 0 2px 0 0;
								padding: 2px 3px;
								border: 1px solid var(--f3CColor);
								color: var(--f3CColor);
								border-radius: 5px;
							}
						}

					}

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

				.two_3 {
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: var(--font13Size);
					padding: 0 2vw;

					.right {
						display: flex;
						flex-direction: column;
						align-items: center;
						font-size: var(--font12Size);
						padding: 0 2vw 0 0;

						.iconfont {
							font-size: 20px;
						}
					}
				}

				.two_4 {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 3vw 2vw;
					font-size: var(--font14Size);
					background-color: var(--mainColor);
					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);
					}
				}
			}

			.thr {
				.thr_1 {
					padding: 2vw 1vw;
					font-size: var(--font16Size);
					font-weight: bold;
				}

				.thr_2 {
					padding: 1vw 0;

					.text {
						margin: 0 5px 0 0;
						padding: 1vw;
						border-radius: 5px;
						font-size: var(--font12Size);
						color: var(--f69Color);
						background-color: var(--mainColor);

						.iconfont {
							font-size: 14px;
						}
					}
				}

				.thr_3 {
					display: flex;
					justify-content: space-between;
					padding: 1vw 0;
					font-size: var(--font15Size);
					margin: 1vw 0 0 0;

					.scroll-view_H {
						white-space: nowrap;
						width: 80vw;
						height: 5vh;

						.text {
							margin: 0 5px 0 0;
							padding: 1vw 2vw;
							border-radius: 5px;
							font-size: var(--font12Size);
							color: var(--f69Color);
							background-color: var(--mainColor);

							.iconfont {
								font-size: 14px;
							}
						}

						.text_1 {
							margin: 0 5px 0 0;
							padding: 1vw 2vw;
							border-radius: 5px;
							font-size: var(--font12Size);
							color: var(--mainColor);
							background-color: var(--f3CColor);

							.iconfont {
								font-size: 14px;
							}
						}
					}

				}

				.thr_4 {
					position: relative;
					height: 82vh;

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

						.left {
							padding: 2vw 0;
							width: 55vw;

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

							.type {
								padding: 0 0 1vw 0;
								color: var(--f3CColor);
								font-size: var(--font12Size);
							}

							.other {
								color: var(--f69Color);
								font-size: var(--font12Size);

								.iconfont {
									font-size: 14px;
								}
							}
						}

						.right {
							width: 28vw;
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							padding: 2vw 0;

							.money {
								text-align: right;
								color: var(--fF0Color);
								font-size: var(--font16Size);
								font-weight: bold;

								text:first-child {
									text-decoration: line-through;
									color: var(--f69Color);
									font-size: var(--font12Size);
									font-weight: 400;
								}
							}

							.button {
								button {
									font-size: var(--font14Size);
									border-radius: 40px;
									color: var(--mainColor);
									background: linear-gradient(to right, #00BFFF, #007AFF);
								}
							}
						}
					}
				}
			}
		}
	}

	.popup {
		padding: 2vw;

		.one {
			padding: 2vw;

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

			.cotent {
				display: flex;
				flex-wrap: wrap;
				margin: 5vw 0;

				.list {
					margin: 0 0 5vw 0;

					.text {
						margin: 0 5px 0 0;
						padding: 2vw 7vw;
						border-radius: 5px;
						font-size: var(--font14Size);
						color: var(--f69Color);
						background-color: var(--f9Color);
					}

					.text_1 {
						margin: 0 5px 0 0;
						padding: 2vw 7vw;
						border-radius: 5px;
						font-size: var(--font14Size);
						color: var(--mainColor);
						background-color: var(--f3CColor);
					}
				}
			}
		}

		.button {
			display: flex;
			padding: 2vw;

			button {
				width: 30vw;
				font-size: var(--font16Size);
				border-radius: 40px;
			}

			button:last-child {
				color: var(--mainColor);
				background: linear-gradient(to right, #00BFFF, #007AFF);
			}
		}
	}

	.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, #0000CD, #007aff);
						}

						.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>