<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="itembody">
						<view class="itemhead">
							 <img class="img" src="@/assets/img/index.png" alt="">
						</view>
						<view class="item" >
							<view class="name">
						
								{{item.name}}
								<span class="tag" v-if="item.userPreferred"> <u-icon name="star-fill" color="#FFAE00" size="24"></u-icon> 常用 </span>
								
							</view>
							<view class="item2"   >
								<span class="span" :class="' lockStatus lockStatus'+item.lockStatus">{{item.lockStatusN}}</span><span class="span">|</span><span  class="span" >{{item.parkingName}}</span>
							</view>
						</view>
						
					</view>
					
					<view class="value">小于50米
					<u-icon name="arrow-right"  color="#CCCCCC"></u-icon>
					</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="itembody">
						<view class="itemhead">
							 <img class="img" src="@/assets/img/index.png" alt="">
						</view>
						<view class="item" >
							<view class="name">
						
								{{item.name}}
								<span class="tag"  v-if="item.userPreferred" > <u-icon name="star-fill" color="#FFAE00" size="24"></u-icon> 常用 </span>
							</view>
							<view class="item2"   >
								<span class="span" :class="' lockStatus lockStatus'+item.lockStatus">{{item.lockStatusN}}</span><span class="span">|</span><span  class="span" >{{item.parkingName}}</span>
								
								<template v-if="item.buildMode">
								
									<span class="span">|</span>
									<span  class="span buildMode1" v-if="item.buildMode==1" >用户自建</span>
									<span  class="span buildMode2" v-if="item.buildMode==2" >平台运营</span>
									
								</template>
							</view>
						</view>
						
					</view>
					<view class="value" v-if="item.distance" >{{distanceN(item.distance)}}<u-icon name="arrow-right" color="#CCCCCC"></u-icon></view>
					<view class="value" v-else ><u-icon name="arrow-right" color="#CCCCCC"></u-icon></view>
					
					
				</view>

			</view>
		</view>
		<u-tabbar v-model="current" :list="tabbarList"
		 mid-button-size="112" :mid-button="true"
		 @input="gotoinput"
		 inactive-color="#ADB8C2" active-color="#5098FF">
		</u-tabbar>
	</view>
</template>

<script>
	import * as API from '@/apis/pagejs/pagesIndex.js'
	import componentLogin from '@/components/componentLogin.vue';
	 
	import tabbar from '@/pages/business/as-components/Tabbar.vue';
	
	export default {
		components: {
			componentLogin,tabbar,
		},
		data() {
			return {
				 current: 0,
				pointAuthorize:true,
				latitude: 30.337053,

				longitude: 112.240222,
			
				allList: [],
				isReady:true,
				point: false, //是否获取了定位
				tabbarList: [{
				
						iconPath:require("@/assets/img/tabBar/index1.png") ,
						selectedIconPath: require( "@/assets/img/tabBar/index1-HL.png"),
						text: '附近 ',
						pagePath2: "/pages/business/index",
					},
					 {
					 	iconPath: require("@/assets/img/tabBar/parking_icon.png"),
					 	selectedIconPath: require("@/assets/img/tabBar/parking_icon.png"),
					 	text: '',
						pagePath2: "/pages/business/main",
					 	midButton: true,
					 },
					
					
					{
						iconPath: require("@/assets/img/tabBar/center.png"),
						selectedIconPath: require("@/assets/img/tabBar/center-HL.png"),
				
						text: '我的',
						pagePath2: "/pages/business/center",
					}
				],
			}
		},
		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()
					}
					
				}
		},
		 
		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: {
			gotoinput(i){
				var item=this.tabbarList[i]
				if(!item.pagePath){
					uni.reLaunch({
						url:item.pagePath2
					})
				}
			},
			gotoInfo(item){
				var url='pages/business/lockInfo?from=1&id='+item.id;
				if (this.point) {
					url+=`&point=1&longitude=${this.longitude}&latitude=${this.latitude}`
				}
				url+="&d="+new Date().getTime()
				this.gotoUrl(url)
			},
			getParkingList() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				var obj = {
					pageIndex: 1,
					pageSize: 999,
					radius: 999999999,
				}
				if (this.point) {
					obj.longitude = this.longitude
					obj.latitude = this.latitude
				}
				API.floorList(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{
				    display: flex;
				    align-items: center;
					    justify-content: space-between;
					.itemhead{
							    display: flex;
						.img {
							width: 48rpx;
							height: 48rpx;
						}
					}
				.itembody{
					padding: 16rpx 0;
					
					    display: flex;
					    align-items: center;
				}
				.item {
					    display: flex;
					    flex-direction: column;
				padding-left: 16rpx;
					.name {
						color: rgba(51, 51, 51, 1);
						font-size: 32rpx;
						display: flex;
						align-items: center;
						//padding-bottom: 8rpx;
						
						.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{
					
						color: rgba(119, 119, 119, 1);
								
						font-size: 24rpx;
						.span{
							margin-right: 12rpx;
						}
						.lockStatus{
							color: #4CAF50;
						}
						.lockStatus1{
							color: #4CAF50;
						}
						.lockStatus2{
							color: #ff9800;
						}
						.lockStatus0{
							color: red;
						}
						.lockStatus4{
							color: red;
						}
				}
			}
			
		}
	}

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

			}
		}
	}
	
	.buildMode{
		font-size: 24rpx;
		    border: 1px solid #bbbbbb;
		    border-radius: 4px;
		    color: #1677ff;
		    padding: 2rpx 8rpx;
		    margin:0 8rpx;
			
	}
	.buildMode1{
		color: #1677ff;
		//border: 1px solid #1677ff;
	}
	.buildMode2{
		color:#4CAF50;
		//border: 1px solid #4CAF50;
	}
</style>