<template>
	<RefreshView ref="mescrollRef" :hasBack="true" @backClick="backClick" :isInterceptBack="true" text="选择城市" :useDownScroll="false"
		:useUpScroll="false" pageBg="#F1F2F5">
	<view style="padding-bottom: 200rpx;">
		<city-select @cityClick="cityClick" :formatName="formatName" :activeCity="activeCity" :hotCity="hotCity"
			:obtainCitys="obtainCitys" :isSearch="true" ref="citys"></city-select>
	</view>
	</RefreshView>
</template>

<script>
	import citys from '@/components/city-select/citys.js'
	import citySelect from '@/components/city-select/city-select.vue'
	export default {
		components: {
			citySelect
		},
		data() {
			return {
				//需要构建索引参数的名称（注意：传递的对象里面必须要有这个名称的参数）
				formatName: 'title',
				//当前城市
				activeCity: {
					id: 1,
					title: '南京市'
				},
				//热门城市
				hotCity: [{
						id: 0,
						title: '南京市'
					},
					{
						id: 1,
						title: '南京市'
					}
				],
				//显示的城市数据
				obtainCitys: [{
						id: 0,
						title: '南京'
					},
					{
						id: 1,
						title: '北京'
					},
					{
						id: 2,
						title: '天津'
					},
					{
						id: 3,
						title: '东京'
					}
				],
				location: {
					city: "",
					code: ""
				}
			}
		},
		onLoad: function(option) {
			console.log(option);
			this.location.city = option.city
			this.location.code = option.code
			
			//修改需要构建索引参数的名称
			this.formatName = 'cityName'
			//修改当前城市
			this.activeCity = {
				cityName: '正在定位',
				cityCode: 0
			}
			//修改热门城市
			this.hotCity = [{
					cityName: '北京',
					cityCode: 110000
				},
				{
					cityName: '上海',
					cityCode: 310000
				}, {
					cityName: '广州',
					cityCode: 440100
				}, {
					cityName: '深圳',
					cityCode: 440300
				}, {
					cityName: '杭州',
					cityCode: 330100
				},
			]
			//修改构建索引数据
			this.obtainCitys = citys
			let _this = this
			uni.getLocation({
				type: 'gcj02',
				success: function(res) {

					let url =
						"https://restapi.amap.com/v3/geocode/regeo?key=59970402d1c3f7dc1efff17d4dfcff21&location=" +
						res.longitude + "," + res.latitude +
						"&poitype=&radius=1000&extensions=all&batch=false&roadlevel=0";

					_this.HttpOtherUrl({
						url: url
					}).then((res) => {

						let json = JSON.stringify(res);
						let info = JSON.parse(json).regeocode.addressComponent;
						// 城市
						let city = info.city;
						// 城市编码
						let code = info.adcode;

						//修改需要构建索引参数的名称
						_this.formatName = 'cityName'
						//修改当前城市
						_this.activeCity = {
							cityName: city,
							cityCode: code
						}
					})
				},
				fail() {
					//修改当前城市
					_this.activeCity = {
						cityName: '定位失败',
						cityCode: 0
					}
				}
			});
		},
		methods: {
			backClick(){
				console.log("location：", this.location);
				this.SetResult(this.location)
			},
			cityClick(item) {
				let json = JSON.stringify(item);

				if (JSON.parse(json).cityCode == 0) {
					this.Toast("当前选择无效")
				} else {
					console.log("city：", JSON.parse(json).cityName);
					console.log("code：", JSON.parse(json).cityCode);

					this.location.city = JSON.parse(json).cityName,
						this.location.code = JSON.parse(json).cityCode

					this.SetResult(this.location)
					// uni.$emit('location', {
					// 	city: JSON.parse(json).cityName,
					// 	code: JSON.parse(json).cityCode
					// });
					// this.Back()
				}
			}
		}
	}
</script>

<style>

</style>
