<template>
	<view class="page">
		<u-navbar title="地图模式" :border-bottom="false" :background="bgc" title-color='#fff' back-icon-color="#fff" title-size='44' height='58'></u-navbar>
			<view class="dtxs">
					<map class='map' :latitude="latitude" :longitude="longitude" @markertap="handleMarkerClick" :markers="covers" :show-location="true"/>
			</view>
		<view class="listbox" v-if="falga">
			<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 class="saoyisao">
				<view @click="scanQRCode">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uBh15vzRx6gV0wRQj7gi" mode=""></image><text>扫一扫</text>
				</view>
			</view>
		</view>	
		
		<view class="list_item" v-if="falgb">
			<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}} <text class="bu">{{listobj.availableDeviceCount}}台可共享</text>
					</view>
				</view>
			</view>
			<view class="chakan" @click="btnchak(listobj.storeId)">
				<view class="">
					本店共有{{listobj.deviceCount}}台共享设备
				</view>
				<view class="">
					查看 <u-icon name="arrow-right" color="#6863D9" size="20" style="margin-left: 10rpx;"></u-icon>
				</view>
			</view>
			<view class="anniu">
				<view class="" @click="mapFun">
				    <image class="picsm" src="https://api.ccttiot.com/smartmeter/img/static/u9Z7DXlWfjJtIrq5OEty" mode=""></image> 到这去
				</view>
				<view class="" style="color: #fff;background-color: #8883F0;" @click="scanQRCode">
				    <image class="picsm" src="https://api.ccttiot.com/smartmeter/img/static/uvrsmSxGunHTI9S51ujn" mode=""></image>	扫一扫
				</view>
			</view>
		</view>
	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				latitude: '', // 初始纬度  
				longitude: '', // 初始经度   
				covers: [], // 覆盖物数组  
				index:1,
				falga:true,
				falgb:false,
				storeId:'',
				listobj:{},
				mapname:'',
				mapaddress:'',
				isMch:'',
				bgc: {
					backgroundColor: " #8883F0",
				},
			}
		},
		onLoad(option) {
			this.storeId = option.markerId
			if(option.markerId){
				this.falga = false
				this.falgb = true
			}
			this.getxq()
		},
		methods: {
			scanQRCode() {
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: res => {
						// console.log('扫描结果:', res);
						// let id = res.result
						function getQueryParam(url, paramName) {
							let regex = new RegExp(`[?&]${paramName}=([^&]*)`);
							let results = regex.exec(url);
							return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null;
						}
						let sceneValue = res.result
						let decodedValue = decodeURIComponent(sceneValue);
						let id = getQueryParam(decodedValue, 'deviceNo')
						this.$u.get(`/app/device/${id}/withSuitList`).then((res) =>{
							if(res.code == 200){
								uni.navigateTo({
									url: '/page_components/fuwu/index?id=' + id
								})
							}
						})
					},
					fail: err => {
						console.error('扫描失败:', err);
						uni.showToast({
							title: '扫描失败',
							icon: 'none'
						});
					}
				});
			},
			
			btnchak(id){
				uni.navigateTo({
					url:'/page_fenbao/statulist/myshop/shebeilist/index?storeId=' + id
				})
			},
			
			getxq(){
				this.$u.get("/app/store/" + this.storeId).then(res => {
					if (res.code == 200) {
					   this.listobj = res.data
					}
				})
			},
			getshanghu(){
				this.$u.get("/app/user/userInfo").then((res) => {
					if (res.code == 200) {
						this.isMch = res.data.isMch
					}
				})
			},
			
			mapFun() {
				uni.openLocation({
					latitude: this.listobj.lat,
					 //纬度 - 目的地/坐标点
					longitude: this.listobj.lng, 
					 //经度 - 目的地/坐标点
					name:this.mapname,
					address: this.mapaddress
				});
			},
			
			getMyLocation() {
			    uni.getLocation({  
			        type: 'wgs84',  
			        success: (res) => {  
			            console.log('我的位置:', res);  
			            this.latitude = res.latitude;  
			            this.longitude = res.longitude;  
			            this.jinweidu = this.longitude + ',' + this.latitude;  
			            // 请求附近的店铺  
			            this.$u.get("/app/store/listNearBy?center=" + this.jinweidu + '&radius=' + 1000).then(res => {  
			                if (res.code == 200) {  
			                    // 假设返回的店铺数据在res.rows中,并且每个店铺有latitude和longitude字段  
			                    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/uZXgsJE6hXbRSwbKILB4' ,// 你可以为每个店铺指定不同的图标 
										label: {
											content: item.name,
											// anchorX:-20,
											textAlign: 'center'
										}
			                        };  
			                        this.covers.push(shopCover); // 将店铺覆盖物添加到数组中  
			                    });  
			                    // 添加当前位置的覆盖物(如果需要的话)  
			                    // const myLocationCover = {  
			                    //     latitude: this.latitude,  
			                    //     longitude: this.longitude,  
			                    //     width: 20,  
			                    //     height: 40,  
			                    //     iconPath: '../../../static/image/icon1.png'  
			                    // };  
			                    // this.covers.push(myLocationCover);  
			                }  
			            });  
			        },  
			        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{
		padding-bottom: 41rpx;
	}
	/deep/ .u-icon__icon{
		padding-bottom: 41rpx;
	}
	/deep/ .map{
		width: 100%;
		height: 100%;
	}
	page {
		// background-color: ;
		background: linear-gradient(180deg, #D9D8FF 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;
				justify-content: space-between;
				width: 625rpx;
				height: 60rpx;
				line-height: 60rpx;
				padding: 0 22rpx;
				box-sizing: border-box;
				background: #D9D8FF;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				// opacity: 0.5;
				view{
					font-size: 28rpx;
					color: #8883F0;
				}
			}
			.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 #8883F0;
					text-align: center;
					font-weight: 500;
					font-size: 36rpx;
					color: #8883F0;;
				}
			}
			.list{
				display: flex;
				// margin-top: 44rpx;
				padding-bottom: 16rpx;
				.pic{
					width: 150rpx;
					height: 134rpx;
					// background-color: #D9D8FF;
					border-radius: 20rpx;
					// margin-right: 28rpx;
					image{
						width: 100%;
						height: 100%;
						mode: "aspectFill";
						margin-top: 10rpx;
						border-radius: 20rpx;
					}
				}
				.cen{
					// margin-right: 140rpx;
					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: 1px solid #ccc;
							border-radius: 20rpx;
						}
						.bu{
							margin-left: 10rpx;
							background:#D9D8FF;
							color: #4D48B5;
						}
					}
				}
				.right{
					padding-top: 40rpx;
					image{
						width: 40rpx;
						height: 40rpx;
					}
					text{
						display: block;
						font-weight: 400;
						font-size: 20rpx;
						color: #808080;
						line-height: 28rpx;
					}
				}
			}
		}
		.picsm{
				width: 48rpx;
				height: 48rpx;
				vertical-align: middle;
				display: inline-block;
				margin-bottom: 10rpx;
				margin-right: 10rpx;
		}
		.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, #8883F0 0%, #837EEB 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>