<template>
	<view class="localSearchBox" >
		<view class="header">
			<!-- <text @click="back()">取消</text> -->
			<!-- <text>选择位置</text> -->
			<text></text>
		</view>
		<view class="searchBox">
			<uni-easyinput prefixIcon="search" @confirm="searchAddress" placeholder="请输入地址" ></uni-easyinput>
		</view>
		<view class="listBox">
			<view class="listItem" v-for="(item, index) in list" :key="item.uid" @click="addressClick(index)">
				<view class="shortAddress">
					<view class="shortTitle">{{ item.name }}</view>
					<image
						src="../../static/image/checked.png"
						v-if="defaultIndex === index"
						class="checkedImg"
					></image>
				</view>
				<view class="realAddress">{{ item.address }}</view>
			</view>
		</view>
	</view>
</template>

<script>
import bmap from '../../utils/bmap-wx.js'
import { config } from '../../utils/config.js'
export default {
	name: 'localSearch',
	data() {
		return {
			defaultIndex: -1,
			list: [],
			BMap: null,
			latlngStr: ''
		};
	},
	methods: {
		initIndex() {
			this.defaultIndex = 0
		},
		// back() {
		// 	this.$emit('closeSelectAddress', {});
		// },
		submitAddress() {
			let address = this.list[this.defaultIndex].address
			this.$emit('changeAddress', address);
		},
		addressClick(index) {
			this.defaultIndex = index;
			this.submitAddress()
		},
		getLocation() {
			uni.getLocation({
				type: 'gcj02', //返回可以用于uni.openLocation的经纬度
				success: res => {
					console.log(res,'res')
					this.latlngStr = `${res.latitude},${res.longitude}`
					this.getLocationInfo()
				}
			});
		},
		searchAddress(e) {
			this.getLocationInfo(e)
		},
		getLocationInfo(addressKey = '') {
			if(addressKey == "") {
				this.BMap.search({
					"query": addressKey,
					location: this.latlngStr,
					page_size: 20,
					page_index: 1,
					success: res => {
						console.log(res);
						this.list = res.originalData.results
					},
					fail: err => {
						console.log(err);
					}
				})
			}else {
				this.BMap.suggestion({
					"query": addressKey,
					success: res => {
						console.log(res);
						this.list = res.result || []
					},
					fail: err => {
						console.log(err);
					}
				})
			}
		}
	},
	created() {
		this.BMap = new bmap.BMapWX({ 
				ak: config.BMapKey
		});
		this.getLocation()
	}
};
</script>

<style lang="scss">
.localSearchBox {
	box-sizing: border-box;
	padding: 0 32rpx;
	width: 100%;
	height: 100%;
	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 50px;
	}
	.searchBox{
		height: 36px;
	}
	.listBox {
		width: 100%;
		height: calc(100% - 80px);
		overflow-x: hidden;
		overflow-y: scroll;
		box-sizing: border-box;
		padding-bottom: 10px;
		.listItem {
			margin-top: 25px;
			.shortAddress {
				color: #151b26;
				display: flex;
				font-size: 16px;
				font-weight: 400;
				line-height: 24px;
				.shortTitle {
					flex: 1;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
				.checkedImg {
					height: 24px;
					width: 24px;
				}
			}
			.realAddress {
				color: #5c5f66;
				font-size: 14px;
				font-weight: 400;
				line-height: 20px;
				margin-top: 12px;
			}
		}
	}
}
</style>
