<template>
	<view class="setbox">
		<map
			id="container"
			class="map"
			:latitude="center.lat"
			:longitude="center.lng"
			@poitap="onClcikImgMap"
			@tap="onClcikImgMap"
			:markers="covers"
			scale="15"
			:style="'width: 750rpx;height:' + mapheight + 'px;'"
		></map>

		<view class="maptop">
			<view class="maptopflex">
				<image :src="serimg" class="maptopimg"></image>
				<input ref='input' class="maptopint" id="tipInputs" v-model="searchtxt" @input="searchKeyword" placeholder="请输入地点进行查找" />
			</view>
			<!-- 地图联想 -->
			<view class="addrSearch_list" >
				<view class="addrSearch_item" v-for="(item, index) in searchList" :key="index" @click="handleAddrClick(item)" @blur='getblur'>
					<view class="wrap_name">
						<!-- #ifdef H5 -->
						<text class="wrap_text">{{item.name.substring(0, item.name.indexOf(searchtxt))}}</text>
							<text class="wrap_text" style="color: #2797FF;">{{searchtxt}}</text>
							<text class="wrap_text">{{item.name.substring(item.name.indexOf(searchtxt) + searchtxt.length)}}</text>
						<!-- #endif -->
						<!-- #ifndef H5 -->
						<rich-text :nodes="richNodes(item)"></rich-text>
						<!-- #endif -->
					</view>
					<view class="wrap_addr">
						<text class="wrap_addrs">{{ item.addr }}</text>
					</view>
				</view>
			</view>
		</view>

		<view class="mapfot">
			<view class="mapfott"><text class="mapfottit">标点位置</text></view>
			<view class="mapfotx">
				<image :src="adrimg" class="mapfotimg"></image>
				<text class="mapfottxt" v-if="address">{{ address }}</text>
			</view>
			<view class="mapfotn" @click="getNextFn"><text class="mapfotntit">下一步</text></view>
		</view>
	</view>
</template>

<script>
import amapFile from '@/plugins/amap-wx.js';
import self from "@/utils/location.js"
// #ifdef APP
var myAmapFun = new amapFile.AMapWX({
	key: 'ccbe52b1ec5f66295fa4609c90a819b7',
	batch: true
	});
