<template>
	<view>
		<view id="map" class='map' />
		<view>
			<!-- 普通弹窗 -->
			<uni-popup ref="popup" :is-mask-click="false" background-color="#fff" @change="onPopupChange">
				<view class="popup-content" :class="{ 'popup-bottom': popupType === 'bottom', 'popup-right': popupType === 'right' }">
					<text class="text">popup 内容</text>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script module="leaflet" lang="renderjs">
	import 'leaflet/dist/leaflet.css'
	import '@/utils/map/leafletHelper.css'

	import {
		DefaultRequest,
		IdRequest
	} from "@/grpc/base_pb.js"

	// import mapHelper from '@/utils/mapHelper'
	import leafletHelper from '@/utils/map/leafletHelper'

	export default {
		components: {},
		data() {
			return {
				popupType: 'bottom',
				popupShow: false,
				map: null,
				checkPoints: [{
						cp_id: 1, // 检查点ID
						serial_num: 1, // 序号
						c_type: 1, // 检查点类型 1：起始点 2：途经点 3：结束点 4：其他
						category: 1, // 检查点种类 1：实体点 2：虚拟点 3：VR点
						latitude: 36.67205,
						longitude: 117.126945
					},
					{
						cp_id: 2,
						serial_num: 2,
						c_type: 1,
						category: 1,
						latitude: 36.670871,
						longitude: 117.12725
					},
					{
						cp_id: 3,
						serial_num: 3,
						c_type: 1,
						category: 1,
						latitude: 36.671723,
						longitude: 117.128565
					},
					{
						cp_id: 4,
						serial_num: 4,
						c_type: 1,
						category: 1,
						latitude: 36.672704,
						longitude: 117.128415
					},
					{
						cp_id: 5,
						serial_num: 5,
						c_type: 1,
						category: 1,
						latitude: 36.672003,
						longitude: 117.129305
					},
					{
						cp_id: 6,
						serial_num: 6,
						c_type: 1,
						category: 1,
						latitude: 36.672416,
						longitude: 117.127116
					},
					{
						cp_id: 7,
						serial_num: 7,
						c_type: 1,
						category: 1,
						latitude: 36.671689,
						longitude: 117.127798
					},
					{
						cp_id: 8,
						serial_num: 8,
						c_type: 1,
						category: 1,
						latitude: 36.67094,
						longitude: 117.128522
					},
				],
				players: [{
						id: 1,
						nickName: '小鱼儿',
						phone: '13105310001',
					},
					{
						id: 2,
						nickName: '花无缺',
						phone: '13105310002',
					},
					{
						id: 3,
						nickName: '李大嘴',
						phone: '13105310003',
					},
					{
						id: 4,
						nickName: '叶南天',
						phone: '13105310004',
					},
				],
				players_position: [{
						id: 1,
						lat: 36.671357,
						lng: 117.127925
					},
					{
						id: 2,
						lat: 36.671657,
						lng: 117.125925
					},
					{
						id: 3,
						lat: 36.672257,
						lng: 117.128925
					},
					{
						id: 4,
						lat: 36.670433,
						lng: 117.127873
					}
				],
				shopId: 0,
				mapUrl: null,
				mapInfo: {},
				shopList: [],
				controlInfoList: []
			}
		},
		computed: {},
		mounted() {
			this.handleDrawMap()
		},
		onLoad() {
			// this.getShopList()
			// this.getShopMap(8)
		},
		beforeDestroy() {
			// console.log("beforeDestroy")
			leafletHelper.free()
		},
		methods: {
			async getShopMap(shopId) {
				try {
					// 创建请求参数并赋值
					var request = new IdRequest()
					request.setId(shopId)
					// 调用客户端相应的grpc方法，发送grpc请求，并接受后台发送回来的返回值
					this.$client.assMapInfoDetail(request, {}, async (err, response) => {
						if (err) {
							console.log(`[assMapInfoDetail] err: code = ${err.code}` +
								`, message = "${err.message}"`)
						} else {
							let res = response.toObject()
							console.log('[assMapInfoDetail]', res)
							this.mapInfo = res

							// await mapHelper.handleMapInfo(this, this.mapInfo)
							this.handleDrawMap()
						}
					})
				} catch (e) {
					console.log('[getShopMap] err', e)
				}
			},
			handleDrawMap() {
				leafletHelper.init(this, 'map', [36.67175772119628, 117.12792580603369], 17)
				leafletHelper.global.setCheckPoints(this.checkPoints)
				leafletHelper.global.setPlayers(this.players)
				leafletHelper.global.setPlayersPosition(this.players_position)
				leafletHelper.addMapLayer(8)

				leafletHelper.checkPoint.drawAllCheckPoints()
				leafletHelper.checkPoint.drawPath()

				leafletHelper.player.drawAllPlayers()
				// leafletHelper.player.drawOneTrail(1, 10000, true)
				// leafletHelper.player.drawOneTrail(2, 0, false)
				leafletHelper.player.drawAllTrails(5000)
				
				leafletHelper.checkPoint.toggle(true)
				leafletHelper.player.togglePlayer(true)
				leafletHelper.player.toggleTooltip(true)
				leafletHelper.player.toggleTrail(true)
			},
			//type: 弹出方式 [ top center bottom left right ]
			popup(type) {
				this.type = type
				if (!this.popupShow) {
					this.$refs.popup.open(type)
					this.popupShow = true
				} else {
					this.$refs.popup.close()
					this.popupShow = false
				}
			},
			onPopupChange(e) {
				console.log('[onPopupChange] 当前模式：' + e.type + ', 状态：' + e.show);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.map {
		width: 100vw;
		// height: 100vh;
		height: 95vh;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: white;
	}

	// @mixin flex {
	// 	/* #ifndef APP-NVUE */
	// 	display: flex;
	// 	/* #endif */
	// 	flex-direction: row;
	// }

	// @mixin height {
	// 	/* #ifndef APP-NVUE */
	// 	height: 100%;
	// 	/* #endif */
	// 	/* #ifdef APP-NVUE */
	// 	flex: 1;
	// 	/* #endif */
	// }

	.popup-bottom {
		width: 100%;
		height: 200px;
	}

	.popup-right {
		// @include height;
		width: 200px;
		height: 100%;
	}
</style>