<template>
	<view class="page">
		<u-navbar title="附近门店" :border-bottom="false" :background="bgc" title-color='#000' back-icon-color="#000" title-size='36' height='50'></u-navbar>
			<view class="dtxs">
					<map class='map' id="map" :latitude="latitude" :longitude="longitude" @markertap="handleMarkerClick"
						:show-location="true" :markers="covers" :scale="mapScale" />
			</view>
<!-- 		<view class="listbox">
			<view class="moshi">
				<image src="https://api.ccttiot.com/smartmeter/img/static/u7HPhEwbIJqbLRpImBON" mode="" @click="btndt(1)"></image>
				<image src="https://api.ccttiot.com/smartmeter/img/static/uGVN2tOaCq2hiWKy1cYl" mode="" @click="btndt(2)" v-if="isMch"></image>
			</view>
		</view>	 -->
		
		<view class="list_item">
			<view class="list">
				<view class="pic">
					<image :src="listobj.picture" mode="aspectFill"></image>
				</view>
				<view class="cen">
					<view class="cena" style="font-weight: 600;">
						{{listobj.name}}
					</view>
					<view class="cenb">
						{{listobj.address}}
					</view>
					<view class="cenc">
						营业时间：{{listobj.businessTimeStart}}-{{listobj.businessTimeEnd}} 
					</view>
				</view>
			</view>
			<view class="chakan" >
				<view class="wz" style="color: #109F64;" v-if="listobj.availableDeviceCount > 0">可租借</view>
				<view class="wz" style="color: #109F64;" v-if="num > 0">可归还</view>
				<view class="wz" style="background-color: #FFEFEF;color: #FF4444;" v-if="num == 0">不可还</view>
			</view>
			<view class="anniu">
				<view class="" @click="mapFun">
				    <image style="width: 42rpx;height: 40rpx;vertical-align: middle;margin-bottom: 14rpx;margin-right: 10rpx;" src="https://api.ccttiot.com/smartmeter/img/static/u7jJoQmp3QBVyOWa4ayq" mode=""></image>到这去
				</view>
				<view class="" style="color: #fff;background-color: #25CE88;" @click="scanQRCode">
				  <image style="width: 48rpx;height: 48rpx;vertical-align: middle;margin-bottom: 10rpx;" src="https://api.ccttiot.com/smartmeter/img/static/uMXqFPfRkmTxb3pLYKL2" mode=""></image>	扫码充电
				</view>
			</view>
		</view> 
	
	</view> 
</template>

