const util = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    list:[],
    query: { isSelectCity:false},
    alpha: '',
    windowHeight: '',
    addBg: false
  },
  onReady(){
    var res = wx.getSystemInfoSync();
    this.setData({ windowHeight: res.windowHeight })
  },
  onLoad: function (option) {
    var self = this;
    if (option.query){
      self.setData({ query: { isSelectCity:true,url:option.url} });
    }
    // util.loadCity(function(res){
      // self.setData({ city: res.data.city });
    // }, 1);
    self.getCityList();
  },
  getCityList:function(){
    var that = this;
    util.ajax({
      func:"city_list"
    },function(res){
      if (res.code == 0) {
        that.setData({ list: res.data.pinyin });
        var query = wx.createSelectorQuery();
        query.select('#selector').boundingClientRect();
        query.exec(function (res) {
          that.apHeight = Math.ceil(res[0].top + res[0].height) / 20;
          that.apTop = res[0].top;
        });
      }
    });
  },
  handlerAlphaTap(e) {
    let { ap } = e.target.dataset;
    this.setData({ alpha: ap });
  },
  //滑动
  handlerMove(e) {
    let { list } = this.data;
    this.setData({ addBg: true });
    let rY = e.touches[0].clientY;//竖向滑动的距离
    if (rY >= 0) {
      let index = Math.ceil(Math.ceil(rY - this.apTop - this.apHeight) / this.apHeight);
      if (0 <= index < list.length) {
        let nonwAp = list[index];
        nonwAp && this.setData({ alpha: nonwAp.letter.toUpperCase() });
      }
    }
  },
  //滑动结束
  handlerEnd(e) {
    this.setData({ addBg: false });
  },
  setCity: function (event){
    var city = app.globalData.city;
    if (util.isEmpty(city)){
      city = {
        city:''
      };
    }
    app.globalData.previous = city.city;
    city.city = event.currentTarget.dataset.city;
    getApp().globalData.city = city;
    wx.setStorageSync("city", city);
    if (!this.data.query.isSelectCity){
      wx.navigateBack();
    }else{
      wx.reLaunch({
        url: this.data.query.url
      });
    }
  }
})