// packageE/ConsumePacket/index/index.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: "",
    list: [],
    base_data: {},
    sum_settled: "", //已结算总金额
    sum_unsettled: "", //未结算总金额
    receiveBonus: {}, //领取储备金

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
    this.initData();
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  onShareTimeline() {
    return {
      title: '储备基金',
    };
  },

  //切换tab
  onChange(event) {
    if (event.detail.index === 1) {
      this.setData({
        active: "unsettled",
      });
    } else if (event.detail.index === 2) {
      this.setData({
        active: "settled",
      });
    } else if (event.detail.index === 3) {
      this.setData({
        active: "invalid",
      });
    } else {
      this.setData({
        active: "",
      });
    }
    this.initData();
    this.getData();
  },

  //跳转
  toUrl() {
    let price = this.data.receiveBonus.price;
    let from_id = this.data.receiveBonus.from_id;
    console.log(from_id);
    wx.navigateTo({
      url: 
      "/packageH/reserveFunds/reserveFundsSuccess/reserveFundsSuccess?price=" +
      price + 
      "&from_id=" + 
      from_id,
    });
  },

  //初始化数据
  initData() {
    this.setData({
      list: [],
      page: 1,
      total_page: 0,
      isLoadMore: true,
    });
  },

  //获取数据
  getData() {
    let that = this;
    let json = {
      is_type: this.data.active,
      page: 1,
    };
    let urlStr = app.getNetAddresss(
      "plugin.reserve-fund.api.Income.index"
    );
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            base_data: res.data.getBonus.member,
            sum_settled: res.data.getBonus.sum_settled,
            sum_unsettled: res.data.getBonus.sum_unsettled,
            list: res.data.getBonus.list.data,
            total_page: res.data.getBonus.list.last_page,
            receiveBonus: res.data.getReceiveBonus,
            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 = {
        is_type: this.data.active,
        page: this.data.page,
      };
      let urlStr = app.getNetAddresss(
        "plugin.reserve-fund.api.Income.index"
      );
      app._getNetWork({
        url: urlStr,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              total_page: res.data.getBonus.list.last_page,
              list: that.data.list.concat(res.data.getBonus.list.data),
              isLoadMore: true,
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  },
});