<template>
	<view class="discountsDetail">
		<zs-choose-tab :tabList="tabList" @choose="chooseTab"></zs-choose-tab>

		<!-- 内容 -->
		<zs-list class="store-box" mt="0" @load="loadMore" :status="status">
			<view class="item" v-for="(item,index) in list" :key="index" @click="goDetail(item)">
				<!-- <image :src="item.src" mode="" class="icon" @click="goDetail(item)"></image> -->
				<view class="box">
					<zs-img :src="item.logoPath" width="200rpx" height="200rpx" radius="full"
						></zs-img>
					<view class="info">
						<view class="title">
							{{item.shopVo.shopName}}
						</view>
						<view class="desc">
							{{item.address}}
						</view>
						<view class="desc">
							距离你{{(item.shopVo.distance/1000).toFixed(2)}}km
						</view>
					</view>
				</view>
				
				<view class="goods-item" :class="[d != 0?'none':'']" v-for="(i,d) in filterData(item.goodsVos)" :key="d">
						<view class="tag" v-if="i.goodsLabel">
							{{i.goodsLabel}}
						</view>
						
					<view class="price-box">
							<view class="unit">
								￥
							</view>
							<view class="price">
								{{i.realPrice}}
							</view>
							<view class="discount-tag" v-if="i.marketPrice">
								<view class="discount-num">
									{{((i.realPrice/i.marketPrice)*10).toFixed(1)}}折
								</view>
							</view>
							<view class="old-price">
								 ￥{{i.marketPrice}}
							</view>
					</view>
					<view class="goods-name">
						{{i.goodsName}}
					</view>
				</view>

			</view>
		</zs-list>



	</view>
</template>

<script>
	import {
		search
	} from '@/api/shop.js';
	import {
		getMenu
	} from '@/api/common.js'
	export default {
		options: {
			styleIsolation: 'shared'
		},
		data() {
			return {
				longitude: '',
				latitude: '',
				destination: '',
				_mapContext: null,
				status: 'more',
				list: [],
				btnText: '去使用',
				banner: '',
				menuId:'',
				query: {
					district:'',
					menuId: '',
					queryName: '',
					range:'',
					'location.lat': 0,
					'location.lon': 0,
					pageCurrent: 1,
					pageSize: 100
				},
				location:'',
				tabList: [{
						id: 1,
						label: '区域',
						list: [

							{
								id: 1,
								label: '观山湖'
							},
							{
								id: 2,
								label: '白云区'
							},
							{
								id: 3,
								label: '花果山区'
							}
						]
					},
					{
						id: 1,
						label: '全部分类',
						list: [{
								label: '甜点饮品'
							},
							{
								label: '特色美食'
							},
							{
								label: '风味小吃'
							}
						]
					},
					
					{
						id: 1,
						label: '附近',
						list: [
							{
								id:5,
								label: '5KM'
							},
							{
								id:10,
								label: '10KM'
							},
							{
								id:20,
								label: '20KM'
							},
							{
								id:50,
								label: '50KM'
							}
						]
					},
				]
			}
		},
		methods: {
			// 筛选前两个商品
			filterData(data){
				return data.slice(0,2)
			},
			// 金刚区
			getMenu(parentId) {
				return new Promise((resolve, reject) => {
					getMenu({
						currentPage: 1,
						pageSize: 10,
						status: 2,
						parentId
					}).then(res => {
						if (res.state == 'Success') {
							let list = []
							res.content.records.map(item => {
								list.push({
									id: item.id,
									label: item.menuName
								})
							})
							this.tabList[1].list = list
							console.log(this.tabList);
						}
					})
				})
			},
			chooseTab(val) {
				this.query.range = val[2].id
				if(val[0]){
					// this.query['location.lat'] = val[0].location.lat
					// this.query['location.lon'] = val[0].location.lng
					this.query.range = 50
					this.query.district = val[0].label
				}else if(!val[0]){
					this.query['location.lat'] = this.location.latitude
					this.query['location.lon'] = this.location.longitude
					this.query.district = ''
				}
				if(val[1]){
					this.query.secondMenuId =+val[1].id
				}else if(!val[1]){
				  delete this.query.secondMenuId
				}
				this.query.pageCurrent = 1
				this.status = 'more'
				this.list = []
				this.search()
				console.log(this.query.menuId);
			},
			goDetail(item) {
				let shopInfo = {
					shopId:item.shopVo.shopId,
					shopName:item.shopVo.shopName,
					detailContent:item.detailContent,
					logoPath:item.logoPath,
				}
				uni.setStorageSync('shopInfo', JSON.stringify(shopInfo))
				uni.navigateTo({
					url: '../shopDetail/shopDetail'
				})
			},
			loadMore() {
				this.search()
			},
			search() {
				return new Promise((resolve, reject) => {
					if (this.status == 'noMore') return
					this.status = 'loading'
					let obj = JSON.parse(JSON.stringify(this.query))
					if(!obj.range){
						delete obj.range
					}
					search(obj).then(res => {
						if (res.state == 'Success') {
							this.list = this.list.concat(res.content.records)
							if (this.list.length >= res.content.total) {
								this.status = 'noMore'
							} else {
								this.status = 'more'
								this.query.pageCurrent++
							}
							resolve()
						}
					})
				})
			},
		},
		onPullDownRefresh() {
			this.query.pageCurrent = 1
			this.list = []
			this.status = 'more'
			this.search().then(() => {
				uni.stopPullDownRefresh()
			})
		},
		onLoad(option) {
			this.tabList[0].list = JSON.parse(uni.getStorageSync('districtList'))
			this.menuId = option.id
			this.getMenu(option.id)
			let that = this
			const eventChannel = this.getOpenerEventChannel();
			if (JSON.stringify(eventChannel) !== '{}') {
				eventChannel.on('banner', function(data) {
					that.banner = data
				})
			}
			uni.setNavigationBarTitle({
				title: option.title
			});
			this.query.menuId = option.id
			this.location = JSON.parse(uni.getStorageSync('location'))
			this.query['location.lat'] = this.location.latitude
			this.query['location.lon'] = this.location.longitude
			this.search()

		},

	}
