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

  /**
   * 页面的初始数据
   */
  data: {
    customs: [],
    jumpNum: '',
    tel: '',
    total: 0,
    totalAmount: 0,
    totalDebt: 0,
    isfirst: true,
    ussindex: 0
  },
  ussChane(e) {
    this.setData({
      ussindex: e.detail.value
    })
    this.getAllCustom()
  },
  settel(e) {
    this.setData({
      tel: e.detail.value
    })
    this.getAllCustom()
  },
  todetail(e) {
    wx.navigateTo({
      url: '/pages/order/customdetail/customdetail?id=' + e.currentTarget.dataset.id + "&onlyID=" + e.currentTarget.dataset.onlyid,
    })
  },
  toedite(e) {
    wx.navigateTo({
      url: '/pages/order/addcustom/addcustom?id=' + e.currentTarget.dataset.id
    })
  },
  getDefaultBook() {
    util.query('accountbook/wxapi/getdefault', {}, function(res) {
      if (res.code === 10000) {
        res.data.userInfos.unshift({
          key: 0,
          wechatName: '全部'
        })
        that.setData({
          uss: res.data.userInfos
        })
      }
    })
  },
  toadd() {
    wx.navigateTo({
      url: '/pages/order/addcustom/addcustom',
    })
  },
  del(e) {
    wx.showModal({
      title: '确认',
      content: '是否确定删除客户',
      success(res) {
        if (res.confirm) {

          util.query("customer/wxapi/delete", {
            id: parseInt(e.currentTarget.dataset.id)
          }, function(res) {
            if (res.code == 10000) {
              util.showToast('删除成功')
              that.getAllCustom()
            } else {
              util.showToast(res.message)
            }
          }, "POST")
        } else if (res.cancel) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    //pyutil.getPinYinFirstCharacter('？吧#：&')
    this.getDefaultBook()
    this.getAllCustom()
  },
  getAllCustom() {
    var data = {
      pageNo: 1,
      pageSize: 9999,
      nameOrPhone: this.data.tel
    }
    if (this.data.ussindex != 0) {
      data.creatorUid = this.data.uss[this.data.ussindex].uid
    }
    util.query('customer/wxapi/listpage', data, function(res) {
      if (res.code === 10000) {
        var da = res.data.content
        var pys = []
        for (var i in da) {
          da[i].debt = da[i].debt.toFixed(2)
          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,
          total: res.total,
          totalAmount: res.totalAmount.toFixed(2),
          totalDebt: res.totalDebt.toFixed(2)
        })
      } else {
        that.setData({
          customs: [],
          total: 0,
          totalAmount: 0,
          totalDebt: 0
        })
      }
    })
  },
  setjumpNum(e) {
    this.setData({
      jumpNum: e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!this.data.isfirst) {
      this.getAllCustom()
      this.getDefaultBook()
    }
  },

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

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

  },

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

  },

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

  },

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

  }
})