// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    list: [],
    info: {},
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.getTitle();
    this.getStoreInfo();
    this.getData();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

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

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

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      wx.showToast({
        title: '没有更多数据',
        icon: 'none'
      });
    }
  },

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

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {},
  getTitle() {
    // 兑换操作
    let urlStr = app.getNetAddresss('plugin.share-chain.frontend.plugin-name.index');
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          wx.setNavigationBarTitle({
            title: resdata.data.plugin_name ? resdata.data.plugin_name : ''
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getStoreInfo() {
    let urlStr = app.getNetAddresss('plugin.share-chain.frontend.process.index');
    urlStr += '&source_code=' + this.options.source_code + '&queue_id=' + this.options.queue_id;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            info: resdata.data
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none',
            duration: 1500,
            success: function() {
              setTimeout(function() {
                //要延时执行的代码
                wx.reLaunch({
                  url: '/packageG/pages/member/extension/extension'
                });
              }, 1500); //延迟时间
            },
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  // 获取数据
  getData() {
    let urlStr = app.getNetAddresss('plugin.share-chain.frontend.process.getList');
    urlStr += '&source_code=' + this.options.source_code + '&queue_id=' + this.options.queue_id;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            list: resdata.data.data
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none',
            duration: 1500,
            success: function() {
              setTimeout(function() {
                //要延时执行的代码
                wx.reLaunch({
                  url: '/packageG/pages/member/extension/extension'
                });
              }, 1500); //延迟时间
            },
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.setData({
      isLoadMore: false
    }); // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages
      });
      let urlStr = app.getNetAddresss('plugin.share-chain.frontend.process.getList');
      urlStr += '&source_code=' + this.options.source_code + '&queue_id=' + this.options.queue_id;
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          let resdata = res.data;
          if (resdata.result === 1) {
            let myData = this.data.list.concat(resdata.data.data);
            this.setData({
              list: myData
            });
          } else {
            let page = this.data.page - 1;
            this.setData({
              page,
              isLoadMore: false
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
});
