// packageB/member/love/loveCycleReceive/loveCycleReceive.js
var app = getApp();
// 标记 防止重复加载

Page({
  /**
   * 页面的初始数据
   */
  data: {

   
    details:{},
    tabsIndex:0,
    
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],

    love_name:"爱心值",

    receiveBtnLoading:false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.love_name & options.love_name!=''){
      this.setData({love_name:options.love_name});
    }
    wx.setNavigationBarTitle({
      title: options.love_name+'周期释放',
    });
    this.getData();
    this.getListData();
  },

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

  },

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

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

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

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

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

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

  // 初始化数据
  getData() {
    let urlStr = app.getNetAddresss(
      "plugin.love-cycle-award.frontend.controllers.cycle-award.statistics"
    );
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) {
          return app.tips(res.msg);
        } 
        this.setData({
          details:res.data
        });
      }
    });
  },

  getListData(){
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.isLoadMore = true;

    let urlStr = app.getNetAddresss("plugin.love-cycle-award.frontend.controllers.cycle-award.queue-list");
    let json={};
    if(this.data.tabsIndex>0){
      json.status = this.data.tabsIndex;
    }
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);

        this.setData({
          isLoadMore: true,
          total_page: res.data.last_page,
          listData: res.data.reward_queues,          
        });
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
      }
    });
  },

  getMoreData(){
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page++;
      let json = {
        page: this.data.page
      };
      if(this.data.tabsIndex>0){
        json.status = this.data.tabsIndex;
      }
      let urlStr = app.getNetAddresss("plugin.love-cycle-award.frontend.controllers.cycle-award.queue-list");
      app._getNetWork({
        url: urlStr,
        data:json,
        success: (resdata) => {
          var res = resdata.data;

          if (res.result == 1) {
            this.data.isLoadMore = true;
            var myData = res.data.reward_queues;
            this.setData({
              listData: this.data.listData.concat(myData)
            });
          } else {
            this.data.isLoadMore = false;
            this.data.page--;
          }
        }
      });
    }
  },

  gotoLoveCashbackRecord(val) {
    var id = val.currentTarget.dataset.id || val.target.dataset.id;
    wx.navigateTo({
      url:
        "/packageB/member/love/loveCashbackRecord/loveCashbackRecord" +
        "?id=" +
        id,
    });
  },

  // 领取
  receiveAwardGain() {
    if(this.data.details.receivable==0) return;
    if(this.data.receiveBtnLoading==true) return;
    this.data.receiveBtnLoading = true;
    let urlStr = app.getNetAddresss(
      "plugin.love-cycle-award.frontend.controllers.receive-award.gain"
    );
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) {
          return ;
        }
        this.setData({
          'details.receivable':0
        });
      },
      fail: function (res) {
        console.log(res);
      },
      complete:()=>{
        this.data.receiveBtnLoading = false;
      }
    });
  },

  changeTabsIndex(evt){
    console.log(evt);
    this.data.tabsIndex = evt.detail.index;
    this.getListData();
  }



 
});
