// pages/addressBook/addressBook.ts
import { getAction, postAction } from '../../api/base';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    indexList: [] as any,
    dataList: [] as any,
    scrollTop: 0,
    total: 0
  },
  onPageScroll(e: any) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },
  searchName(e: any) {
    console.log(e);
    this.getList({
      userName: e.detail.value
    });
  },
  getList(parms: any) {
    console.log(parms);
    if (parms.userName == '') {
      parms = {};
    }
    wx.showLoading({
      title: '数据加载中'
    });
    getAction('api/externalAddressBook/list', parms).then((res: any) => {
      wx.hideLoading();
      if (res.code == 200) {
        let indexList = [] as any;
        let dataList = [] as any;
        let total = 0;
        for (let key in res.result) {
          console.log();
          indexList.push(key);
          dataList.push({
            key: key,
            list: res.result[key]
          });
          total += res.result[key].length;
        }
        // res.result.forEach((key, item) => {
        //   console.log(item)
        //   indexList.push(key)
        //   dataList.push({
        //     key: key,
        //     list: res.result[key]
        //   })
        // });
        this.setData({
          indexList: indexList,
          dataList: dataList,
          total: total
        });
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      });
    }
    this.getList({});
  },

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      name: ''
    });
    this.getList({});

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

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

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