</script>

<style lang="scss">
	.bar-item-text {
		max-width: 250rpx !important;
	}

	.screen-bar::after {
		display: none;
	}

	.discountsDetail cc-drop-down-menu .screen-bar {
		// position: fixed;
		top: 0%;
		left: 0;
		width: 100%;
	}

	.discountsDetail {
		background: #F9F9F9;
		padding-top: 80rpx;

		.banner {
			width: 100%;
			height: 310rpx;
			position: relative;
			z-index: 99;
		}

		.zs-list {
			margin: 0 24rpx;

			.item {
				padding: 24rpx;
				background: #fff;
				border-radius: 16rpx;
				margin: 20rpx 0 0;

				.box {
					display: flex;

					.icon {
						border-radius: 16px;
					}

					.info {
						flex: 1;
						padding-left: 20rpx;
						display: flex;
						flex-direction: column;
						position: relative;

						.title {
							font-weight: 600;
							font-size: 36rpx;
							color: #222222;
							width: 340rpx;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							margin-top: 25rpx;
						}

						.desc {
							margin-top: 20rpx;
							font-size: 24rpx;
							color: #AAAAAA;
							overflow: hidden;
							text-overflow: ellipsis;
							/* 弹性伸缩盒子模型显示 */
							display: -webkit-box;
							/* 限制在一个块元素显示的文本的行数 */
							-webkit-line-clamp: 2;
							/* 设置或检索伸缩盒对象的子元素的排列方式 */
							-webkit-box-orient: vertical;
						}

						

					}
				}
				.goods-item.none{
					border: none;
					padding-bottom: 0;
				}
				.goods-item{
					display: flex;
					padding: 10rpx 0;
					display: flex;
					align-items: center;
					.tag{
						text-align: center;
						font-size: 20rpx;
						color: #FFFFFF;
						background: $uni-color-primary;
						padding: 2rpx 10rpx;
						border-radius: 8rpx 8rpx 8rpx 8rpx;
					}
					.goods-name{
						font-weight: 400;
						font-size: 24rpx;
						color: #222222;
						margin-left: 18rpx;
						width: 250rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
					.price-box {
						display: flex;
						align-items: center;
						margin-left: 10rpx;
							.unit {
								font-size: 20rpx;
								color: $uni-color-primary;
								font-weight: bold;
							}
					
							.price {
								font-size: 28rpx;
								color: $uni-color-primary;
								font-weight: bold;
							}
							.discount-tag{
								display: flex;
								align-items: center;
								width: 82rpx;
								height: 28rpx;
								line-height: 28rpx;
								background: #FFF6F5;
								border-radius: 8rpx 8rpx 8rpx 8rpx;
								border: 1rpx solid $uni-color-primary;
								margin-left: 10rpx;
								.icon{
									width: 30.5rpx;
									height: 28rpx;
								}
								.discount-num{
									flex: 1;
									text-align: center;
									font-size: 20rpx;
									color: $uni-color-primary;
								}
							}
					
							.old-price {
								font-size: 20rpx;
								color: #AAAAAA;
								text-decoration: line-through;
								margin-left: 12rpx;
							}
						
					}
				}
			}
		}

	}
</style>