// packageC/selfCarry/selfCarry_reward/selfCarry_reward.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    member: {},
    deliver_name: "",
    current: 0,
    amount: null,
    dividend: [],
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

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

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

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

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

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

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

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.package-deliver.frontend.bonus.getDeliverInfo");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            amount: res.data.amount,
            deliver_name: res.data.deliver.deliver_name,
            member: res.data.deliver.member
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: '获取失败',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getInfo() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.package-deliver.frontend.bonus.getList");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            dividend: res.data.list.data,
            total_page: res.data.list.last_page,
            isLoadMore: true,
            page: res.data.list.current_page
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    let that = this;
    this.setData({
      isLoadMore: false
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let pages = this.data.page;
      pages += 1;
      this.setData({
        page: pages
      });
      let urlStr = app.getNetAddresss('plugin.package-deliver.frontend.bonus.getList');
      app._getNetWork({
        url: urlStr,
        data: {
          page: that.data.page
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var nextPageData = res.data.list.data;
            let dividend = [...that.data.dividend, ...nextPageData];
            that.setData({
              dividend,
              total_page: res.data.list.last_page
            });
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    }
  }
});