<script>
	export default {
		data() {
			return {
				latitude: 31.02, // 初始纬度  
				longitude: 112.12, // 初始经度   
				covers: [], // 覆盖物数组  
				index:1,
				falga:true,
				falgb:false,
				storeId:'',
				listobj:{},
				mapname:'',
				mapaddress:'',
				isMch:'',
				num:0,
				bgc:{
					background:'#25D088'
				},
				listmap: [],
				mapScale: 16,
				datetime: 0,
				dingobj: '',
				mapContext: null,
				mapScaleInterval: null,
			}
		},
		onReady() {
			this.mapContext = uni.createMapContext('map', this);
			this.mapScaleInterval = setInterval(this.updateMarkers, 1000);
		},
		beforeDestroy() {
			if (this.mapScaleInterval) {
				clearInterval(this.mapScaleInterval);
				this.mapScaleInterval = null;
			}
		},
		onLoad(option) {
			this.storeId = option.markerId
			if(option.markerId){
				this.falga = false
				this.falgb = true
			}
			this.getxq()
		},
		methods: {
			updateMarkers() {
				this.mapContext.getScale({
					success: (res) => {
						this.covers = []; // 清空之前的覆盖物  
						if (res.scale <= 14) {
							this.addMarkersWithoutLabels()
						} else {
							this.addMarkersWithLabels()
						}
					},
					fail: (error) => {
						// console.error('获取地图缩放级别失败：', error)
					},
				});
			},
			addMarkersWithoutLabels() {
				this.listmap.forEach((item) => {
					const shopCover = {
						id: parseFloat(item.storeId),
						latitude: item.lat,
						longitude: item.lng,
						width: 25,
						height: 30,
						iconPath: 'https://api.ccttiot.com/smartmeter/img/static/uI3B6yPwtiUBD3vafLtw', 
						borderColor:'#fff',
						borderRadius:5,
						bgColor: 'rgba(255, 255, 255, 0.7)'
					};
					this.covers.push(shopCover)
				});
			},
			addMarkersWithLabels() {
				this.listmap.forEach((item) => {
					const shopCover = {
						id: parseFloat(item.storeId),
						latitude: item.lat,
						longitude: item.lng,
						width: 25,
						height: 30,
						iconPath: 'https://api.ccttiot.com/smartmeter/img/static/uI3B6yPwtiUBD3vafLtw',
						label: {
							content: item.name,
							anchorX:this.calculateAnchorX(item.name),
							fontWeight: 700,
							color: '#25D088',
							textShadow: '2px 2px 0px #000, -2px -2px 0px #000, 2px -2px 0px #000, -2px 2px 0px #000',
							rotate:20,
							borderColor:'#fff',
							borderRadius:5,
							bgColor: 'rgba(255, 255, 255, 0.7)'
						}
					}
					this.covers.push(shopCover)
				});
			},
			calculateAnchorX(name) {  
			    let chineseLength = 0
			    let englishLength = 0 
			    for (let i = 0; i < name.length; i++) {  
			        const charCode = name.charCodeAt(i) 
			        if (charCode >= 0x4e00 && charCode <= 0x9fa5) {  
			            chineseLength++;  
			        } else if (/[a-zA-Z]/.test(name[i])) { 
			           englishLength = englishLength+0.3 
			        }  
			    }  
			    const totalLength = chineseLength + englishLength * 2
			    return -totalLength * 6.5
			},
			
			scanQRCode() {
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: res => {
						console.log('扫描结果：', res);
						let result = res.result
						uni.navigateTo({
							url: '/page_user/yajin/index?sn=' + result
						})
					},
					fail: err => {
						console.error('扫描失败：', err)
						uni.showToast({
							title: '扫描失败',
							icon: 'none'
						})
					}
				})
			},
			
			getxq(){
				this.$u.get("/app/store/" + this.storeId).then(res => {
					if (res.code == 200) {
					   this.listobj = res.data
					   this.num = this.listobj.slotCount - this.listobj.deviceCount
					}
				})
			},
			
			mapFun() {
				uni.openLocation({
					latitude: this.listobj.lat,
					longitude: this.listobj.lng, 
					name:this.mapname,
					address: this.mapaddress
				});
			},
			async setMapScale(e, val) {
				let mapContext = uni.createMapContext('map', this);
				let setScale = () => {
					return new Promise((resolve, reject) => {
						mapContext.getScale({
							success: r => {
								resolve()
							}
						})
					})
				};
				await setScale();
				mapContext.moveToLocation({
					success: (res) => {
						const timer = setTimeout(() => {
							clearTimeout(timer);
						}, 500);
					}
				})
			},
			
			getMyLocation() {
			    uni.getLocation({  
			        type: 'wgs84',  
			        success: (res) => {  
			            this.jinweidu = res.longitude + ',' + res.latitude
			            this.latitude = Number(res.latitude.toFixed(5)) - 0.004
			            this.longitude = Number(res.longitude.toFixed(5)) + 0.004
						this.setMapScale()
			            // 请求附近的店铺  
			            this.$u.get("/app/store/listNearBy?center=" + this.jinweidu + '&radius=' + 1000).then(res => {  
								if (res.code == 200) {
									this.listmap = res.data
									res.data.forEach(item => {
										const shopCover = {
											id: parseFloat(item.storeId),
											latitude: item.lat,
											longitude: item.lng,
											width: 25,
											height: 30,
											iconPath: 'https://api.ccttiot.com/smartmeter/img/static/uI3B6yPwtiUBD3vafLtw', 
											label: {
												content: item.name,
												anchorX: (() => {
													if (item.name.length <= 2) {
														return -12
													} else if (item.name.length <= 3) {
														return -20
													} else if (item.name.length <= 4) {
														return -25
													} else if (item.name.length <= 5) {
														return -30
													} else if (item.name.length <= 6) {
														return -35
													} else if (item.name.length <= 8) {
														return -40
													} else if (item.name.length <= 10) {
														return -45
													} else {
														return -item.name.length * 5
													}
												})(),
												fontWeight: 700,
												color: '#25D088',
												textShadow: '2px 2px 0px white, -2px -2px 0px white, 2px -2px 0px white, -2px 2px 0px white',
												borderColor:'#fff',
												borderRadius:5,
												bgColor: 'rgba(255, 255, 255, 0.7)'
											}
										};
										this.covers.push(shopCover)
									})
								} 
			            });  
			        },  
			        fail: (err) => {  
			            console.error('获取位置失败：', err)
			        }  
			    });  
			},  
			
			handleMarkerClick(e){
				this.$u.get("/app/store/" + e.markerId).then(res => {
					if (res.code == 200) {
					   this.listobj = res.data
					   this.mapname = res.data.city
					   this.mapaddress = res.data.address
					}
				})
				this.falga = false
				this.falgb = true
			},
			
			
			btntab(num){
				this.index = num
			},
			
			btndt(num){
				if(num == 1){
					uni.navigateBack()
				}
			},
			
			btnitem(){
				this.falga = false
				this.falgb = true
			}
		},
		mounted() {
		   this.getMyLocation(); 
		}
	}
</script>

