// pages/integralIndex/integralIndex.js
var http = require('../../../utils/http.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    scoreProdList: [],
    scoreInfo:{},
  },

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

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 获取当前积分信息
    this.getScoreInfo()
  },

  /**
   * 跳转
   */
  navigateTo(e) {
    const path = e.currentTarget.dataset.path
    if (path == 'scoreDet') {
      wx.navigateTo({
        url: '/packageMemberIntegral/pages/integralDetail/integralDetail',
      })
    } else if (path == 'scoreIndex') {
      wx.navigateTo({
        url: '/packageMemberIntegral/pages/memberIndex/memberIndex',
      })
    }
  },

  /**
   * 获取当前积分信息
   */
  getScoreInfo() {
    var params = {
      url: '/p/score/scoreInfo',
      method: 'GET',
      data: {
        channelId:wx.getStorageSync('channelId')||3
      },
      callBack: res => {
        this.setData({
          scoreInfo: res
        })
      }
    }
    http.request(params)
  },

  /**
   * 获取积分商品列表
   */
  getScoreProdList() {
    var param = {
      url: "/p/score/page",
      method: "GET",
      data: {
        current: this.data.current,
        size: 10,
        channelId:wx.getStorageSync('channelId')||3
      },
      callBack: (res) => {
        wx.hideLoading()
        var scoreProdList = []
        if (this.data.current == 1) {
          this.setData({
            scoreProdList: res.records,
            pages: res.pages,
            current: res.current
          });
        } else {
          scoreProdList = this.data.scoreProdList
          scoreProdList.push(...res.records)
          this.setData({
            scoreProdList
          })
        }
      }
    };
    http.request(param);
  },

  /**
   * 跳转积分商品详情
   */
  toScoreProdDet(e) {
    console.log(e)
    const prodId = e.currentTarget.dataset.scoreprodid
    wx.navigateTo({
      url: '/packageMemberIntegral/pages/convertProdDet/convertProdDet?prodId=' + prodId,
    })
  },


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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.current < this.data.pages) {
      this.setData({
        current: this.data.current + 1
      })
      this.getScoreProdList()
    }
  },

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

  }
})