// pages/yuezhan/search/index.js
const { request, serverName } = require('../../../utils/services');
const { Toast } = require('../../../utils/util.js');
const QQMapWX = require('../../../common/component/mapSDK/qqmap-wx-jssdk.min.js');
var qqmapsdk = "";
const app = getApp();

const WxParse = require('../../../common/component/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['国内', '国际'],
    testImg: '../../../imgs/testImg/thumbSmallImg2.jpg',
    avatar: '../../../imgs/testImg/1.jpg',
    activeIndex:0,
    domesticCityList:[],
    internationalCity:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: '2Z3BZ-H7EWO-F4YWX-SG5JF-2VOK2-S2FUB'
    });
    this.getLocationName();
    this.getDomesticCity();
    console.log(app.globalData.city);
    this.setData({
      cityAction: app.globalData.city
    })
  },

  tabClick: function (e) {
    this.setData({
      loading: true,
      activeIndex: e.currentTarget.id,
      domesticCityList: [],
      internationalCity: []
    });
    if (this.data.activeIndex==0){
      this.getDomesticCity();
    }
    else{
      this.getInternationalCity();
      
    }
  },

  clickItem:function(e){
    console.log(e.currentTarget)
    
    let { name } = e.currentTarget.dataset;
    console.log(name);
    app.globalData.city = name;
    app.globalData.clickToSelect = true;
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  getDomesticCity:function(){
    request["getDomesticCity"]({},"",res=>{
      if(res.data.code==0){
        let { data: domesticCityList} = res.data;
        this.setData({
          domesticCityList
        })
      }
      // console.log(domesticCityList)
    })
  },
  getLocationName:function(){
    // 调用接口
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        // console.log('0202', res)
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: (res) =>{
            let { city:locationName } = res.result.address_component;
            locationName = locationName.substring(0,2);
            this.getList(1, locationName)
            // app.globalData.city = locationName;
            this.setData({
              locationName
              
            })
          },
          fail: function (res) {
            console.log(res)
            this.setData({
              locationName:""
            })
          },
          complete: function (res) {
            console.log(res);
          }
        });
      }
    })
  },

  getList: function (page, locationName) {
    let loginSessionKey = wx.getStorageSync("token");
    console.log(loginSessionKey)
    let { latitude, longitude, activeDist, activeType } = this.data;

    this.setData({
      loading: true
    })
    request["getExhibitionListOffline"]({
      page: page,
      loginSessionKey,
      city: locationName||"",
      sort:0,
      type:0,
      lng: longitude,
      lat: latitude,
    }, '', res => {

      let { totalElements: exhibitionNum } = res.data.data;

      console.log(res)
      this.setData({
        exhibitionNum,
        loading: false,
      });
      wx.stopPullDownRefresh();


    }, err => {

    }, complete => {

    })
  },

  getInternationalCity(){
    request["getInternationalCity"]({}, "", res => {
      if (res.data.code == 0) {
        let { data: internationalCity } = res.data;
        this.setData({
          internationalCity
        })
      }
      // console.log(res)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})