<style lang="scss">
	/deep/ .u-title,
	/deep/ .uicon-nav-back {
		padding-bottom: 22rpx;
	}
	/deep/ .map{
		width: 100%;
		height: 100%;
	}
	page {
		background: linear-gradient(180deg, #25CE88 0%, rgba(255, 255, 255, 0) 100%);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}

	.page {
		width: 750rpx;
        position: fixed;
		top: 0;
		left: 0;
		.list_item{
			position: fixed;
			bottom: 84rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 706rpx;
			height: 400rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0,0,0,0.08);
			border-radius: 20rpx;
			margin: auto;
			padding: 46rpx 40rpx;
			box-sizing: border-box;
			.chakan{
				display: flex;
				margin-bottom: 10rpx;
				// justify-content: space-between;
				padding-left: 140rpx;
				width: 625rpx;
				box-sizing: border-box;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				.wz{
					font-size: 20rpx;
					background: #B7FFE1;
					border-radius: 16rpx 16rpx 16rpx 16rpx;
					padding: 4rpx 16rpx;
					box-sizing: border-box;
					margin-left: 10rpx;
				}
			}
			.anniu{
				display: flex;
				justify-content: space-between;
				margin-top: 10rpx;
				view{
					width: 300rpx;
					height: 82rpx;
					line-height: 82rpx;
					border-radius: 45rpx 45rpx 45rpx 45rpx;
					border: 2rpx solid #25CE88;
					text-align: center;
					font-weight: 500;
					font-size: 36rpx;
					color: #25CE88;;
				}
			}
			.list{
				display: flex;
				padding-bottom: 16rpx;
				.pic{
					width: 150rpx;
					height: 134rpx;
					border-radius: 10rpx;
					image{
						width: 100%;
						height: 100%;
						mode: "aspectFill";
						margin-top: 10rpx;
						border-radius: 20rpx;
					}
				}
				.cen{
					margin-left: 20rpx;
					.cena{
						font-weight: 500;
						font-size: 28rpx;
						color: #3D3D3D;
						line-height: 38rpx;
					}
					.cenb{
						font-weight: 400;
						font-size: 24rpx;
						color: #3D3D3D;
						line-height: 32rpx;
						margin-top: 14rpx;
					}
					.cenc{
						font-weight: 400;
						font-size: 24rpx;
						color: #3D3D3D;
						line-height: 32rpx;
						margin-top: 14rpx;
						text{
							display: inline-block;
							padding: 4rpx 18rpx;
							box-sizing: border-box;
							border-radius: 20rpx;
						}
						.bu{
							margin-left: 10rpx;
							background:#D9D8FF;
							color: #25CE88;
						}
					}
				}
				.right{
					padding-top: 40rpx;
					image{
						width: 40rpx;
						height: 40rpx;
					}
					text{
						display: block;
						font-weight: 400;
						font-size: 20rpx;
						color: #808080;
						line-height: 28rpx;
					}
				}
			}
		}
		
		.dtxs{
			width: 100%;
			height: 100vh;
			position: fixed;
			top: 0;
			left: 0;
			z-index: -1;
		}
		.listbox{
			background: #FFFFFF;
			border-radius: 64rpx 64rpx 0  0;
			box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0,0,0,0.08);
			height:186rpx;
			width: 100%;
			overflow-y: scroll;
            position: fixed;
			bottom: 0;
			left: 0;
			.saoyisao{
				margin-top: 44rpx;
				view{
					border-radius: 54rpx 54rpx 54rpx 54rpx;
					text-align: center;
					width: 676rpx;
					height: 102rpx;
					background: linear-gradient( 90deg, #25CE88 0%, #25CE88 100%);
					line-height: 102rpx;
					margin: auto;
					image{
						width: 56rpx;
						height: 56rpx;
						vertical-align: middle;
 						display: inline-block;
						margin-bottom: 12rpx;
						margin-right: 10rpx;
					}
					text{
						font-weight: 500;
						font-size: 40rpx;
						color: #FFFFFF;
					}
				}
			}
			.moshi{
				position: fixed;
				bottom:210rpx;
				left: 30rpx;
				z-index: 99;
				image{
					width: 210rpx;
					height: 62rpx;
				}
			}
			.title{
				border-radius: 64rpx 64rpx 0  0;
				display: flex;
				justify-content: space-between;
				font-weight: 500;
				font-size: 36rpx;
				color: #3D3D3D;
				line-height: 160rpx;
				padding: 0 76rpx;
				width: 100%;
				background-color: #fff;
				position: fixed;
				height: 160rpx;
				top: 40vh;
				left: 0;
				padding-bottom: 20rpx;
			}

		}
	}
	.active{
		border-bottom: 10rpx solid #D9D8FF;
		border-radius: 7rpx;
		padding-bottom: 10rpx;
		font-weight: 700;
	}
</style>