<template>
	<view class="content">
		<view id="map" class="map">
			<map @updated='mapChange' id="map_1" ref='map_1' style="width:750rpx; height: 600rpx" :latitude="latitude"
				:longitude="longitude" :markers="jhzmarker"></map>
		</view>
		<scroll-view scroll-y="true" class="scroll-wrapper" :style="{'height': height}" @scrolltolower="loadData">
			<espempty v-if="loaded && AEDList.length === 0" :tit="'救护站列表为空'"></espempty>
			<view class="list-tpl flex" @click="openAddress(item)" v-for="(item, index) in AEDList" :key="item.id">
				<template>
					<view class="list-left">
						<view class="info">
							<view class="title">{{ item.name }}</view>
							<view class="addr">地址：{{ item.address }}</view>
						</view>
					</view>
					<view class="image">
						<image src="../../static/img/img10.png"></image>
						<view class="tip" v-if="item.space > 999">>999m</view>
						<view class="tip" v-else>{{ item.space }}m</view>
					</view>
				</template>
			</view>
			<uni-load-more :status="loadtype"></uni-load-more>
		</scroll-view>
		<uni-popup ref="popup" type="bottom" @click="close">
			<view class="popup_row">
				<view class="rows">
					<view class="rows-item" @click="toGaodeMap">高德地图</view>
					<view class="rows-item" @click="tobaiDuMap">百度地图</view>
					<view class="rows-item" @click="totengxunMap">腾讯地图</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import espempty from '@/components/espempty';
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import {
		getdis,
		tocall,
		getListMechanism
	} from '@/api/index.js';
	import {
		userinfo
	} from '@/api/user.js';
	import {
		getcomAddress
	} from '@/api/index.js';
	export default {
		components: {
			uniPopup,
			espempty,
			uniLoadMore
		},
		//相关配置参数
		data() {
			return {
				loaded: false,
				jhzmarker: [],
				loadtype: 'more',
				page: 1,
				limit: 10,
				AEDList: [],
				height: '',
				list: [],
				latitude: '',
				longitude: '',
				current: 0,
				name: '',
				uid: '',
				name: '',
				phone: '',
				to_phone: '',
				marker: [],
				specificAdd: '',
				addressData: {
					name: '',
					mobile: '',
					latitude: 0, //纬度
					longitude: 0, //经度
					address: {
						province: '',
						city: '',
						district: '',
						detail: '',
					},
					area: '',
					default: false
				},
			}
		},
		onReady(res) {
			var _this = this;
			uni.getSystemInfo({
				success: resu => {
					const query = uni.createSelectorQuery();
					query.select('.scroll-wrapper').boundingClientRect();
					query.exec(function(res) {
						console.log(res, 'ddddddddddddd');
						_this.height = resu.windowHeight - res[0].top + 'px';
						console.log('打印页面的剩余高度', _this.height);
					});
				},
				fail: res => {}
			});
		},
		onLoad() {
			let obj = this;
			console.log('开始加载事件');
			// obj.loadBaseData();
			// uni.showLoading({
			// 	title: '加载中',
			// 	mask: true
			// });
			// obj.loadData();
			try {
				let locationAddress
				// #ifdef H5
				let wxOjb = require('jweixin-module');
				locationAddress = wxOjb.getLocation;
				// #endif
				// #ifdef MP
				locationAddress = uni.getLocation;
				// #endif
				wxOjb.ready(() => {
					console.log('加载完毕注册事件');
					locationAddress({
						type: 'gcj02',
						success: function(res) {
							console.log('获取经纬度', res);
							obj.longitude = res.longitude
							obj.latitude = res.latitude
							obj.marker = [{
								latitude: obj.latitude,
								longitude: obj.longitude,
								iconPath: '/static/img/img19.png',
								width: '45',
								height: '45',
							}, ]
							obj.loadData();
						},
						fail(e) {
							console.log('失败', e);
							window.location.reload();
						}
					});
				})

			} catch (e) {
				console.log(e);
				let locationAddress
				// #ifdef H5
				let wxOjb = require('jweixin-module');
				locationAddress = wxOjb.getLocation;
				// #endif
				// #ifdef MP
				locationAddress = uni.getLocation;
				// #endif
				wxOjb.ready(() => {
					console.log('加载完毕注册事件');

					locationAddress({
						type: 'gcj02',
						success: function(res) {
							console.log('获取经纬度1', res);
							obj.longitude = res.longitude
							obj.latitude = res.latitude
							obj.loadData();
						},
						fail(e) {
							console.log('失败', e);
						}
					});
				})

			}
		},
		computed: {
			...mapState('user', ['hasLogin', 'userInfo'])
		},
		methods: {
			...mapMutations('user', ['setUserInfo']),
			// 地图渲染完毕事件
			mapChange(e) {
				console.log(e);
			},
			// 载入数据
			async loadData() {
				let obj = this;
				if (obj.loadtype == 'loading' || obj.loadtype == 'noMore') {
					return
				}
				getListMechanism({
					page: obj.page,
					limit: obj.limit,
				}).then(({
					data
				}) => {
					uni.hideLoading();
					console.log(data, 999);
					for (let i = 0; i < data.length; i++) {
						data[i].space = obj.space(obj.latitude, obj.longitude, data[i].latitude, data[i]
							.longitude);
					}
					let arr = data.map(item => ({
						latitude: item.latitude,
						longitude: item.longitude,
						iconPath: '/static/img/img014.png',
						width: '35',
						height: '35',
						id: item.id,
						address: item.address
					}));
					obj.jhzmarker = obj.marker.concat(arr)
					obj.AEDList = obj.AEDList.concat(data);
					console.log(obj.AEDList, 'obj.AEDList+++++++');
					// obj.markerList = data;
					obj.loaded = true
					if (obj.limit == data.length) {
						obj.loadtype = 'more';
						obj.page++;
					} else {
						obj.loadtype = 'noMore';
					}
				})
			},
			space(lat1, lng1, lat2, lng2) {
				console.log(lat1, lng1, lat2, lng2);
				var radLat1 = (lat1 * Math.PI) / 180.0;
				var radLat2 = (lat2 * Math.PI) / 180.0;
				var a = radLat1 - radLat2;
				var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
				var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math
					.pow(Math.sin(b / 2), 2)));
				s = s * 6378.137;
				s = Math.round(s * 10000) / 10000;
				return s * 1000; // 单位米
			},
			openAddress(item) {
				let obj = this;
				obj.latitude2 = item.latitude;
				obj.longitude2 = item.longitude;
				obj.address = item.address;
				this.$refs.popup.open();
			},
			// 调用高德
			toGaodeMap() {
				uni.showLoading({
					title: '打开中..'
				})
				let latitude = this.latitude2;
				let longitude = this.longitude2;
				let address = this.address;
				console.log('选择高德', latitude, longitude, address);
				window.location.href = `https://uri.amap.com/marker?position=${longitude},${latitude}&name=${address}`;
			},
			// 调用腾讯
			totengxunMap() {
				uni.showLoading({
					title: '打开中..'
				})
				let latitude = this.latitude2;
				let longitude = this.longitude2;
				let address = this.address;
				console.log('选择腾讯', latitude, longitude);
				window.location.href =
					`http://apis.map.qq.com/uri/v1/marker?marker=coord:${latitude},${longitude};addr:${address}`;
			},
			// 调用百度
			tobaiDuMap() {
				uni.showLoading({
					title: '打开中..'
				})

				function bd_encrypt(gg_lng, gg_lat) {
					var X_PI = Math.PI * 3000.0 / 180.0;
					var x = gg_lng,
						y = gg_lat;
					var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
					var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
					var bd_lng = z * Math.cos(theta) + 0.0065;
					var bd_lat = z * Math.sin(theta) + 0.006;
					return {
						lat: bd_lat,
						lng: bd_lng
					}
				}
				let towhere = bd_encrypt(this.longitude2, this.latitude2)
				let formwhere = bd_encrypt(this.longitude, this.latitude)
				let latitude6 = towhere.lat;
				let longitude6 = towhere.lng;
				let latitude = formwhere.lat
				let longitude = formwhere.lng
				let address = this.address;
				console.log('选择百度', latitude, longitude);
				console.log('获取当前经纬度', latitude6, longitude6);
				window.location.href =
					`http://api.map.baidu.com/direction?origin=latlng:${latitude},${longitude}|name:我的位置&destination=${latitude6},${longitude6}&mode=driving&region=${address}&output=html&src=webapp.baidu.openAPIdemo`;
			},

		}
	}
