// pages/userCenter.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    pageSize: 10,
    query: '',
    totalPages: 0,
    integralCouponList: [], // 积分兑换优惠券列表
    noMore: false,
    isExchange: false,
    exchangeId: '',
    exchangePoint: 0,
  },

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

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

  // 积分兑换优惠券列表
  initIntegralCouponList() {
    wx.showLoading({
      title: '加载中'
    })
  //   wxService.get(`/coupon/pointsRedemptionCouponSetting/findPage?pageNo=${pageNo}&pageSize=${pageSize}&query=${this.data.query}`).then(res => {
  //     const {result,data} = res.data
  //     if(result == 0){
  //        wx.hideLoading()
  //        this.setData({
  //         integralCouponList: this.data.pageNo == 1? [...data.content]: [...this.data.integralCouponList,...data.content],
  //         totalPages: data.totalPages
  //        })
  //     }

  //   }).finally(() => {
  //     wx.hideLoading();
  //   })
    // 查询所有生效中的活动
    wxService.get(`/coupon/pointsRedemptionCouponSetting/getAllValid`).then(res => {
      if(res){
        const {result,data} = res.data
        if(result == 0){
           wx.hideLoading()
           this.setData({
            integralCouponList: data.reverse()
           })
        }
      }
    }).finally(() => {
      wx.hideLoading();
    })
  },
  //立即兑换
  exchange(e){
    const {id, point} = e.currentTarget.dataset
    this.setData({
      isExchange :true,
      exchangeId: id,
      exchangePoint: point
    })
  },
  // 点击跳转至会员规则页
  handleGoMemberRulesRules() {
    wxService.router(`/pages/memberRules/memberRules?type=2`)
  },
  goCouponDetail(e) {
    const {id} = e.currentTarget.dataset
    wxService.router(`/pages/integralMallDetail/integralMallDetail?id=${id}`)
  },
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // if (this.data.pageNo < this.data.totalPages) {
    //   this.setData({
    //       pageNo: this.data.pageNo + 1,
    //       pageSize: 10,
    //       noMore: true
    //   })
    //   this.initIntegralCouponList(this.data.pageNo, this.data.pageSize)
    // } else {
    //   this.setData({
    //     noMore: true
    //   })
    // }
  },
})