// branch.js
const app = getApp(),
  { hostname } = app.globalData,
  userInfo = wx.getStorageSync('userInfo')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDownline()
  },
  // 切换Tab
  switchTab(e) {
    let { id } = e.target
    switch (id) {
      case '0':
        this.setData({
          showAll: true,
          tabIndex: 0
        })
        break;
      case '1':
        this.setData({
          showAll: false,
          orderStatus: 0,
          tabIndex: 1
        })
        break;
      case '2':
        this.setData({
          showAll: false,
          orderStatus: 1,
          tabIndex: 2
        })
        break;
      case '3':
        this.setData({
          showAll: false,
          orderStatus: 2,
          tabIndex: 3
        })
        break;
    }
    // this.orderCounter()
  },
  getDownline() {
    let that = this
    wx.request({
      url: hostname + 'ShareLogic/share_friend',
      method: 'POST',
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      data: {
        // user_id: 100445
        user_id: wx.getStorageSync('users_id')

      },
      success: (res) => {
        // console.log(res.data)
        let downline = res.data.data
        console.log(downline)
        if (res.data.status == 1) {
          let downline = res.data.data
          // let two = downline.two
          // downline.two.forEach(item => {
          //   item.forEach(person =>
          //     two.push(person)
          //   )
          // })
          
          // downline.two = two
          let downlineCount = downline.one.length + downline.two.length
          that.setData({ downline, downlineCount })
        }
        // that.setData({ downline})
        setTimeout(() => wx.hideLoading(), 1000)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '邀请好友成为您的伙伴',
      path: '/pages/more/downLine/downLine?share_id=' + wx.getStorageSync('openid'),
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享失败',
          image: '/images/warning.png'
        })
      }
    }
  },
  
})