// #endif


	export default{
		data(){
			return{
				serimg:require("@/work/static/clock/serch.png"),
				adrimg:require("@/work/static/clock/adress.png"),
				mapimg:require("@/work/static/clock/map.png"),
				center:{
					lng:'',
					lat:'',
				},
				mapflag:false,
				// userId: this.$store.state.user.userId,
				// deptId: this.$store.state.user.deptId,
				address:'',
				searchtxt:'',
				type:'add',//新增还是修改,
				placeSearchComponent:'',
				autoCompleteComponent:'',
				map:'',
				//117.212232,31.83982
				// {
				// 	latitude: 31.839676,
				// 	longitude: 117.211954,
				// 	iconPath: require("@/work/static/clock/map.png"),
				// 	anchor:{x: 0.5, y: 0.5}
				// }
				covers: [],
				mapheight:0,
				mapContext:"",
				searchList:[],
				focus:false,
				location:'',
			}
		},
		onReady() {
			this.getcreateMap()
		},
		onLoad(e) {
			if(e.type=='update'){
				this.type='update'
				this.center.lng=e.lng;
				this.center.lat=e.lat;
				this.address=e.address;
			}else{
				if(e.address){
					this.type='update'
					this.center.lng=e.lng;
					this.center.lat=e.lat;
					this.address=e.address;
				}else{
					this.type='add';
				}
				
			}
			if(e.fromurl){
				this.fromurl=e.fromurl
			}
			console.log(this.type)
		},
		mounted() {
			var that=this;
			this.getinfo();
			if(this.type=='add'){
				this.getlocation()
			}else{				
				var lng=this.center.lng;
				var lat=this.center.lat;
				that.covers = [];
				that.covers = [{
					latitude: lat,
					longitude: lng,
					iconPath: that.mapimg,
					anchor:{x: 0.5, y: 0.5}
				}]
				// #ifdef APP
				that.getCity(lat,  lng)
				// #endif
			}
		},
		methods:{
			getNextFn(){
				if(this.type=='add'){
					if(this.fromurl&&this.fromurl=='rule'){
						var address={
							address:this.address,
							lng:this.center.lng,
							lat:this.center.lat,
						}
						uni.setStorageSync('location', address);
						uni.$emit('refreshaddr');
						uni.navigateBack({delta: 1});
					}else{
						uni.redirectTo({
							url: '/work/pages/clock/setswork?type=add&address='+this.address+'&lng='+this.center.lng+'&lat='+this.center.lat
						});
					}
					
				}else{
					var address={
						address:this.address,
						lng:this.center.lng,
						lat:this.center.lat,
					}
					uni.setStorageSync('location', address);
					uni.$emit('refreshaddr');
					uni.navigateBack({delta: 1});
				}
				
			},
			searchKeyword(){
				var that=this;
				if(this.searchtxt){
					// #ifdef H5
					AMap.plugin('AMap.PlaceSearch', function(){
						let autoOptions = {
							city:'全国',
							citylimit: true,
							pageIndex: 1,
							pageSize: 20,
						}
						let placeSearch = new AMap.PlaceSearch(autoOptions);
						placeSearch.search(that.searchtxt, function(status, result) {
							// 搜索成功时，result即是对应的匹配数据
							if (status === 'complete' && result.info === 'OK') {
								let items = result.poiList.pois;
								console.log(items,122)
								if(items.length > 0){
									var pois = [];
									for (let i in items) {
										var obj = {
											"name": items[i].name,
											"addr": items[i].address,
											"lng": items[i].location.lng,
											"lat": items[i].location.lat
										};
										pois.push(obj);
									}
									that.searchList = pois;
								}
							}else{
								that.searchList = [];
							}
						})
					})
					// #endif

					// #ifndef H5
					myAmapFun.getInputtips({
						keywords: that.searchtxt,
						city: '全国',
						citylimit: true,
						success: function(result){
							if(result.tips.length > 0){
								let pois = [];
								let items = result.tips;
								for (let i in items) {
									var lnglat = items[i].location.split(',');
									var obj = {
										"name": items[i].name,
										"addr": items[i].district + items[i].address,
										"lng": lnglat[0],
										"lat": lnglat[1]
									};
									pois.push(obj);
								}
								that.searchList = pois;
							}else{
								that.searchList = [];
							}
						},
						fail: function(info){
							that.searchList = [];
							console.log('fail-search', info)
						}
					})
					// #endif
				}
			},
			handleAddrClick(addr){
				console.log(this.$refs.input)
				var that=this;
				var lng=addr.lng;
				var lat=addr.lat;
				that.center.lng=lng;
				that.center.lat=lat;
				that.covers = [];
				that.covers = [{
					latitude: addr.lat,
					longitude: addr.lng,
					iconPath: that.mapimg,
					anchor:{x: 0.5, y: 0.5}
				}]
				that.address=addr.name || addr.addr;
				that.searchList=[];
				that.$refs.input.blur()
				// #ifdef APP
				that.getCity(lat,  lng)
				// #endif
			},
			richNodes(item){
			return [{
				children: [
					{
				type: 'text',
				attrs: {
				class:'wrap_text'
				},
				text: item.name.substring(0, item.name.indexOf(this.searchtxt))
				},
				{
				type: 'text',
				attrs: {
				class:'wrap_texts'
				},
				text: this.searchtxt
				},
				{
				type: 'text',
				attrs: {
				class:'wrap_text'
				},
				text: item.name.substring(item.name.indexOf(this.searchtxt) + this.searchtxt.length)
				}
				]
			}]
			},
			// 获取当前屏幕信息
			getinfo(){
				var that=this;
				uni.getSystemInfo({
					success(e) {
						that.mapheight=e.windowHeight;
					},
				})
			},
			getcreateMap(){
				//mapId 就是你在 map 标签中定义的 id
				this.mapContext = uni.createMapContext('container', this);
			},
			onClcikImgMap(e) {
				console.log(e,123)
				var that=this;
				var lng=e.detail.longitude;
				var lat=e.detail.latitude;
				that.center.lng=lng;
				that.center.lat=lat;
				that.covers = [];
				that.covers = [{
					latitude: e.detail.latitude,
					longitude: e.detail.longitude,
					iconPath: that.mapimg,
					anchor:{x: 0.5, y: 0.5}
				}]
				// #ifdef APP
				that.getCity(lat,  lng)
				// #endif
			},
			//获取中心点
			getCenterLanLat() {
				var that = this;
					this.mapContext.getCenterLocation({
					  type: 'gcj02',
					  success: res => {
					    that.center.lng=res.longitude;
					    that.center.lat=res.latitude;
						that.covers = [];
						that.covers = [{
							latitude: e.detail.latitude,
							longitude: e.detail.longitude,
							iconPath: that.mapimg,
							anchor:{x: 0.5, y: 0.5}
						}]
					    // #ifdef APP
						
					    that.getCity(res.latitude,  res.longitude)
					    // #endif
					  },
					  fail: err => {
					    console.log('获取当前地图中心的经纬度2', err);
					  },
					});

			},
			getCity(latitude, longitude) {
				var that = this;
				that.location = `${longitude},${latitude}`
				console.log(that.location)
				myAmapFun.getRegeo({
				//如果经纬度有问题会导致不进入回调方法，从而报错
					location: that.location,
					success: function(e) {
						//成功回调
						that.address='';
						that.address = e[0].regeocodeData.formatted_address; //详细地址
						console.log(that.address)
					},
					fail: function(info) {
						//失败回调
						console.log(info)
					}
				})
			},
			// 获取定位
			getlocation(){
				var that = this;
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: function(res) {
						that.center.lng=res.longitude;
						that.center.lat=res.latitude;
						// 设置中心点
						that.covers = [];
						that.covers = [{
							latitude: res.latitude,
							longitude: res.longitude,
							iconPath: that.mapimg,
							anchor:{x: 0.5, y: 0.5}
						}]

						// #ifdef H5
						var adr= address.province+address.city+address.district+address.street+address.streetNum+address.poiName
						// that.address=res.longitude
						that.address=adr
						// #endif
						// #ifdef APP
						that.getCity(res.latitude,  res.longitude)
						// #endif

					},
					fail: function(red) {
						uni.showToast({
							title:"获取定位失败"
						})
					},
				})
			},
			// 点击刷新方法
			refresh(e){
				this.getlocation();
			},

		}
	}
