// pages/addScore/addScore.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseWhich:1,
    scoreData:[],
    newData:[],
    pageNum:1,
    count:10,
    total_page:"",
    scrollHig:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.openId){
      this.addScore();
    }else{
      app.globalData.openidSuccessFuc = this.addScore;
    }
    this.setData({
      scrollHig: wx.getSystemInfoSync().windowHeight-44-41-20,
    });
  },
  addScore:function(){//积分增长
    let params = {
      list_type:2,
      cur_page:this.data.pageNum,
      show_num:this.data.count,
    }
    app.wxRequest(app.globalData.httpUrl + 'front/point/addlist', params, e => {
      console.log(e)
      if (e.code == 200) {
        if(e.data.length==0){
          console.log(5555)
          wx.showToast({
            title: e.message,
            icon:"none"
          })
          return;
        }
        for(var i=0;i<e.data.length;i++){
          this.data.newData.push(e.data[i]);
          e.data[i].add_date = e.data[i].add_date.slice(0,10);
        }
        this.setData({
          scoreData:this.data.newData
        })
        this.data.total_page = e.total_page;
      }
    }, this)
  },
  deleScore:function(){//核销积分
    let params = {
      list_type:2,
      cur_page:this.data.pageNum,
      show_num:this.data.count,
    }
    app.wxRequest(app.globalData.httpUrl + 'front/point/checklist', params, e => {
      console.log(e)
      if (e.code == 200) {
        if(e.data.length==0){
          wx.showToast({
            title: e.message,
            icon:"none"
          })
          return;
        }
        for(var i=0;i<e.data.length;i++){
          this.data.newData.push(e.data[i]);
          e.data[i].check_date = e.data[i].check_date.slice(0,10);
        }
        this.setData({
          scoreData:this.data.newData
        })
        this.data.total_page = e.total_page;
      }
    }, this)
  },
  getBot:function(){//scroll触底事件
    if(this.data.pageNum<this.data.total_page){
      this.data.pageNum++;
      this.getDataList();
    }else{
      wx.showToast({
        title: '没有更多数据了',
        icon:"none"
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  chooseFn: function(e){//选择
    this.setData({
      chooseWhich:e.currentTarget.dataset.index
    })
    this.data.pageNum = 1;
    this.data.scoreData = [];
    this.data.newData = [];
    if(this.data.chooseWhich==1){
      this.addScore();
    }else{
      this.deleScore();
    }
  },
})