// packageE/ConsumePacket/index/index.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: "",
    list: [],
    base_data: {},

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
  },

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

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

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

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

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

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

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

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

  onChange(event) {
    if (event.detail.index === 1) {
      this.setData({
        active: "0",
      });
    } else if (event.detail.index === 2) {
      this.setData({
        active: "1",
      });
    } else if (event.detail.index === 3) {
      this.setData({
        active: "-1",
      });
    } else {
      this.setData({
        active: "",
      });
    }
    this.initData();
    this.getData();
  },
  initData() {
    this.setData({
      list: [],

      page: 1,
      total_page: 0,
      isLoadMore: true,
    });
  },
  //获取数据
  getData() {
    let that = this;
    let json = {
      status: this.data.active,
      page: 1,
    };
    let urlStr = app.getNetAddresss(
      "plugin.consume-red-packet.Frontend.Modules.Receive.Controllers.records.index"
    );
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            base_data: res.data.base_data,
            list: res.data.page_list.data,
            total_page: res.data.page_list.last_page,
            isLoadMore: true,
          });
          if (that.data.total_page <= 1) {
            that.setData({
              isLoadMore: false,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreData() {
    var that = this;
    if (this.data.page >= this.data.total_page) {
      that.setData({
        isLoadMore: false,
      });
    } else {
      that.setData({
        page: this.data.page + 1,
      });
      let json = {
        status: this.data.active,
        page: this.data.page,
      };
      let urlStr = app.getNetAddresss(
        "plugin.consume-red-packet.Frontend.Modules.Receive.Controllers.records.index"
      );
      app._getNetWork({
        url: urlStr,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              total_page: res.data.page_list.last_page,
              list: that.data.list.concat(res.data.page_list.data),
              isLoadMore: true,
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  },
});
