<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)">
								<!-- <web-view :src="item.linkUrl" v-if="item.type=='link'"></web-view> -->
								<img :src="item.picUrl" alt="">
							</swiper-item>
						</swiper>
					</view>
				</view>
			</view>
		</view>
		<!-- swiper结束 -->

		<!-- 分类部分开始 -->
		<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">
			<view class=" sticky-bac">
				<text class="sticky-bac-text1" v-text="item.name"></text>
				<view @click="toCoreRange('tag',item.tagId, item.name)" class="sticky-bac-text2">
					<view></view>
					<view class="flex ">
						<text>更多</text>
						<u-icon name="arrow-right" color="#9c9c9c" size="16"></u-icon>
					</view>
				</view>
			</view>
			<view class="Seckill-bom">
				<view class="Seckill-bom-grid">
					<navigator :url="`/pages/goods/detail?id=${ itemExc.goodsId }`" class="core-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></view>
				</view>
			</view>
		</view>
		<!-- 限时秒杀部分结束 -->


		<!-- 精选商品部份开始 -->
		<u-sticky offset-top="60" class="">
			<view class="screen-coin uSticky-toTop">
				<view class="screen-coin-shopin">精选商品</view>
				<view class="flex screen-coin-select">
					<view @click="ascSelect" class="flex">
						<view class="title">
							<text>{{ ascListsingle.text}}</text>
						</view>
						<u-icon name="arrow-up" color="#333" size="18" v-if="ascShow"></u-icon>
						<u-icon name="arrow-down" color="#333" size="18" v-else></u-icon>
					</view>
					<view @click="openSelect" class="flex">
						<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="#333" size="18" v-if="coinShow"></u-icon>
						<u-icon name="arrow-down" color="#333" 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="`/pages/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"></u-overlay>
		<u-overlay :show="ascShow" @click="ascShow = false" zIndex="1"></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: 4500,
				duration: 2000,
				imgTop: [],
				goods: [],
				exclusive: [],
				exclusiveSingle: [],

				pageNum: 1,
				total: 0,
				list: [],
				coinShow: false,
				coinList: [{
						min: null,
						max: null,
						desc: '全部价格'
					},
					{
						min: 1,
						max: 20,
						desc: '盲豆'
					},
					{
						min: 21,
						max: 40,
						desc: '盲豆'
					},
					{
						min: 41,
						max: 60,
						desc: '盲豆'
					},
					{
						min: 61,
						max: 80,
						desc: '盲豆'
					},
					{
						min: 81,
						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,
			};
		},
		onShow() {

		},
		onLoad() {
			this.pageList()
			this.actionInfo = this.coinList[this.actionIndex]
			this.ascListsingle = this.ascList[this.ascListIndex]
			this.getSwiper()
			this.getListIficationBox()
			this.getExclusive()
		},
		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") {
					// web-view
				} else {
					uni.navigateTo({
						url: `/pages/coreRange/coreRange?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]
					})
					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]
					})
				})
				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]
							})

							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
				} else {
					this.coinShow = false
				}
			},
			
			//排序切换
			ascSelect() {
				if (this.ascShow == false) {
					this.ascShow = true
					this.coinShow = false
				} 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('没有更多数据了')
			}
		},
	}
</script>

<style lang="scss" scoped>
	.swiper-ban {
		background-color: #FFFFFF;
		padding: 0 3px;

		.uni-padding-wrap {
			border-radius: 5px;
			overflow: hidden;
		}
	}

	.uni-padding-wrap img {
		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-bac {
		margin-top: 10px;
		background-color: #FFFFFF;
		padding: 0 3px;

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

		;

		.sticky-bac-text2 {
			width: 128px;
			height: 40px;
			text-align: right;
			line-height: 40px;
			color: #9c9c9c;
			float: right;
			display: grid;
			grid-template-columns: 1fr 1fr;
		}
	}

	.Seckill-bom {
		padding: 0 0.3rem;
		// margin-bottom: 10px;
		background-color: #FFFFFF;

		.Seckill-bom-grid {
			min-height: 550rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			padding: 20rpx 15rpx;
			margin-bottom: 10px;
		}
	}

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


	//================
	.screen-coin {
		// position: fixed;
		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: 40rpx;
				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;
			// padding-top: 96rpx;

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