</script>

<style lang="scss">
	body,
	page {
		height: 100%;
		width: 100%;
		background-color: #fff;
	}

	.content {
		height: 100%;
		width: 100%;
		background-color: #fff;
	}

	.map {
		width: 100%;
		height: 600rpx;
	}

	/* #ifdef H5 */
	.location {
		overflow-y: scroll; //溢出就滑动起来
		// z-index: 990;
		position: fixed;
		bottom: 0;
		height: 55%;
		width: 100%;
		background-color: #fff;
		padding: 24rpx;

		//搜索框
		.Search-box {
			// z-index: 999;
			height: 80rpx;
			// position: fixed;
			// 		top: 0;
			// 		left: 0;
			// 		width: 100%;
			// background: #FFFFFF;
			// padding-top: 10rpx;
			padding-right: 24rpx;
			padding-left: 24rpx;

			.Search-box-sort {
				float: left;
				margin-left: 32rpx;
				margin-right: 19rpx;

				.sort-text {
					width: 57rpx;
					height: 29rpx;
					font-size: 30rpx;
					font-weight: 500;
					color: rgba(51, 51, 51, 1);
					line-height: 58rpx;
					margin-right: 19rpx;
				}

				.sort-img {
					width: 21rpx;
					height: 11rpx;
					margin-bottom: 4rpx;
				}
			}

			.Search-box-size {
				// width:70%;
				height: 58rpx;
				border-radius: 32rpx;
				background-color: #f1f1f1;
				padding-left: 36rpx;
				display: flex;
				align-items: center;

				.box-img {
					height: 32rpx;
					width: 32rpx;
					margin-right: 16rpx;
				}

				// .box-right{
				// 	height: 58rpx;
				// 	// width: 100%;
				// 	background-color: pink;
				// }

				.box-word {
					width: 100%;
					font-size: 26rpx;
					font-weight: 500;
					color: rgba(205, 203, 203, 1);
					line-height: 55rpx;
				}
			}
		}

		.location-item {
			z-index: 1000;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1rpx solid #E7E8EA;
			padding: 15rpx 0;

			.box-left {
				display: flex;

				.img01 {
					width: 8rpx;
					height: 28rpx;
					margin-right: 18rpx;

					image {
						width: 8rpx;
						height: 28rpx;
					}
				}

				.img02 {
					width: 117rpx;
					height: 117rpx;
					border-radius: 50%;

					image {
						width: 117rpx;
						height: 117rpx;
						border-radius: 50%;
					}
				}

				.info {
					margin-left: 16rpx;

					.info-header {
						display: flex;
						align-items: center;

						.name {
							font-size: 32rpx;
							color: #333333;
						}

						.distance {
							margin-left: 25rpx;
							font-size: 28rpx;
							color: #303133;
						}

						.location-icon {
							margin-left: 10rpx;
							width: 20rpx;
							height: 26rpx;

							image {
								width: 20rpx;
								height: 26rpx;
								display: inline;
							}
						}
					}

					.address {
						font-size: 26rpx;
						color: #333333;
						margin-top: 33rpx;
					}
				}
			}

			.box-right {
				.img {
					height: 107rpx;

					image {
						width: 107rpx;
						height: 107rpx;
					}
				}

				.called {
					margin-right: 16rpx;
					height: 107rpx;
					line-height: 107rpx;
					font-size: 28rpx;
					color: #CB131C;
				}
			}
		}
	}

	/* #endif */

	.list-tpl {
		background-color: #ffffff;
		margin: 25rpx 0rpx;
		padding: 25rpx 25rpx;
		font-size: 28rpx;
		border-radius: 15rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1px solid #f1f1f1;

		.list-left {
			display: flex;
			width: 100%;

			.number {
				font-size: 32rpx;
				margin-right: 14rpx;
			}

			.info {
				width: 100%;

				.title {
					font-size: 32rpx;
					color: #222222;
					font-weight: 500;
				}

				.addr {
					width: 400rpx;
					margin-top: 20rpx;
					font-size: 20rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
				}
			}
		}

		.image {
			width: 10%;
			text-align: center;

			image {
				width: 50rpx;
				height: 50rpx;
			}

			.tip {
				color: #7f7f7f;
				font-size: 21rpx;
			}
		}
	}

	.popup_row {
		width: 100%;
		height: 500rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		.rows {
			width: 100%;
			padding: 0 24rpx;

			.rows-item {
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				width: 100%;
				font-size: 32rpx;
				color: #303133;
			}
		}
	}
</style>
