// packageE/superior/superior.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    page:1,
    info:'',
    ind : null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdata();
  },
  shows(e){
    console.log(e);
    let inds = e.currentTarget.dataset.ind;
    if(this.data.ind == null){
      this.setData({
        ind:inds
      });
    }else{
      this.setData({
        ind:null
      });
    }
  },
  swichTabTItem(e){
    this.setData({
      active:e.detail.index,
      page:1,
      ind : null,
      list:[]
    });
    this.getdata();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.current_page >= this.data.last_page){
      return;
    }else{
      let pages = this.data.page + 1;
      this.setData({
        page : pages
      });
      this.getMore();
    }
  },

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

  },
  getdata() {
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    let urlStr = app.getNetAddresss('plugin.superior-reward.api.index.index');
    app._postNetWork({
      url: urlStr,
      data:{
        state:this.data.active,
        page:this.data.page
      },
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if(res.result == 1){
          console.log(res.data);
          wx.setNavigationBarTitle({
            title: res.data.custom_name?res.data.custom_name:'上级奖',
          });
          that.setData({
            info:res.data,
            list:res.data.list.data,
            current_page:res.data.list.current_page,
            last_page:res.data.list.last_page
          });
        }else{
          wx.showToast({
            title: res.msg,
            duration:1000,
            icon:'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getMore(){
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    let urlStr = app.getNetAddresss('plugin.superior-reward.api.index.index');
    app._postNetWork({
      url: urlStr,
      data:{
        state:this.data.active,
        page:this.data.page
      },
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if(res.result == 1){
          that.setData({
            info:res.data,
            list:that.data.list.concat(res.data.list.data),
            current_page:res.data.list.current_page,
            last_page:res.data.list.last_page
          });
        }else{
          wx.showToast({
            title: res.msg,
            duration:1000,
            icon:'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  }
});