<template>
	<view class="content">
		<!-- 顶部导航 -->
		<uni-nav-bar :border="false" @click-left="toUserCenter" leftIcon="person-filled" leftText=" " :shadow="false"
		 background-color="#fff" :status-bar="true" :fixed="true" color="rgb(255,0,0)" title=" ">
		</uni-nav-bar>
		<!-- 页面主体 -->
		<view class="pageContent">
			<map id="map" :latitude="centerY" :longitude="centerX" scale="14" :markers="markers" bindmarkertap="markertap"
			 show-location style="width: 100%; height:100%;">
				<view class="cover-view">
					<view class="flex-wrp">
						<view @click="intoList" class="flex-item">
							<image class="img" src="/static/img/index-list.png"></image>
							<view>网点列表</view>
						</view>
						<view @click="showPop=true" class="flex-item">
							<image class="img" src="/static/img/index-call.png"></image>
							<view>联系客服</view>
						</view>
						<view class="flex-item">
							<image class="img" src="/static/img/index-discount.png"></image>
							<view>优惠活动</view>
						</view>
					</view>
				</view>
				<view class="cover-bottom">
					<button @click="scanCode" type="warn">扫码洗车</button>
				</view>
			</map>
		</view>
		<!-- 客服弹窗 -->
		<u-popup v-model="showPop" mode="bottom">
			<view class="popup-box">
				<view class="pop-top">
					<view class="top-first pop-item">
						在线客服
					</view>
					<view @click="callPhone" class="pop-item">
						电话客服
					</view>
				</view>
				<view @click="showPop=false" class="pop-cancle">
					取消
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	export default {
		components: {
			uniNavBar, uniPopup
		},
		data() {
			return {
				markers: [], // 标注点
				centerX: '',
				centerY: '',
				showPop: 'false', // 是否显示客服弹窗
				markData: [{
						id: '1',
						name: '洗车机',
						latitude: '34.29318',
						longitude: '108.94712',
					},
					{
						id: '2',
						name: '洗车机',
						latitude: '34.29318',
						longitude: '118.94712',
					},
					{
						id: '11',
						name: '洗车机',
						latitude: '34.29318',
						longitude: '128.94712',
					},
					{
						id: '12',
						name: '洗车机',
						latitude: '38.29318',
						longitude: '108.94712',
					},
					{
						id: '13',
						name: '洗车机',
						latitude: '44.29318',
						longitude: '108.94712',
					}
				]
			}
		},
		onReady() {
			this.mapCtx = wx.createMapContext('map')
		},
		onLoad() {
			this.getUserLocation()
		},
		methods: {
			// 用户位置信息授权
			getUserLocation() {
				let _this = this
				// 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.userLocation" 这个 scope
				wx.getSetting({
					success(res) {
						if (!res.authSetting['scope.userLocation']) {
							wx.authorize({
								scope: 'scope.userLocation',
								success() {
									// 用户已经同意小程序使用位置信息，后续调用 wx.getLocation 接口不会弹窗询问
									_this.getLocation()
								}
							})
						} else {
							_this.getLocation()
						}
					}
				})
			},
			// 获取用户位置
			getLocation () {
				wx.getLocation({
					type: 'gcj02', //返回可以用于wx.openLocation的经纬度
					success: (res) => {
						console.log(res)
						console.log(11111111111)
						let latitude = res.latitude;
						let longitude = res.longitude;
						this.centerX = longitude
						this.centerY = latitude
						this.markers = this.getMarkersData()
					}
				});
			},
			// 点击标点获取数据
			markertap(e) {
				var id = e.markerId
				var name = this.data.markers[id - 1].name
				console.log(name)
				this.setData({
					lingyuanName: name,
					showDialog: true,
				})
			},
			// 获取标记点数据
			getMarkersData() {
				let markers = [];
				for (let item of this.markData) {
					let marker = this.createMarker(item);
					markers.push(marker)
				}
				return markers;
			},
			// 进入个人中心
			toUserCenter () {
				uni.navigateTo({
				    url: '/pages/userCenter/index'
				})
			},
			// 打开网点列表
			intoList() {
				uni.navigateTo({
				    url: '/pages/store/storeList'
				})
			},
			// 联系客服
			concatServicer() {
				 this.$refs.popup.open()
			},
			// 电话客服
			callPhone () {
				uni.makePhoneCall({
				    phoneNumber: '114' 
				})
			},
			// 扫码洗车
			scanCode () {
				uni.scanCode({
				    success: function (res) {
				        console.log('条码类型：' + res.scanType);
				        console.log('条码内容：' + res.result);
				    }
				});
			},
			// 创建标记点
			createMarker(point) {
				let latitude = point.latitude;
				let longitude = point.longitude;
				let marker = {
					iconPath: "/image/location.png",
					id: point.id || 0,
					name: point.name || '',
					latitude: latitude,
					longitude: longitude,
					width: 30,
					height: 30,
					label: {
						content: point.name,
						color: '#22ac38',
						fontSize: 14,
						bgColor: 'transparent',
						borderRadius: 30,
						borderColor: "#22ac38",
						// borderWidth: 1,
						padding: 3
					},
					callout: {
						content: point.name,
						fontSize: 0,
					}
				};
				return marker;

			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 0;

		.pageContent {
			width: 100%;
			flex: 1;

			.cover-view {
				z-index: 9;
				position: absolute;
				top: calc(60% - 120rpx);
				left: calc(50% - 360rpx);
				background: rgba(255, 0, 0, 0.6);
				border-radius: 20rpx;

				.flex-wrp {
					display: flex;
					flex-direction: column;
				}

				.flex-item {
					width: 120rpx;
					height: 120rpx;
					font-size: 24rpx;
					color: #fff;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					.img {
						width: 50rpx;
						height: 50rpx;
						margin-bottom: 10rpx;
					}
				}
			}

			.cover-bottom {
				z-index: 9;
				position: absolute;
				bottom: calc(60rpx);
				width: 80%;
				left: calc(9%);
			}

		}
		uni-popup{
			z-index: 999;
		}
		.popup-box{
			width: 100%;
			height: 320rpx;
			background-color: #F8F8F8;
			display: flex;
			flex-direction: column;
			.pop-top{
				width: 100%;
				font-size: 32rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				background-color: #fff;
				.pop-item{
					width: 80%;
					text-align: center;
					line-height: 100rpx;
				}
				.top-first{
					border-bottom: 1px solid #F8F8F8;
				}
			}
			.pop-cancle{
				width: 100%;
				flex: 1;
				margin-top: 20rpx;
				background-color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30rpx;
				color: #333;
			}
		}
	}
</style>
