<template>
	<view class="discountsDetail">

		<!-- 地图 -->
		<map id="map" class="map" style="width: 100%;" :longitude="longitude" :latitude="latitude" :scale='scale'
			 enable-poi show-location :markers="markers"
			@regionchange="mapChange" @callouttap="handleCallout">
			<image class="position" src="../../static/map-position.png" mode="" @click="myPosition"></image>
			<cover-view slot="callout">  
						  <cover-view v-for="(item,index) in markers" :key="item.id" class="callout" :marker-id="item.id"  @click="test">
						    <cover-image class="icon" src="@/static/jiayou.png"></cover-image>  
								<cover-view class="title">
									7.65/L
								</cover-view>
								<cover-view class="triangle"></cover-view>
						  </cover-view>  
			      </cover-view>  

		</map>

		<!-- titleArr: 选择项数组 dropArr: 下拉项数组 二维数组 @finishDropClick: 下拉筛选完成事件-->
		<cc-dropDownMenu :titleArr="titleArr" :dropArr="dropArr" @finishDropClick="finishClick"></cc-dropDownMenu>
		<!-- 内容 -->
		<zs-list class="store-box" mt="0" @load="loadMore" :status="status">
			<view class="item" v-for="(item,index) in list" :key="index">
				<!-- <image :src="item.src" mode="" class="icon" @click="goDetail(item)"></image> -->
				<zs-img :src="item.logoPath" width="164rpx" height="164rpx" radius="full" @click.native="goDetail(item)"></zs-img>
				<view class="info">
					<view class="title">
						{{item.shopVo.shopName}}
					</view>
					<view  class="activity" :class="!item.activityContent?'white':''">
						<image class="card" src="../../static/card-icon.png" mode=""></image>
						{{item.activityContent || '-'}}
					</view>
					<view class="address" >
						<image class="position" src="../../static/address-icon.png" mode=""></image>
						距离你{{(item.shopVo.distance/1000).toFixed(2)}}km ·{{item.district}}
					</view>
					
				</view>
			</view>
		</zs-list>



	</view>
</template>

<script>
	import { search } from '@/api/shop.js';
	export default {
		options: {
			styleIsolation: 'shared'
		},
		data() {
			return {
				map: null,
				longitude: 0,
				latitude: 0,
				scale:18,
				markers: [],
				destination: '',
				active: 0,
				status: 'more',
				list: [],
				btnText: '去加油',
				list: [],
				titleArr: ['全城', '距离最近'],
				dropArr: [
					// 距离
					[{
							text: '全城',
							value: ""
						},
						{
							text: '观山湖区',
							value: "440103"
						}
					],

					// 类型
					[{
							text: '距离最远',
							value: "0-40"
						},
						{
							text: '距离最近',
							value: "40-60"
						}
					],
				],
				filterResultData: [],
				query:{
					menuId:'',
					queryName:'',
					'location.lat':0,
					'location.lon':0,
					pageCurrent:1,
					pageSize:10
				}
			}
		},

		methods: {
			// 点击地图气泡
			handleCallout(val){
				console.log(val);
			},
			// 地图中打开商家
			handleAdress(item) {
				this.map.openMapApp({
					longitude: Number(item.longitude) ,
					latitude: Number(item.latitude),
					destination: item.shopVo.shopName,
					complete(r) {
						console.log(r);
					}
				})
			},
			// 获取初始位置
			initPosition() {
				let that = this
				uni.getLocation({
					type: 'gcj02',
					success(res) {
						that.longitude = res.longitude
						that.latitude = res.latitude
					}
				})
			},
			// 回到我的位置
			myPosition() {
				let that = this
				console.log(that.map);
				uni.getLocation({
					type: 'gcj02',
					success(res) {
						that.map.moveToLocation({
							longitude: res.longitude,
							latitude: res.latitude
						})
						console.log(res);
						// that.markers =[
						// 	{	id:1,
						// 		latitude: res.latitude,
						// 		longitude: res.longitude,
						// 		width: '42rpx',
						// 		height: '42rpx',
						// 		iconPath: '/static/jiayou.png',
						// 		callout:{
						// 			content:'#92 7.5元/L',
						// 			bgColor:'#fff',
						// 			color:'#222',
						// 			borderRadius:'50%',
						// 			fontSize: '22rpx',
						// 			padding:'16rpx',
						// 			display:'ALWAYS'
						// 		},
						// 	},
							
						// ]
					
					}
				})
			},
			// 地图拖动
			mapChange(val) {
				
			},
			finishClick(resultData) {
				this.filterResultData = resultData;
				// uni.showModal({
				// 	title: '温馨提示',
				// 	content: '筛选数据 = ' + JSON.stringify(resultData)
				// })
			},

			goDetail(item) {
				uni.setStorageSync('shopInfo', JSON.stringify(item))
				uni.navigateTo({
					url: '../shopDetail/shopDetail?type=' + this.active
				})
			},
			loadMore() {
				this.search()
			},
			search() {
				return new Promise((resolve,reject)=>{
					if(this.status == 'noMore') return
					this.status = 'loading'
					search(this.query).then(res=>{
						if(res.state == 'Success'){
							this.list = this.list.concat(res.content.records)
							res.content.records.map((item,index)=>{
								this.markers.push(
								{
								  id: index+1,  
								  latitude: item.shopVo.location.lat,  
								  longitude: item.shopVo.location.lon,  
								  zIndex: '1',
								  iconPath: '/static/triangle.png',  
								  width: '18rpx',  
								  height: '16rpx',  
								  customCallout: {  
								    anchorY: 0,  
								    anchorX: 0,  
								    display: 'ALWAYS'  
								  },
								  label:{
									  content:item.shopVo.shopName,
									  fontSize:'10',
									  textAlign:'center'
								  },
								  joinCluster:true
								}
								)
							})
							
							if(this.list.length >= res.content.total){
								this.status = 'noMore'
							}else{
								this.status = 'more'
								this.query.pageCurrent++
							}
							resolve()
						}
					})
				})
			},
		},
		onLoad(option) {
			this.initPosition()
			this.query.menuId = option.id
		},
		onReady() {
			this.map = uni.createMapContext("map", this);
			let location = JSON.parse(uni.getStorageSync('location'))
			this.query['location.lat'] = location.latitude
			this.query['location.lon'] = location.longitude
			this.search()
		}
	}
