// packageC/redPacket/red_amount/red_amount.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    page: 1,
    tag: "old",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.setNavigationBarColor({
    //   frontColor: "#ffffff",/*标题颜色，这里貌似仅支持 #ffffff 和 #000000 */
    //   backgroundColor: "#fe2064",/*背景色 十六进制即可*/
    // });
    if (options.tag) {
      this.setData({
        tag: options.tag,
      });
    }
    this.getData(this.data.page);
  },

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

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.current_page < that.data.last_page) {
      wx.showLoading({
        title: "加载中",
        mask: true,
      });
      that.setData({
        page: that.data.page + 1,
      });
      console.log(this.data.page);
      that.getNext(this.data.page);
      return;
    } else {
      wx.showToast({
        title: "没有更多",
        icon: "none",
      });
      return;
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getData(page) {
    let that = this;
    // let urlStr = app.getNetAddresss("plugin.red-packet.api.logs.quotaLogs");
    let urlStr;
    if (this.data.tag == "new") {
      urlStr = app.getNetAddresss("plugin.redpack-tool.frontend.quotaLog.getList");
    } else {
      urlStr = app.getNetAddresss("plugin.red-packet.api.logs.quotaLogs");
    }
    app._getNetWork({
      url: urlStr,
      data: {
        page: page,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (that.data.tag == "new") {
          that.setData({
            quotaLogs: res.data.quota_logs.data,
            member: res.data.member,
            receive_logs: res.data.quota_info,
            resmun: res.data.quota_info.surplus_total.toFixed(2),
            current_page: res.data.quota_logs.current_page,
            last_page: res.data.quota_logs.last_page,
          });
        } else {
          that.setData({
            quotaLogs: res.data.quotaLogs.data,
            member: res.data.member,
            receive_logs: res.data.receive_logs,
            resmun: (res.data.receive_logs.amount_sum - res.data.receive_logs.receive_amount).toFixed(2),
            current_page: res.data.quotaLogs.current_page,
            last_page: res.data.quotaLogs.last_page,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getNext(page) {
    let that = this;
    // let urlStr = app.getNetAddresss("plugin.red-packet.api.logs.quotaLogs");
    let urlStr;
    if (this.data.tag == "new") {
      urlStr = app.getNetAddresss("plugin.redpack-tool.frontend.quotaLog.getList");
    } else {
      urlStr = app.getNetAddresss("plugin.red-packet.api.logs.quotaLogs");
    }
    app._getNetWork({
      url: urlStr,
      data: {
        page: page,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (that.data.tag == "new") {
          if (res.data.quota_logs.current_page > res.data.quota_logs.last_page) {
            wx.hideLoading();
            wx.showToast({
              title: "没有更多",
              icon: "none",
            });
            that.setData({
              current_page: res.data.quota_logs.current_page,
              last_page: res.data.quota_logs.last_page,
            });
          } else {
            wx.hideLoading();
            that.setData({
              quotaLogs: that.data.quotaLogs.concat(res.data.quota_logs.data),
              current_page: res.data.quota_logs.current_page,
              last_page: res.data.quota_logs.last_page,
            });
          }
        } else {
          if (res.data.quotaLogs.current_page > res.data.quotaLogs.last_page) {
            wx.hideLoading();
            wx.showToast({
              title: "没有更多",
              icon: "none",
            });
            that.setData({
              current_page: res.data.quotaLogs.current_page,
              last_page: res.data.quotaLogs.last_page,
            });
          } else {
            wx.hideLoading();
            that.setData({
              quotaLogs: that.data.quotaLogs.concat(res.data.quotaLogs.data),
              member: res.data.member,
              receive_logs: res.data.receive_logs,
              current_page: res.data.quotaLogs.current_page,
              last_page: res.data.quotaLogs.last_page,
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});
