<template>
	<view class="refuel">
		<!-- 内容 -->
		<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)">
				<zs-img :src="item.logo" width="200rpx" height="200rpx" radius="full" ></zs-img>
				<view class="info">
					<view class="title">
						{{item.storeName}}
					</view>
					<view class="address" >
						距离你 {{(item.distance/1000).toFixed(2)}} 公里
					</view>
					<view  class="activity">
						<view class="price-box">
							<view class="price">
								￥{{item |filterVipPrice}}/L
							</view>
							<view class="old-price">
								国际价 ￥{{item|filterPrice}}/L
							</view>
						</view>
						<view class="coupon">
							可使用优惠券
						</view>
					</view>
					
				</view>
			</view>
		</zs-list>



	</view>
</template>

<script>
	import { getNearbyStore } from '@/api/refuel.js';
	import {
		search
	} from '@/api/shop.js';
	export default {
		data() {
			return {
				map: null,
				longitude: 0,
				latitude: 0,
				scale:18,
				markers: [],
				destination: '',
				active: 0,
				status: 'more',
				list: [],
				list: [],
				// query:{
				// 	latitude:39.896751,
				// 	longitude:116.460653,
				// 	maxDistanceInKilometers:50,
				// 	pageCurrent:1,
				// 	pageSize:10
				// },
				query: {
					district:'',
					menuId: 4,
					queryName: '',
					'location.lat': 0,
					'location.lon': 0,
					pageCurrent: 1,
					pageSize: 10
				},
			}
		},
		filters: {
			filterVipPrice: function(item) {
				let arr = item.itemInfoList.filter(i=>i.itemName == '92#')
				if(!arr.length &&item.itemInfoList.length){
					arr = [item.itemInfoList[0]] 
				}
				return arr.length?(arr[0].vipPrice/100).toFixed(2) : 0;
			},
			filterPrice: function(item) {
				let arr = item.itemInfoList.filter(i=>i.itemName == '92#')
				if(!arr.length &&item.itemInfoList.length){
					arr = [item.itemInfoList[0]] 
				}
				return arr.length?(arr[0].cityPrice/100).toFixed(2) : 0;
			}
		},
		methods: {

			goDetail(item) {
				if(!uni.getStorageSync('token')){
					return uni.showModal({
						title:'请登录',
						confirmText:'去登录',
						success(res){
							console.log(res);
							if(res.confirm){
								uni.navigateTo({
									url:'/login/login/login?redirect=/refuel/index'
								})
							}
						}
					})
				}
				uni.navigateTo({
					url: `/refuel/refuelDetail?id=${item.storeId}&epId=${item.shopId}`
				})
			},
			loadMore() {
				this.search()
			},
			search() {
					this.status = 'loading'
					let obj = JSON.parse(JSON.stringify(this.query))
					search(obj).then(res => {
						uni.stopPullDownRefresh()
						if (res.state == 'Success') {
							let dataList = []
							res.content.records.map(item=>{
								let obj 
								if(item.tripartiteJson){
									obj = JSON.parse(item.tripartiteJson)
									obj.distance = item.shopVo.distance
									obj.shopId = item.shopVo.id
									dataList.push(obj)
								}
							})
							this.list = this.list.concat(dataList)
							console.log(this.list);
							if (this.list.length >= res.content.total) {
								this.status = 'noMore'
							} else {
								this.status = 'more'
								this.query.pageCurrent++
							}
						}
					})
					// getNearbyStore(this.query).then(res=>{
					// 		uni.stopPullDownRefresh()
					// 		this.status = 'noMore'
					// 	if(res.state == 'Success'){
					// 		this.list = res.content
							
					// 	}
					// })
			},
		},
		onPullDownRefresh() {
			uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						// 解析地址
						this.query['location.lat'] = res.latitude
						this.query['location.lon'] = res.longitude
						// 存储经纬度
						uni.setStorageSync('location',JSON.stringify({latitude:res.latitude,longitude:res.longitude}))
						this.query.pageCurrent = 1
						this.status = 'more'
						this.list = []
						this.search()
					}
			})
		},
		onLoad(option) {
		},
		onReady() {
			// let location = JSON.parse(uni.getStorageSync('location'))
			// this.query['location.lat'] = location.latitude
			// this.query['location.lon']= location.longitude
			// this.search()
			uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						// 解析地址
						this.query['location.lat'] = res.latitude
						this.query['location.lon'] = res.longitude
						console.log(this);
						// 存储经纬度
						uni.setStorageSync('location',JSON.stringify({latitude:res.latitude,longitude:res.longitude}))
						this.search()
					}
			})
		}
	}
</script>

<style lang="scss">

	.refuel {
		background: #F9F9F9;
		padding: 20rpx 24rpx;
		.zs-list {
			// padding-top: 20rpx;
			.item {
				display: flex;
				padding: 24rpx;
				background: #fff;
				border-radius: 16rpx;
				margin-bottom: 20rpx;
				.icon {
					width: 200rpx;
					height: 180rpx;
					border-radius: 8px;
				}

				.info{
					flex: 1;
					padding: 16rpx 0 16rpx 20rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					.title{
						font-size: 28rpx;
						font-weight: bold;
						width: 340rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						margin-top: 10rpx;
					}
					.address{
						font-weight: 300;
						font-size: 24rpx;
						color: #AAAAAA;
					}
					
					.activity{
						display: flex;
						align-items: center;
						justify-content: space-between;
						.price-box{
							display: flex;
							align-items: center;
							.price{
								font-weight: 600;
								font-size: 32rpx;
								color: $uni-color-primary;
							}
							.old-price{
								font-weight: 300;
								font-size: 18rpx;
								color: #AAAAAA;
								text-decoration: line-through;
								margin-left: 10rpx;
							}
						}
						.coupon{
							font-weight: 300;
							font-size: 24rpx;
							color: $uni-color-primary;
						}
					}
				
				}
			}
		}
	}
</style>