// pages/userCenter.js
const wxService = require('../../../../utils/wxService')

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    query: '',
    memberPoint: {
      total: '--',
      expirePoint: 0,
      expireDate: ''
    },
    activityList : [],
    pageNo : 1,
    pageSize : 10,
    totalPage : 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    this.refreshPointRequest();
    this.getPointActivityGroupList();
  },
  //刷新积分请求
  refreshPointRequest() {
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    if (!memberId) {
      return;
    }
    wxService.get('/member/memberPoint/refreshExpiredPoint?memberId=' + memberId).then(res => { })
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取会员积分信息
    this.getMemberIntegrateInfo();
  },
  //查询会员积分信息
  getMemberIntegrateInfo() {
    // wx.showLoading({
    //   title: '数据请求中..',
    // });
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    if (!memberId) {
      return;
    }

    wxService.get(`/member/memberPoint/getMemberPointInfo?memberId=${memberId}`).then(res => {
      let data = res.data.data ? res.data.data : {};
      this.data.memberPoint.total = data.point ? data.point : 0;
      this.data.memberPoint.expirePoint = data.pointWillExpired ? data.pointWillExpired : 0;
      let expireDate = data.pointExpiredTime ? data.pointExpiredTime : '2019-12-31 23:59:59';

      if (expireDate) {
        let dateStr = expireDate.replace(/-/g, '/');
        let da = new Date(dateStr);
        let year = da.getFullYear();
        let month = (da.getMonth() + 1) < 10 ? '0' + (da.getMonth() + 1) : da.getMonth() + 1;
        let day = da.getDate() < 10 ? '0' + da.getDate() : da.getDate();
        expireDate = `${year}年${month}月${day}日`
      }

      this.data.memberPoint.expireDate = expireDate;
      this.setData({
        memberPoint: this.data.memberPoint
      });
    });
  },

  // 点击跳转至会员规则页
  handleGoMemberRulesRules() {
    wxService.router(`/pages/memberRules/memberRules?type=2`)
  },

  //获取积分分组活动列表
  getPointActivityGroupList(refres){
    let pageNo = this.data.pageNo,
      pageSize = this.data.pageSize;
    wxService.post(`/merchant/groupingActivities/findPage?pageNo=${pageNo}&pageSize=${pageSize}`,{
      status : 0
    }).then(res => {
      if(res){
        let list = res.data.data.content ? res.data.data.content: [];
        this.data.activityList = this.data.activityList.concat(list);
        this.data.totalPage = res.data.data.totalPages;
        this.setData({
          activityList: this.data.activityList,
          totalPage: this.data.totalPage
        });
      }

      if(refres){
        wx.stopPullDownRefresh();
        wx.showToast({
          title: '刷新成功',
        });
      }
    })

  },

  //去详情
  onTapToInfo(e){
    let item = e.currentTarget.dataset.item;
    if (item.type == 1){
      //积分换物
      wx.navigateTo({
        url: '/subPackage/page/pages/pointExchangeGoods/pointExchangeGoods?id=' + item.id,
      });
    }
    else if(item.type == 2){
      //积分兑换优惠券
      wx.navigateTo({
        url: '/subPackage/page/pages/pointExchangeCoupon/pointExchangeCoupon?id=' + item.id,
      });
    }
  },
 
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () { 
    this.data.pageNo = 1;
    this.data.activityList = [];

    this.getPointActivityGroupList(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.pageNo < this.data.totalPage){
      this.data.pageNo ++ ;
      this.getPointActivityGroupList();
    }
    else{
      if(this.data.pageNo > 1){
        wx.showToast({
          title: '没有更多数据啦~',
          icon : 'none'
        });
      }
    }
  },
});