<template>
	<view class="core-cla">
		<u-navbar leftIconSize="0" :placeholder="true" bgColor="#ffffff">
			<view class="u-nav-slot" slot="left">
				<text>兑换大厅</text>
			</view>
		</u-navbar>
		<!-- swiper开始 -->
		<view class="swiper-ban" v-if="imgTop.length">
			<view class="uni-padding-wrap">
				<view class="page-section swiper">
					<view class="page-section-spacing">
						<swiper class="swiper" :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
							:duration="duration">
							<swiper-item v-for="(item,index) in imgTop" :key="index"
								@click="toCoreRange(item.type,item.type=='link'?item.linkUrl:item.goodsTags, item.name)">
								<image :src="item.picUrl" alt="">
							</swiper-item>
						</swiper>
					</view>
				</view>
			</view>
		</view>
		<!-- 分类部分开始 -->
		<view class="classificationBox">
			<view v-for="(item,index) in goods" :key="index" @click="toCoreRange('category',item.categoryId, item.name)"
				class="ificationBox-mar">
				<img :src="item.picUrl" alt="">
				<view v-text="item.name" style=" "></view>
			</view>
		</view>
		<!-- 限时秒杀部分开始 -->
		<view v-for="(item,index) in exclusiveSingle" :key="index" class="sticky-wrap">
			<view class=" sticky-nav">
				<text class="sticky-nav-name" v-text="item.name"></text>
				<view @click="toCoreRange('tag',item.tagId, item.name)" class="sticky-nav-all">
					<view></view>
					<view class="flex ">
						<text>更多</text>
						<u-icon name="arrow-right" color="#9c9c9c" size="16"></u-icon>
					</view>
				</view>
			</view>
			<view class="sticky-content-wrap">
				<view class="flex sticky-content-list">
					<navigator :url="`/packageGoods/goods/detail?id=${ itemExc.goodsId }`" class="sticky-content-list-item"
						hover-class="navigator-hover" v-for="(itemExc, indexNum) in item.data" :key="indexNum">
						<view class="flex iamge-wrap">
							<image :src="itemExc.picUrl" mode=""></image>
						</view>
						<view class="title">{{ itemExc.title }}</view>
						<view class="bean">
							<image src="../../static/icon/bean.png" mode="aspectFill"></image>
							<view>× {{ itemExc.exchangePrice }}</view>
						</view>
					</navigator>
					<view class="sticky-content-list-item"></view>
				</view>
			</view>
		</view>
		<!-- 精选商品筛选 -->
		<u-sticky class="offset-Height" :customNavHeight="navHeight" @touchmove.prevent.stop>
			<view class="screen-coin uSticky-toTop">
				<view class="screen-coin-shopin">精选商品</view>

				<view class="flex screen-coin-select">
					<view @click="ascSelect" class="flex screen-coin-select-text">
						<view class="title">
							<text>{{ ascListsingle.text}}</text>
						</view>
						<u-icon name="arrow-up" color="#848484" size="18" v-if="ascShow"></u-icon>
						<u-icon name="arrow-down" color="#848484" size="18" v-else></u-icon>
					</view>
					<view class="screen-coin-select-line"></view>
					<view @click="openSelect" class="flex screen-coin-select-text screen-coin-select-text-two">
						<view class="title">
							<text v-if="actionInfo.min">{{ actionInfo.min }}</text>
							<text v-if="actionInfo.max">-{{ actionInfo.max }}</text>
							<text>{{ actionInfo.desc }}</text>
						</view>
						<u-icon name="arrow-up" color="#848484" size="18" v-if="coinShow"></u-icon>
						<u-icon name="arrow-down" color="#848484" size="18" v-else></u-icon>
					</view>
				</view>
				<view class="screen-coin-list" v-if="ascShow">
					<view class="flex screen-coin-list-item" v-for="(item, index) in ascList" :key="index"
						@click="selectCoinTwo(item, index)">
						<text :class="{ 'action': ascListIndex == index }">{{ item.text }}</text>
					</view>
				</view>

				<view class="screen-coin-list" v-if="coinShow">
					<view class="flex screen-coin-list-item" v-for="(item, index) in coinList" :key="index"
						@click="selectCoin(item, index)">
						<text v-if="item.min" :class="{ 'action': actionIndex == index }">{{ item.min }}</text>
						<text v-if="item.max" :class="{ 'action': actionIndex == index }">-{{ item.max }}</text>
						<text :class="{ 'action': actionIndex == index }">{{ item.desc }}</text>
					</view>
				</view>
			</view>
		</u-sticky>
		<!-- 精选商品列表 -->
		<view class="core">
			<view class="flex core-list">
				<navigator :url="`/packageGoods/goods/detail?id=${ item.goodsId }`" class="core-list-item"
					hover-class="navigator-hover" v-for="(item, index) in list" :key="index">
					<view class="flex iamge-wrap">
						<image :src="item.picUrl" mode=""></image>
					</view>
					<view class="title">{{ item.title }}</view>
					<view class="bean">
						<image src="../../static/icon/bean.png" mode="aspectFill"></image>
						<view>× {{ item.exchangePrice }}</view>
					</view>
				</navigator>
				<view class="core-list-item"></view>
			</view>
			<view class="flex empty" v-if="!list.length">
				<u-empty text="数据为空" mode="order" />
			</view>
		</view>
		<custom-tab-bar :activeValue="'core'" />
		<!-- 筛选盲豆 -->
		<u-overlay :show="coinShow" @click="coinShow = false" zIndex="1" @touchmove.prevent.stop></u-overlay>
		<!-- 筛选排序 -->
		<u-overlay :show="ascShow" @click="ascShow = false" zIndex="1" @touchmove.prevent.stop></u-overlay>

		<custom-tab-bar :activeValue="'core'" />
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import CustomTabBar from '../../components/custom-tab-bar/custom-tab-bar.vue'
	import $http from '@/utils/request.js'
	export default {
		components: {
			CustomTabBar,
		},
		data() {
			return {
				indicatorDots: true,
				autoplay: true,
				interval: 3000,
				duration: 500,
				imgTop: [],
				goods: [],
				exclusive: [],
				exclusiveSingle: [],

				pageNum: 1,
				total: 0,
				list: [],
				coinShow: false,
				coinList: [{
						min: null,
						max: null,
						desc: '全部盲豆'
					},
					{
						min: 1,
						max: 100,
						desc: '盲豆'
					},
					{
						min: 101,
						max: 200,
						desc: '盲豆'
					},
					{
						min: 201,
						max: 500,
						desc: '盲豆'
					},
					{
						min: 501,
						max: 1000,
						desc: '盲豆'
					},
					{
						min: 1001,
						max: 5000,
						desc: '盲豆'
					},
					{
						min: 5000,
						max: null,
						desc: '盲豆以上'
					},
				],
				actionInfo: {},
				actionIndex: 0,
				coinNum: {
					startPrice: null,
					endPrice: null
				},
				orderByColumn: '', //默认exchangePrice
				isAsc: '', //从低到高asc isAsc:'desc'从高到低
				ascShow: false,
				ascList: [{
						data: '',
						text: '默认排序'
					},
					{
						data: 'asc',
						text: '价格从低到高'
					},
					{
						data: 'desc',
						text: '价格从高到低'
					},
				],
				ascListsingle: {},
				ascListIndex: 0,

				// 距顶部位置
				navHeight: 0,
			};
		},
		onLoad() {
			this.pageList()
			this.actionInfo = this.coinList[this.actionIndex]
			this.ascListsingle = this.ascList[this.ascListIndex]
			this.getSwiper()
			this.getListIficationBox()
			this.getExclusive()

			uni.getSystemInfo({
				success: (data) => {
					let custom = wx.getMenuButtonBoundingClientRect()
					//导航栏高度
					this.navHeight = custom.bottom + custom.top - data.statusBarHeight -6
				},
			})
		},
		methods: {
			changeIndicatorDots(e) {
				this.indicatorDots = true
			},

			changeAutoplay(e) {
				this.autoplay = true
			},

			intervalChange(e) {
				this.interval = e.target.value
			},

			durationChange(e) {
				this.duration = e.target.value
			},

			// 跳转点击事件
			toCoreRange(type, data, name) {
				if (type == "link") {
					uni.navigateTo({
						url: `/packageOperate/webview/index?url=${ data }`,
					})
				} else {
					uni.navigateTo({
						url: `/packageGoods/core/index?type=${ type }&data=${ data }&name=${ name }`,
					})
				}
			},

			//轮播图
			getSwiper() {
				$http.post(`/api/v1/mp/user/exchange/banner/list`, {
					noToken: true,
					location: "top"
				}).then(res => {
					res.data.forEach(item => {
						let picUrlArr = item.picUrl.split(',')
						item.picUrl = env.filePublic + picUrlArr[0]
					})
					this.imgTop = this.imgTop.concat(res.data)
				})
			},

			// 分类列表
			getListIficationBox() {
				$http.post(`/api/v1/mp/user/exchange/category/list`, {
					noToken: true
				}).then(res => {
					res.data.forEach(item => {
						let picUrlArr = item.picUrl.split(',')
						item.picUrl = env.filePublic + picUrlArr[0] + '?imageView2/2/w/340'
					})
					this.goods = this.goods.concat(res.data)
				})
			},

			// 秒杀
			async getExclusive() {
				let resData = await $http.post(`/api/v1/mp/user/exchange/activity/list`, {
					noToken: true
				})
				let goodsList = []
				for (let item of resData.data) {

					let resDatas = await $http.post(`/api/v1/mp/user/exchange/goods/list?pageNum=1&pageSize=4`, {
						noToken: true,
						tagIds: item.tagId
					})
					goodsList.push({
						data: resDatas.rows,
						name: item.name,
						tagId: item.tagId
					})
				}
				goodsList.forEach(item => {
					item.data.forEach(items => {
						items.picUrl = env.filePublic + items.picUrl.split(',')[0] + '?imageView2/2/w/340'
					})
				})
				this.exclusiveSingle = goodsList
			},


			//精选商品部分
			getList() {
				uni.showLoading({
					title: '加载中'
				});
				let data = {
					categoryId: this.categoryId,
					tagIds: this.tagIds,
					...this.coinNum,
					noToken: true
				}
				$http.post(
					`/api/v1/mp/user/exchange/goods/list?pageNum=${this.pageNum}&pageSize=20&orderByColumn=${this.orderByColumn}&isAsc=${this.isAsc}`,
					data).then(
					res => {
						uni.hideLoading();
						if (res.code == 0) {
							res.rows.forEach(item => {
								let picUrlArr = item.picUrl.split(',')
								item.picUrl = env.filePublic + picUrlArr[0] + '?imageView2/2/w/340'
							})

							this.total = res.total
							this.list = this.list.concat(res.rows)
						}
					}).catch(() => {
					uni.hideLoading();
				})
			},

			pageList() {
				this.pageNum = 1
				this.total = 0
				this.list = []
				this.getList()
			},

			pageListTwo() {
				this.pageNum = 1
				this.total = 0
				this.list = []
				this.getList()
			},

			openSelect() {
				if (this.coinShow == false) {
					this.coinShow = true
					this.ascShow = false

					uni.createSelectorQuery().select(".core-cla").boundingClientRect((res) => {
						uni.createSelectorQuery().select(".screen-coin-select-text-two").boundingClientRect((
							data) => {
							uni.pageScrollTo({
								duration: 0,
								scrollTop: -res.top + data.top + 350,
							})
						}).exec()
					}).exec()
				} else {
					this.coinShow = false
				}
			},

			//排序切换
			ascSelect() {
				if (this.ascShow == false) {
					this.ascShow = true
					this.coinShow = false

					uni.createSelectorQuery().select(".core-cla").boundingClientRect((res) => {
						uni.createSelectorQuery().select(".screen-coin-select-text").boundingClientRect((data) => {
							uni.pageScrollTo({
								duration: 0, //过渡时间必须为0，uniapp bug，否则运行到手机会报错
								scrollTop: -res.top + data.top,
							})
						}).exec()
					}).exec()

				} else {
					this.ascShow = false
				}
			},

			selectCoin(item, index) {
				this.actionIndex = index
				this.actionInfo = this.coinList[this.actionIndex]
				this.coinShow = false
				this.coinNum.startPrice = item.min
				this.coinNum.endPrice = item.max
				this.pageList()
			},

			selectCoinTwo(item, index) {
				this.ascListIndex = index
				this.ascListsingle = this.ascList[this.ascListIndex]
				this.ascShow = false

				if (item.data == '') {
					this.orderByColumn = ''
					this.isAsc = ''
				} else {
					this.isAsc = item.data
					this.orderByColumn = 'exchangePrice'
				}

				this.pageListTwo()
			},
		},

		//精选商品
		onReachBottom() {
			// 判断是否有数据
			if (this.total > this.pageNum * 20) {
				setTimeout(() => {
					++this.pageNum
					this.getList()
				}, 500)
			} else {
				uni.$u.toast('已经到底了')
			}
		},
		onShareAppMessage(res) {
			return {
				title: '盲票，玩的就是有趣',
				path: '/pages/index/index',
				// imageUrl:'../../static/icon/lucky_bg.png',
			}
		}
	}
