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

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    selected: "0",
    display: 1,
    pagesize: 1,
    //待使用
    wait_used: [],
    //已过期
    overdue: [],
    used: [],
    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0,
    coin_name: "", //爱心值自定义名称
    usable: 0, // 登陆会员可用爱心值
    type: '' //区别是否是冻结币那边通知 或者是消费积分那边
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    console.log(options.type, '类型');
    this.setData({
      type: options.type || ''
    });
    // 冻结币的函数调用
    if (options.type == 'FrozenCoin') {
      this.getAllData(0);
      this.setData({
        coin_name: ''
      });
      return;
    }
    this.setData({
      selected: '0'
    });
    this.getUsable();
    this.getNetData(0);
  },

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

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

  },

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

  },

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

  },

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

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

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

  },
  getUsable() {
    let urlStr = app.getNetAddresss("plugin.coin.Frontend.Controllers.page.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            usable: res.data.data.usable,
            coin_name: res.data.data.coin_name
          });
          wx.setNavigationBarTitle({
            title: res.data.data.coin_name ? (res.data.data.coin_name + '明细') : '明细'
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  // 标签切换的时候触发
  onChange(val) {
    // 判断是否是冻结吧的明细
    if (this.data.type == 'FrozenCoin') {
      this.getAllData(val.detail.index);
      return;
    }
    this.getNetData(val.detail.index);

  },

  //发起http请求
  getNetData(type) {
    let urlStr = app.getNetAddresss("plugin.coin.Frontend.Modules.Coin.Controllers.change-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({
              overdue: [],
              used: [],
              wait_used: myData.data
            });
          } else if (type == 1) {
            this.setData({
              wait_used: [],
              overdue: myData.data,
              used: [],
            });
          } else if (type == 2) {
            this.setData({
              wait_used: [],
              overdue: [],
              used: myData.data,
            });
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  getAllData(type) {
    let urlStr = app.getNetAddresss("plugin.froze.Frontend.Modules.Froze.Controllers.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;
          if (type == 0) {
            this.setData({
              overdue: [],
              used: [],
              wait_used: myData.data
            });
          } else if (type == 1) {
            this.setData({
              wait_used: [],
              overdue: myData.data,
              used: [],
            });
          } else if (type == 2) {
            this.setData({
              wait_used: [],
              overdue: [],
              used: myData.data,
            });
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
});
