// packageE/loveRanking/loveRanking.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    userObj: {},
    love_title: "",
    loveName: "爱心值",
    isLoadMore: true,
    total_page: 0,
    page: 1,
  },

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

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

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },

  _getMoreData() {
    let that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let urlStr = app.getNetAddresss("plugin.love-ranking.api.index.index");
      this.data.page++;
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result != 1) return;
          let love_ranking = res.data.getSet.love_ranking;
          if (
            love_ranking.ranking.current_page >= love_ranking.ranking.last_page
          ) {
            that.setData({
              isLoadMore: false,
            });
          }
          let listData = this.data.listData;
          listData.push(...love_ranking.ranking.data);
          this.setData({ listData });
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getData() {
    let urlStr = app.getNetAddresss("plugin.love-ranking.api.index.index");
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) {
          return;
        }
        let love_ranking = res.data.getSet.love_ranking;

        let listData = love_ranking.ranking.data;
        this.data.total_page = love_ranking.ranking.last_page;
        if (
          love_ranking.ranking.current_page >= love_ranking.ranking.last_page
        ) {
          that.setData({
            isLoadMore: false,
          });
        }

        let obj = {};
        obj.avatar = love_ranking.information.avatar;
        obj.nickname = love_ranking.information.nickname;
        obj.countLove = love_ranking.countLove;
        obj.countNO = love_ranking.countNO;

        this.setData({
          listData,
          loveName: res.data.getSet.title,
          userObj: obj,
        });
        if (res.data.getSet.love_title != "") {
          wx.setNavigationBarTitle({
            title: res.data.getSet.love_title,
          });
        }
      },
    });
  },
  gotoBusinessCard(evt) {
    let card = evt.currentTarget.dataset.card;
    console.log(card);
    if (!card || typeof card == "undefined") {
      wx.showToast({
        icon: "none",
        title: "该会员没有对应的名片",
      });
      return;
    }
    //packageB/member/business_card/BusinessCard/BusinessCard
    //mark=card&card_id=9
    wx.navigateTo({
      url:
        "/packageB/member/business_card/BusinessCard/BusinessCard?mark=card&card_id=" +
        card,
    });
  },
});
