<template>
    <view>
        <city-select
            @cityClick="cityClick"
            :formatName="formatName"
            :activeCity="activeCity"
            :hotCity="hotCity"
            :obtainCitys="obtainCitys"
            :isSearch="true"
            ref="citys"
        ></city-select>
    </view>
</template>

<script>
import citys from '@/components/city-select/citySelect.js'
import citySelect from '@/components/city-select/city-select.vue'
// 腾讯地图
var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({key:'KX5BZ-B64RC-RO62W-AMWAZ-VVTC3-YAFXF'});
export default {
    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: '成都'
                },
				{
				    id: 4,
				    title: '长沙'
				},
				{
				    id: 5,
				    title: '贵阳'
				}
            ]
        }
    },
    components: {
        citySelect
    },
    created() {
		this.getCity()
		
        //动态更新数据
        // setTimeout(() => {
        //     //修改需要构建索引参数的名称
        //     this.formatName = 'cityName'
        //     //修改当前城市
        //     this.activeCity = {
        //         cityName: '南京',
        //         cityCode: 110100
        //     }
        //     //修改热门城市
        //     this.hotCity = [
        //         {
        //             cityName: '南京',
        //             cityCode: 110100
        //         },
        //         {
        //             cityName: '北京',
        //             cityCode: 110102
        //         }
        //     ]
        //     //修改构建索引数据
        //     this.obtainCitys = citys
        //     uni.showToast({
        //         icon: 'none',
        //         title: '更新数据成功',
        //         // #ifdef MP-WEIXIN
        //         duration: 3000,
        //         // #endif
        //         mask: true
        //     })
        // }, 5000)
    },
    methods: {
        cityClick(item) {
			uni.setStorageSync('city',item.title)
			uni.navigateBack()
        },
		getCity(){
			this.activeCity.title = '定位中'
			let that = this
			uni.getLocation({
			        type: 'wgs84',
			        geocode: true,
			        success: (res) => {
			            console.log("获取经纬度成功");
						// 解析地址
						qqmapsdk.reverseGeocoder({
						    location: {
						        latitude: res.latitude,
						        longitude: res.longitude
						    },
						    success: function(res) {
						        console.log("解析地址成功");
						        // 市
						        that.activeCity.title = res.result.ad_info.city.slice(0,2);
						    },
						    fail: function(res) {
								this.city = '定位失败'
						       
						    },
						    complete: function(res) {
						        console.log(res);
						    }
						})
			        },
			        fail: () => {
			            console.log("获取经纬度失败");
			        },
			        complete: () => {
			           
			        }
			    })
		},
    }
}
</script>

<style></style>