</script>

<style lang="scss">
	.callout{
			width: 150rpx;
			height: 54rpx;
			background: #61B5FD;
			// box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(0,0,0,0.2);
			border-radius: 60rpx 60rpx 60rpx 60rpx;
			display: flex;
			align-items: center;
			position: relative;
			.icon{
				width: 42rpx;
				height: 42rpx;
				margin-left: 6rpx;
			}
			.title{
				flex: 1;
				color: #FFFFFF;
				font-weight: bold;			
				font-size: 20rpx;
				text-align: center;
			}
			.triangle{
				width: 0;
				height: 0;
				border-top: 16rpx solid red;
				border-bottm: 16rpx solid transparent;
				border-left: 16rpx solid transparent;
				border-right: 16rpx solid transparent;
				position: absolute;
				bottom: -16rpx;
				z-index: 9999999;
				left: 50%;
				// transform: translate(-50%,100%);
			}
		}

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

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

		.recommend {
			display: flex;
			justify-content: space-between;
			margin: 20rpx 30rpx;

			.recommend-item.color {
				background: linear-gradient(0deg, #FFDA84 0%, #FFF6E1 100%);

			}

			.recommend-item {
				padding: 18rpx;
				border-radius: 16rpx;
				width: 335rpx;
				box-sizing: border-box;
				background: linear-gradient(0deg, #FF9898 0%, rgba(255, 101, 101, 0.09) 100%);
				;
				box-shadow: inset 0rpx 6rpx 12rpx 2rpx rgba(255, 255, 255, 0.16);

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

					.title {
						color: #0F0F0F;
						font-size: 26rpx;
					}

					.type-box {
						display: flex;
						align-items: center;
						padding: 0 16rpx;
						height: 40rpx;
						line-height: 40rpx;
						border-radius: 20rpx;
						border: 1rpx solid #FF4343;

						.hot {
							width: 16.7rpx;
							height: 21.7rpx;
						}

						.type {
							font-size: 20rpx;
							font-weight: 400;
							color: #FF4343;
							margin-left: 6rpx;
						}
					}
				}

				.image-box {
					position: relative;
					margin-top: 20rpx;
					background: #fff;
					border-radius: 16rpx;
					display: flex;

					.icon {
						width: 116rpx;
						height: 116rpx;
						margin-left: 15rpx;
					}

					.info {
						padding: 16rpx;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.title {
							font-size: 20rpx;
							font-weight: bold;
						}

						.btn-box {
							display: flex;
							align-items: center;
							width: 100%;
							font-size: 18rpx;
							height: 36rpx;
							line-height: 36rpx;
							margin-top: 10rpx;

							.btn {
								height: 36rpx;
								line-height: 36rpx;
								padding: 0 14rpx;
								background: #FE5B47;
								color: #FFFFFF;
							}

							.desc {
								height: 36rpx;
								line-height: 36rpx;
								font-weight: 400;
								color: #FF4343;
								padding: 0 18rpx 0 10rpx;
								background: rgba(254, 91, 71, .17);
								border-radius: 0 8rpx 8rpx 0;
							}
						}
					}
				}
			}
		}

		.zs-list {

			// padding-top: 20rpx;
			.item {
				display: flex;
				padding: 20rpx 30rpx;
				background: #fff;
				border-radius: 16rpx;
				margin: 20rpx 30rpx 0;
				border-bottom: 1rpx solid #F0F0F0;
				.icon {
					width: 200rpx;
					height: 180rpx;
					border-radius: 8px;
				}

				.info{
					flex: 1;
					padding-left: 20rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					position: relative;
					.title{
						font-size: 28rpx;
						font-weight: bold;
						width: 340rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						margin-top: 10rpx;
					}
					.address{
						color: #bebebe;
						display: flex;
						align-items: center;
						font-size: 24rpx;
						.u-icon{
							align-self: flex-start;
						}
						.position {
								width: 30rpx;
								height: 30rpx;
								margin-right: 8rpx;
							}
					}
					.activity.white{
						color: #fff;
						background: #fff;
						.card{
							display: none;
						}
					}
					.activity{
						font-size: 24rpx;
						color: $uni-color-primary;
						line-height: 42rpx;
						display: flex;
						align-items: center;
						background: linear-gradient(90deg, #FAF4EB 0%, rgba(255,235,207,0) 100%);
						.card{
							width: 30.4rpx;
							height: 30rpx;
							vertical-align: bottom;
							margin: 0 8rpx 0 14rpx;
						}
						// text-align: center;
						// background: linear-gradient(90deg, rgba(253,149,108,0) 0%, #FD8B64 50%, rgba(253,140,101,0) 100%);
					}
				
				}
			}
		}

			.map {
				height: 480rpx;
				position: relative;
				.position {
					position: absolute;
					bottom: 20rpx;
					right: 20rpx;
					width: 60rpx;
					height: 60rpx;
				}
			}
	}
</style>