</script>

<style lang="scss" scoped>
.maptop {
	position: fixed;
	left: 34rpx;
	right: 34rpx;
	top: 24rpx;
	max-height: calc(100vh - 400rpx);
	display: flex;flex-direction: column;
}
.maptopflex {
	flex: 0 0 auto;
	display: flex;
	align-items: center;
	padding: 0 32rpx;
	box-sizing: border-box;
	flex-direction: row;
	width: 684rpx;
	height: 84rpx;
	background: #ffffff;
	box-shadow: 0px 0px 12rpx 0px rgba(75, 75, 75, 0.35);
	border-radius: 6rpx;
	margin-bottom: 10rpx;
}
.maptopimg {
	width: 36rpx;
	height: 38rpx;
	margin-right: 16rpx;
}
.maptopint {
	font-size: 26rpx;
	color: #666666;
	flex: 1;
	height: 84rpx;
}
.mapfot {
	position: fixed;
	left: 34rpx;
	right: 34rpx;
	bottom: 24rpx;
	background: #fff;
	box-shadow: 0px 0px 12rpx 0px rgba(75, 75, 75, 0.35);
	border-radius: 6rpx;
	padding: 40rpx 32rpx 32rpx;
	height: 300rpx;
}
.mapfott {
	margin-bottom: 28rpx;
}
.mapfottit {
	font-size: 28rpx;
	font-weight: bold;
	color: #343434;
}
.mapfotx {
	display: flex;
	margin-bottom: 32rpx;
	min-height: 70rpx;
	flex-direction: row;
}

.mapfotimg {
	width: 26rpx;
	height: 28rpx;
	flex: 0 0 auto;
	margin-right: 12rpx;
}
.mapfottxt {
	flex: 1;
	font-size: 26rpx;
	color: #343434;
	line-height: 32rpx;
}
.mapfotn {
	width: 618rpx;
	height: 76rpx;
	background: #1678ff;
	border-radius: 6rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
.mapfotntit {
	font-size: 28rpx;
	color: #fff;
}
.addrSearch_list{flex: 1;overflow: auto;}
.addrSearch_item {
	border-bottom: 2upx solid #f5f5f5;
	padding: 4upx 12rpx;
	background: #ffffff;
	.wrap_name {
		display: flex;
		flex-direction: row !important;
	}
	.wrap_addr {
		margin-top: 2upx;
	}
}
.wrap_text {
	font-size: 30upx;
	color: #151515;
	line-height: 40upx;
}
.wrap_texts{
	font-size: 34upx;
	color: #2797FF;
	line-height: 40upx;
}
.wrap_addrs {
	font-size: 26upx;
	color: #999;
	line-height: 32upx;
}
</style>
