// pages/userCenter.js
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    query: '',
    integralCouponList: [], // 积分兑换优惠券列表
    userHasBaseInfo : false,
  },

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

  //授权
  _getUserInfo(res = {}) {
    const userInfo = res.detail || {}
    if (res.detail.userInfo) {
      utils.getUserInfoByBtn(userInfo).then((data = {}) => {
        const { token } = data
        wx.setStorageSync('_accreditUserInfo', userInfo)
        if (token) {
          wx.setStorageSync('token', token)
        } else {
          wx.setStorageSync('token', '')
        }
        wx.setStorageSync('_baseUserInfo', data)
        const baseUserInfo = wx.getStorageSync('_baseUserInfo')
        // 获取当前会员是否是体验者
        const { member } = baseUserInfo
        const curMemberTrial = member && member.trial || false
        wx.setStorageSync('isExperiencer', curMemberTrial)
        this.setUserInfo()
      }).catch(err => {
        if (err) {
          wx.showToast({
            title: `会员系统异常请稍后重试!`,
            icon: 'none'
          })
        }
      })
    }
  },

  //获取当前用户是否登录
  userHasLogin(){
    const baseUserInfo = wx.getStorageSync('_baseUserInfo');
    if(baseUserInfo){
      this.setData({
        userHasBaseInfo : true
      })
    }
    else{
      this.setData({
        userHasBaseInfo: false
      })
    }
  },

  setUserInfo() {
    // 判断有无开卡
    const userInfo = wx.getStorageSync('_baseUserInfo')
    // 新用户去激活领卡 
    if (userInfo && userInfo.member && !userInfo.member.mobile && !userInfo.member.cardNoWeixin) {
      wxService.openCard()
      return false
    }
    // 跳转
    setTimeout(() => {
      this.initIntegralCouponList();
      this.userHasLogin();
    }, 200)
  },
  
  // 获取优惠券活动列表
  initIntegralCouponList(refresh) {
    wx.showLoading({
      title: '加载中'
    });

    wxService.post(`/coupon/couponCenterActivity/buyer/getValidList`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          this.data.integralCouponList = data ? data : [];
          this.setData({
            integralCouponList: this.data.integralCouponList
          });
        }
      }

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

    }).finally(() => {
      wx.hideLoading();
    })
  },
  //立即兑换优惠券
  exchange(e) {
    const { member } = wx.getStorageSync('_baseUserInfo');
    if(!member){
      wx.showToast({
        title: '请先去登录',
        icon : 'none'
      })
      return ;
    }

    //判断是否开卡
    if(!member.cardNoWeixin){
      wx.showToast({
        title: '请先开卡后购买',
        icon : 'none'
      });

      wxService.openCard()
      return ;
    }

    wx.showLoading({
      title: '领取中..',
    });
    const { id } = e.currentTarget.dataset;
    wxService.post(`/coupon/couponCenterActivity/buyer/coupon/draw?id=${id}`).then(res => {
      wx.hideLoading();
      if(res){
        if(res.data.result == 0){
          wx.showToast({
            title: '领取成功',
          });
        }
        else{
          wx.showToast({
            title: '领取失败，请稍后再试',
            icon : 'none'
          });
        }
      }
    });
  },
  //跳转到详情
  goCouponDetail(e) {
    const { id } = e.currentTarget.dataset
    wxService.router(`/subPackage/page/pages/couponCenterInfo/couponCenterInfo?id=${id}`)
  },
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () { 
    this.initIntegralCouponList(true);
  },

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

  },
})