// pages/order/custommanage/custommanage.js
let util = require('../../../utils/util.js');
let pyutil = require('../../../utils/pinyin.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customs: [],
    jumpNum: '',
    tel: ''
  },
  settel(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  select(e) {
    var u = e.currentTarget.dataset
    let pages = getCurrentPages()
    let prevPage = pages[pages.length - 2]
    prevPage.setData({
      addressname: u.name,
      customerid: u.cuid,
      customeronlyid: u.cuonlyid,
      addressid: u.id,
      addressonlyid: u.onlyid
    })
    prevPage.updatelast(u.cuid)
    wx.navigateBack({
      delta: 1
    })

  },
  todetail(e) {
    wx.navigateTo({
      url: '/pages/order/customdetail/customdetail?id=' + e.currentTarget.dataset.id,
    })
  },
  toadd() {
    wx.navigateTo({
      url: '/pages/order/addcustom/addcustom',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    //pyutil.getPinYinFirstCharacter('？吧#：&')

    this.getAllCustom()
  },
  getAllCustom() {
    util.query('customeraddress/wxapi/listpage', {
      pageNo: 1,
      pageSize: 9999,
      name: this.data.tel
    }, function(res) {
      if (res.code === 10000) {
        var da = res.data.content
        var pys = []
        for (var i in da) {

          var py = pyutil.getPinYinFirstCharacter(da[i].name).toUpperCase().substr(0, 1)
          py = util.checkABC(py) ? py : '其他'
          var isfind = false
          for (var j in pys) {
            if (pys[j].py === py) {
              isfind = true
              pys[j].data.push(da[i])
            }
          }
          if (!isfind) {
            pys.push({
              py: py,
              data: [da[i]]
            })
          }

        }
        // pys = Object.keys(pys).sort(function(a, b) {
        //   return pys[b].py - pys[a].py;
        // });
        // var sorted_keys_array = Object.keys(pys).sort((a, b) => {
        //   return datas[b].py - datas[a].py;
        // });
        pys = pys.sort(function(a, b) {
          return a.py.charCodeAt() - b.py.charCodeAt()
        })
        that.setData({
          customs: pys
        })
      } else {
        that.setData({
          customs: []
        })
      }
    })
  },
  setjumpNum(e) {
    this.setData({
      jumpNum: e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})