</script>
<style lang="scss" scoped>
	.u-nav-slot {
		text {
			font-size: 32rpx;
		}
	}
</style>
<style lang="scss" scoped>
	.swiper-ban {
		background-color: #FFFFFF;
		padding: 0 3px;

		.uni-padding-wrap {
			width: 95%;
			margin: 0 auto;
			border-radius: 10rpx;
			overflow: hidden;
		}
	}

	.uni-padding-wrap image {
		width: 100%;
		height: 100%;
	}

	.classificationBox {
		background-color: #FFFFFF;
		display: grid;
		grid-template-columns: 1fr 1fr 1fr 1fr;
		font-size: 12px;

		.ificationBox-mar {
			margin: 0 auto;
			text-align: center;
			margin-top: 10px;
			margin-bottom: 10px;
		}
	}

	.ificationBox-mar img {
		width: 44px;
		height: 44px;
		border-radius: 29px;
	}

	.sticky-wrap {

		.sticky-nav {
			border: 1px;
			border-radius: 20rpx 20rpx 0 0;
			background-color: #FFFFFF;
			margin: 20rpx 20rpx 0 20rpx;


			.sticky-nav-name {
				color: #2f2f2f;
				font-size: 18px;
				line-height: 25px;
				display: inline-block;
				padding: 8px;
			}

			.sticky-nav-all {
				width: 128px;
				height: 40px;
				text-align: right;
				line-height: 40px;
				color: #848484;
				float: right;
				display: grid;
				grid-template-columns: 1fr 1fr;
			}
		}

		.sticky-content-wrap {
			margin: 0 20rpx;
			border-radius: 0 0 20rpx 20rpx;
			overflow: hidden;

			.sticky-content-list {
				width: 100%;
				background-color: #FFFFFF;
				justify-content: space-around;
				flex-wrap: wrap;
				padding-top: 20rpx;

				&-item {
					box-sizing: border-box;
					width: 320rpx;
					height: 460rpx;
					background-color: #FFFFFF;
					border-radius: 10rpx;
					margin-bottom: 30rpx;

					.iamge-wrap {
						image {
							border-radius: 10rpx;
							width: 320rpx;
							height: 320rpx;
							margin-bottom: 6rpx;
						}
					}

					.title {
						padding: 0 14rpx;
						font-size: 28rpx;
						;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
					}

					.bean {
						padding: 0 14rpx;
						display: flex;
						align-items: center;
						font-size: 32rpx;
						line-height: 44rpx;
						color: rgba(235, 112, 9, 100);
						margin-top: 12rpx;

						image {
							width: 42rpx;
							height: 42rpx;
							margin-right: 16rpx;
						}
					}
				}

				&-item:last-child {
					padding: 0;
					height: 0;
				}
			}
		}

		.sticky-content-wrap:last-child {
			margin-bottom: 20rpx;
		}

	}

	.screen-coin-shopin {
		color: #2f2f2f;
		font-size: 36rpx;
		line-height: 16rpx;
		display: inline-block;
		padding: 18px 8px 0px 8px;
	}

	.screen-coin-select-text {
		color: #848484;
	}

	.screen-coin-select-line {
		border-right: 3rpx solid #848484;
		width: 26rpx;
		height: 26rpx;
	}

	.screen-coin {
		width: 100%;
		z-index: 10;
		background-color: #FFFFFF;
		border-top: 1px solid #F8F8F8;
		box-shadow: 0 4rpx 10rpx 0 rgba(151, 151, 151, 0.24);

		&-select {
			height: 86rpx;
			line-height: 86rpx;
			width: 100%;
			justify-content: flex-start;

			.title {
				padding-left: 20rpx;
				margin-right: 10rpx;
			}
		}

		&-list {
			width: 100%;
			background-color: #FFFFFF;
			border-top: 1px solid #F8F8F8;

			&-item {
				height: 86rpx;
				line-height: 86rpx;
				background-color: #FFFFFF;
				border-bottom: 1px solid #F8F8F8;
			}

			&-item:last-child {
				border: 0;
			}

			.action {
				color: $uni-text-color;
			}
		}
	}

	.core {
		margin: 20rpx 0;

		&-list {
			justify-content: space-evenly;
			flex-wrap: wrap;

			&-item {
				box-sizing: border-box;
				width: 340rpx;
				height: 502rpx;
				background-color: #FFFFFF;
				border-radius: 10rpx;
				margin-bottom: 30rpx;

				.iamge-wrap {
					image {
						border-top-left-radius: 10rpx;
						border-top-right-radius: 10rpx;
						width: 340rpx;
						height: 340rpx;
						margin-bottom: 6rpx;
					}
				}

				.title {
					padding: 0 14rpx;
					font-size: 28rpx;
					// line-height: 40rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
				}

				.bean {
					padding: 0 14rpx;
					display: flex;
					align-items: center;
					font-size: 32rpx;
					line-height: 44rpx;
					color: rgba(235, 112, 9, 100);
					margin-top: 12rpx;

					image {
						width: 42rpx;
						height: 42rpx;
						margin-right: 16rpx;
					}
				}
			}

			&-item:last-child {
				padding: 0;
				height: 0;
			}
		}

		.empty {
			height: 60vh;
		}
	}
</style>
<style lang="scss" scoped>
</style>
