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

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    query: '',
    integralCouponList: [], // 积分兑换优惠券列表
    integralGoodsList: [], // 限量精品列表
    noMore: false,
    isExchange: false,
    exchangeId: '',
    exchangePoint: 0,
    currentTab: 'coupon',
    memberPoint : {
      total: '--',
      expirePoint : 0,
      expireDate : ''
    },
    dialogStockEmpty : false,
    dialogUserLimit : false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    this.initIntegralCouponList();
    this.initIntegralProductList();
  },
  handelTab(e) {
    let curTab = e.currentTarget.dataset.tab
    this.setData({
      currentTab: curTab
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.initIntegralProductList()
    // this.initIntegralCouponList();
    //获取会员积分信息
    this.getMemberIntegrateInfo();
  },
  //积分换物记录
  onTapToExchangeRecords(){
    wx.navigateTo({
      url: '/subPackage/page/pages/recordList/recordList',
    });
  },
  //查询会员积分信息
  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
      });
    });
  },

  // 查询所有生效中的活动 限量精品
  initIntegralProductList() {
    wx.showLoading({
      title: '加载中'
    })
    wxService.get(`/member/pointsRedemptionProductSetting/getAllValid`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          let list  = data ? data : [];
          if(list.length == 0){
            this.data.currentTab = 'coupon';
          }
          else{
            this.data.currentTab = 'goods';
          }
          this.setData({
            integralGoodsList: data? data : [],
            currentTab: this.data.currentTab
          })
        }
      }
    }).finally(() => {
      wx.hideLoading();
    })
  },
  // 积分兑换优惠券列表
  initIntegralCouponList() {
    wx.showLoading({
      title: '加载中'
    });
    
    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
    })
  },
  // 立即兑换物品
  exchangeGoods(e) {
    const { id, point } = e.currentTarget.dataset;
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    //检查是不是可以兑换
    wx.showLoading({
      title: '检查兑换资格中..',
    });

    wxService.post(`/member/pointsRedemptionProductRecord/checkByMemberIdAndSettingId?memberId=${memberId}&settingId=${id}`).
    then(res => {
      if(res.data.data == -1){
        this.setData({
          dialogUserLimit : true
        })
      }
      else{
        wx.navigateTo({
          url: '/subPackage/page/pages/exchangeAddress/exchangeAddress?id=' + id,
        });
      }
    });
  },
  //跳转积分换物详情
  // subPackage/page/pages/IntegrateGoodsExchangeInfo/IntegrateGoodsExchangeInfo
  goIntegrateGoodsExchangeDetail(e){
    const { id } = e.currentTarget.dataset
    wxService.router(`/subPackage/page/pages/IntegrateGoodsExchangeInfo/IntegrateGoodsExchangeInfo?id=${id}`)
  },
  // 点击跳转至会员规则页
  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 () {
   
  },
})