// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    selected: "0",
    pagesize: 1,
    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0,
    love_name: "", //爱心值自定义名称
    usable: 0, // 登陆会员可用爱心值
    //全部
    allData: [],
    //收入
    incomeData: [],
    //支出
    expendData: [],
    loveLang:{}
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      let basic_info = wx.getStorageSync("yz_basic_info");
			
      if(basic_info.lang.love){
        this.setData({
          loveLang:basic_info.lang.love
        });
      }
    }catch(e){
      console.log(e);
    }

    this.setData({
      selected: '0'
    });
    this.initData();
    this.getUsable();
    this.getNetData(0);
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {
    this.loadBottom();
  },

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

  },
  getUsable() {
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Controllers.page.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            usable: res.data.data.usable,
            love_name: res.data.data.love_name
          });

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  initData() {
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.goload = true;
    this.data.loading = true;
    this.data.allLoaded = false;
    this.data.isLoadMore = true;

    this.data.allData = [];
    this.data.incomeData = [];
    this.data.expendData = [];
  },
  // 标签切换的时候触发
  onChange(val) {
    this.initData();
    this.data.selected = val.detail.index;
    console.log(val);
    this.getNetData(val.detail.index);
  },
  // 加载更多
  loadBottom() {
    //console.log('jfc',"loadBottom");
    //	this.allLoaded = true;// 若数据已全部获取完毕,不在显示加载更多组件

    if (this.data.isLoadMore) {
      this.getMoreData(this.data.page, this.data.selected);
      //this.$refs.coupon_loadmore.onBottomLoaded();
    } else {
      console.log("没有更多数据");
    }
  },

  //切换优惠券状态列表
  switchItem() {
    //this.initData();
    this.getNetData(this.data.selected);
  },

  //发起http请求
  getNetData(type) {
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.love-records.index");
    urlStr += "&service_type=" + type;
    urlStr += "&page=1";
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          var myData = res.data.data;
          this.setData({
            total_page: res.data.data.last_page
          });
          if (type == 0) {
            this.setData({
              incomeData: [],
              expendData: [],
              allData: myData.data
            });
          } else if (type == 1) {
            this.setData({
              incomeData: myData.data,
              expendData: [],
              allData: []
            });
          } else if (type == 2) {
            this.setData({
              incomeData: [],
              expendData: myData.data,
              allData: []
            });
          }

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData(page, type) {

    if (this.data.page == this.data.total_page) {
      this.setData({
        isLoadMore:false
      });
      return;
    }
    if (this.data.page >= this.data.total_page) {
      this.setData({
        loading: true,
        allLoaded: true
      });
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.love-records.index");
      urlStr += "&service_type=" + type;
      urlStr += "&page=" + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          if (res.data.result == 1) {
            var myData = res.data.data;
            this.setData({
              loading: false,
              allLoaded: false
            });
            if (type == 0) {
              var allData = this.data.allData.concat(myData.data); //数组拼接
              this.setData({
                allData
              });
            } else if (type == 1) {
              var incomeData = this.data.incomeData.concat(myData.data);
              this.setData({
                incomeData
              });
            } else if (type == 2) {
              var expendData = this.data.expendData.concat(myData.data);
              this.setData({
                expendData
              });
            }
          } else {
            var page = this.data.page - 1;
            this.setData({
              page,
              loading: true,
              allLoaded: true,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
});