// shoppingGuid/page/pages/home/home.js
const wxService = require('../../../../utils/wxService')
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    navBar: [{ name: '招募榜', type: 1, isActive: true }, { name: '门店榜单', type: 2, isActive: false }],
    current : 1,
    staff : [],
    store : [],
    rankList:[],
    storeList:[],
    defaultAvatar: 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/home_default_logo.png',
    loadingText : '数据加载中...'
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRecruitRanking()  // 招募榜
  },
  //获取排行榜
  getRecruitRanking() {
    wx.showLoading({
      title: '加载中'
    })
    wxService.post('/marketing/shoppingguide/recruitRanking').then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          let list = data ? data : [];
          if(list.length == 0){
            this.data.loadingText = '暂无榜单~';
          }

          this.setData({
            rankList: list,
            loadingText : this.data.loadingText
          });
        }
      }
    })
  },
  // 门店榜
  getStoreRanking() {
    wx.showLoading({
      title: '加载中'
    })
    wxService.post('/marketing/shoppingguide/storeRanking').then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          this.setData({
            storeList: data || []
          })
        }
      }
    })
  },
  // 点击事件
  onTopBarTap(e){
    let index = e.currentTarget.dataset.index,
      item = e.currentTarget.dataset.item;
    this.data.navBar.forEach(nav => {
      nav.isActive = false;
    });

    this.data.navBar[index].isActive = true;
    this.data.current = this.data.navBar[index].type;
    
    this.setData({
      navBar: this.data.navBar,
      current: this.data.current
    },()=>{
      const { current } = this.data
      current == 1? this.getRecruitRanking() : this.getStoreRanking()
    });
  },
  //跳转到我的招募码
  onTapShowMyQrcode(){
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/qrcode/qrcode',
    })
  },

  // 回到商城
  onTapToMall(){
    wx.redirectTo({
      url: '/pages/userCenter/userCenter',
    })
  },
  //跳转到会员
  onTapToMember(){
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/selectUsers/selectUsers',
    });
  },

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

  },

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

  },
})