// pages/o2oLocation/o2oLocation.js
var app = getApp();
// import utils from '../../../utils/city.js';
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    tag: '',
    sixCity: [],
    citys: [],
    city: '',
    JumpArray: [],
    position_switch:false
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.tag == "activityHome" || options.tag == "activitySearch" || options.tag == 'activitySort' ) {
      this.setData({
        tag: options.tag,
        isHome:true,
      });
    }else if(options.tag == "search"){
      this.setData({
        tag: options.tag,
        isHome:true,
      });
    }else if(options.tag == "storeConsume"){
      this.setData({
        tag: options.tag,
        id:options.id,
      });
    }else if (options.tag) {
      this.setData({
        tag: options.tag
      });
    }
    this._getSixCityList();
    this._getCityList();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  _getSixCityList() {
    let api = '';
    let that = this;
    if (this.data.tag == "hotel") {
      api = "plugin.hotel.frontend.hotel.hotel.get-hot-citys";
    }else if(this.data.tag == "easyRefuel"){
      api = "plugin.easy-refuel.frontend.index.getHotCitys";
    }else if(this.data.tag == "activityHome" || this.data.tag == "activitySearch" || this.data.tag == 'activitySort'){
      api = "plugin.activity-apply.api.index.getHotCitys";
    }else if(this.data.tag == "communityStore" || this.data.tag == "communityStoreDetails"){  //社区门店
      api = "plugin.store-community.frontend.community.getHotCitys";
    } else {
      api = 'plugin.store-cashier.frontend.store.store.getHotCitys';
    }
    let urlStr = app.getNetAddresss(api);
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            sixCity: res.data.citys
          });
          // console.log(that.data.sixCity,res.data.citys);
          try{
            if(res.data.position_switch == '1'){
              that.setData({
                position_switch:true
              });
            }
          }catch(err){
            console.log(err);
          }

        } else {
          console.log(res.msg);
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  _getCityList() {
    let api = '';
    let that = this;
    if (this.data.tag == "hotel") {
      api = "plugin.hotel.frontend.hotel.hotel.get-city-initials";
    }else if(this.data.tag == "easyRefuel"){
      api = "plugin.easy-refuel.frontend.index.getCitys";
    } else if(this.data.tag == "activityHome" || this.data.tag == "activitySearch" || this.data.tag == 'activitySort'){
      api = "plugin.activity-apply.api.index.getCityInitials";
    } else if(this.data.tag == "communityStore" || this.data.tab=="communityStoreDetails"){
      api = "plugin.store-community.frontend.community.getCityInitials";
    }else {
      api = 'plugin.store-cashier.frontend.store.store.getCityInitials';
    }
    let urlStr = app.getNetAddresss(api);
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            citys: res.data.initials
          });
          let shortcutListData = res.data.initials.map((group) => {
            return String(group.title).substr(0, 1);
          });
          that.setData({
            shortcutList: shortcutListData
          });
          that.Jumpinit();
        } else {
          console.log(res.msg);
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  goback() {
    wx.navigateBack({
      delta: 1
    });
  },
  tosearch() {
    if(this.data.tag == 'storeConsume'){
      // 门店消费卡进来的
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=' + this.data.tag+'&id='+this.data.id
      });
      return;
    }
    if (this.data.tag) {
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=' + this.data.tag
      });
    } else {
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc'
      });
    }
  },
  toGeolocation() {
    if(this.data.tag == 'storeConsume'){
      // 门店消费卡进来的
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=' + this.data.tag+'&id='+this.data.id
      });
      return;
    }
    if (this.data.tag) {
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=' + this.data.tag
      });
    } else {
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc'
      });
    }
  },
  setCity(e) {
    let areaname = e.currentTarget.dataset.fulladdress;
    console.log(e);
    if(!areaname){
      this.setData({
        city: ''
      });
      wx.setStorage({
        key: "address-point",
        data: { point: { lat: '0', lng: '0' }, city: '' }
      });
      let mapmessageData = {
        title: '',
        message: '',
        latitude: '',
        longitude: '',
        city: ''
      };
      if(this.data.tag == 'home'){
        wx.reLaunch({
          url: '/packageG/index/index?addressData=' + JSON.stringify(this.data.mapmessageData)
        });
      }else if(this.data.tag == 'diy'){
        let urls = wx.getStorageSync('diyurl');
        wx.reLaunch({url:urls});
      }else{
        wx.reLaunch({
          url: '/packageC/o2o/o2oHome/o2oHome?addressData=' + JSON.stringify(mapmessageData)
        });
      }
    }else if (this.data.tag) {
      this.setData({
        city: areaname
      });
      if (this.data.tag == "activityHome" || this.data.tag == "activitySearch" || this.data.tag == 'activitySort') {
        wx.navigateTo({
          url: '/packageC/o2o/o2oCity/o2oCity?tag=' + this.data.tag+'&city=' + this.data.city
        });
      }else if(this.data.tag == "storeConsume"){
        wx.navigateTo({
          url: '/packageC/o2o/o2oCity/o2oCity?tag=' + this.data.tag+'&city=' + this.data.city+'&id='+this.data.id
        });
      }else{
        wx.navigateTo({
          url: '/packageC/o2o/o2oCity/o2oCity?tag=' + this.data.tag+'&city=' + this.data.city
        });
      }
    } else {
      this.setData({
        city: areaname
      });
      wx.navigateTo({
        url: '/packageC/o2o/o2oCity/o2oCity?city=' + this.data.city
      });
    }
  },
  Jump(e) {
    let index = e.currentTarget.dataset.index;
    // let text = e.currentTarget.dataset.text;
    wx.pageScrollTo({
      scrollTop: this.data.JumpArray[index],
      duration: 300
    });
  },
  Jumpinit() {
    let that = this;
    const query = wx.createSelectorQuery();
    query.selectAll('.d_jump').boundingClientRect();
    query.selectViewport().scrollOffset();
    query.exec(
      function(res) {
        let resData = res["0"];
        let JumpArray = [];
        JumpArray = resData.map(function(val) {
          return val.top;
        });
        that.setData({
          JumpArray: JumpArray
        });
      }
    );
  }
});
