<template>
	<view class="jpmain body">
		<componentLogin ref="refLogin"  :check="0"
		@findByOpenId="findByOpenId"></componentLogin>

		<view class="Area Area1" v-if="pointAuthorize">
			<view class="top">
				<view class="name">附近停车场</view>
				<view class="value" @click="getUserLocation()">
					<img class="img" src="@/assets/img/homepage/riLine-restart-line.svg" alt="">
					重新定位
				</view>
			</view>
			<view class="list">
				<view class="line" v-for="(item,i) in nearList"
					@click="gotoInfo(item)">
					<view class="item" >
						<view class="name">
							<img class="img" src="@/assets/img/homepage/stLine-location-l.svg" alt="">

							{{item.name}}
							<span class="tag" v-if="item.userPreferred"> <u-icon name="star-fill" color="#FFAE00" size="24"></u-icon> 常用 </span>
							
						</view>
						<view class="value">小于50米</view>
					</view>
					<view class="item2" v-if="item.address&&0" >
						地址：<span  class="value">{{item.address}}</span>
					</view>
				</view>
			</view>
			
			<u-empty text="50米内没有停车场" mode="list" margin-top="80" v-if="!nearList.length">
				<view slot="bottom">
					<view class="value-slot" @click="getUserLocation()">
						<img class="img" src="@/assets/img/homepage/riLine-restart-line.svg" alt="">
						重新定位
					</view>
				</view>
			</u-empty>

		</view>

		<view class="Area Area2" v-if="otherList.length">
			<view class="top">
				<view class="name">全部停车场</view>

			</view>
			<view class="list">
				<view class="line" v-for="(item,i) in otherList" :key="i"
					@click="gotoInfo(item)">
					<view class="item" >
						<view class="name"> <img class="img" src="@/assets/img/homepage/stLine-location-l.svg" alt="">

							{{item.name}}
							<span class="tag"  v-if="item.userPreferred" > <u-icon name="star-fill" color="#FFAE00" size="24"></u-icon> 常用 </span>
						</view>
						<view class="value">{{distanceN(item.distance)}}</view>
					</view>
					<view class="item2" v-if="item.address&&0" >
						地址：<span  class="value">{{item.address}}</span>
					</view>
				</view>

			</view>
		</view>

	</view>
</template>

<script>
	import * as API from '@/apis/pagejs/index.js'
	import componentLogin from '@/components/componentLogin.vue';

	export default {
		components: {
			componentLogin,
		},
		data() {
			return {
				pointAuthorize:true,
				latitude: 30.337053,

				longitude: 112.240222,
			
				allList: [],
				isReady:true,
				point: false, //是否获取了定位
			}
		},
		onLoad() {
			this.userInfo = this.jphelp.getPersonInfo()
			
		},
		onReady() {
			this.$refs.refLogin.findByOpenId()
			//this.getParkingList()
			
		},
		onShow() {
				if(this.isReady){
					if(this.point){
						// #ifdef MP-WEIXIN
							this.getUserLocation()	
						// #endif
					}else{
						this.getParkingList()
					}
					
				}
		},
		onShareTimeline(){
			return {
				title: '智泊e家',
				path: '/pages/index/index' ,
			}
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				//.log(res.target)
			}
			return {
				title: '智泊e家',
				path: '/pages/index/index' 
			}
		},
		computed: {
			otherList() {
				var sz = []


				for (var i in this.allList) {
					var item = this.allList[i]
					if (this.point) {

						if (item.distance > 1) {
							sz.push(item)
						}
					} else {
						sz.push(item)
					}
				}

				return sz
			},
			nearList() {
				var sz = []
				for (var i in this.allList) {
					var item = this.allList[i]
					if (this.point) {

						if (item.distance < 1) {
							sz.push(item)
						}
					} else {

					}

				}

				return sz
			}
		},
		methods: {
			gotoInfo(item){
				var url='pages/index/main/parkingDetails?id='+item.id;
				if (this.point) {
					url+=`&point=1&longitude=${this.longitude}&latitude=${this.latitude}`
				}
				this.gotoUrl(url)
			},
			getParkingList() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				var obj = {
					pageIndex: 1,
					pageSize: 20,
					radius: 50,
				}
				if (this.point) {
					obj.longitude = this.longitude
					obj.latitude = this.latitude
				}
				API.parkingList(obj).then((res) => {

					this.allList = res.data.data
					console.log(this.nearList)
					uni.hideLoading();

				}).catch(error => {

					uni.hideLoading();
					uni.showToast({
						title: error,
						icon: "none"
					})

				})
			},
			findByOpenId(res) {
				this.isReady=true
				// #ifdef MP-WEIXIN
					this.getUserLocation()	
				// #endif
				
				this.getParkingList()
				
			},
			getUserLocation() {
				var _this=this
				uni.authorize({
					scope: 'scope.userLocation',
					success() {
						uni.getLocation({
							type: 'gcj02',
							altitude:true,
							isHighAccuracy:true,
							success: function(res) {
								
								console.log('当前位置的经度：' + res.longitude);
								console.log('当前位置的纬度：' + res.latitude);
								_this.point=true
								_this.longitude=res.longitude
								_this.latitude=res.latitude
								_this.getParkingList()
							}
						});
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.body {
		padding: 32rpx;
		padding-top:40rpx;
	}
	
	.value-slot {
		color: rgba(22, 119, 255, 1);
		font-size: 24rpx;
		display: flex;
		align-items: center;
	
		.img {
			width: 28rpx;
			height: 28rpx;
		}
	}
	.Area {
		.img {
			margin: 0 8rpx;
		}

		margin-bottom: 32rpx;

		.top {
			margin-bottom: 8rpx;
			display: flex;
			justify-content: space-between;

			.name {
				color: rgba(51, 51, 51, 1);
				font-size: 32rpx;
				font-weight: bold;
			}

			.value {
				color: rgba(22, 119, 255, 1);
				font-size: 24rpx;
				display: flex;
				align-items: center;

				.img {
					width: 28rpx;
					height: 28rpx;
				}
			}
		}

		.list {
			.line{
				.item {
					display: flex;
					justify-content: space-between;
					padding: 16rpx 0;
				
					.name {
						color: rgba(51, 51, 51, 1);
						font-size: 32rpx;
						display: flex;
						align-items: center;
				
						.img {
							width: 32rpx;
							height: 32rpx;
						}
						.tag{
							font-size: 24rpx;
							color: rgba(255,174,0,1);
							border: 1px solid rgba(255,174,0,1);
							padding: 1px 8rpx;
							border-radius: 4px;
							margin-left: 8rpx;

						}
				
					}
				
					.value {
						color: rgba(119, 119, 119, 1);
				
						font-size: 28rpx;
					}
				}
				.item2{
					.value {
						color: rgba(119, 119, 119, 1);
									
						font-size: 28rpx;
					}
				}
			}
			
		}
	}

	.Area2 {
		margin-top: 40rpx;
		.list {
			.line {
				border-bottom: 1px solid rgba(232, 232, 232, 1);
				;

			}
		}
	}
</style>