// 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'
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  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()
          this.setData({
            rankList: data || []
          })
        }
      }
    })
  },
  // 门店榜
  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()
    });
  },

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

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

  },

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

  },
})