// subPackage/page/pages/valueCard/valueCard.js
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    cardList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    this.getValueCardList();
    console.log(this.data)
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  //跳转卡详情
  onTapToCardInfo(e){
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/subPackage/page/pages/valueCardInfo/valueCardInfo?id=' + item.id,
    });
  },
  //获取有效活动列表
  getValueCardList(refresh){
    wx.showLoading({
      title: '加载中..',
    });

    wxService.post(`/merchant/superValueCardActivity/miniProgram/getPageList`,{},null,true).then(res => {
      if(res){
        let list = res.data.data ? res.data.data : [];
        this.data.cardList = list;
        this.setData({
          cardList: this.data.cardList
        });
      }
      wx.hideLoading();
      if(refresh){
        setTimeout(() => {
          wx.showToast({
            title: '刷新成功',
          });
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
        }, 300);
      }
    });
  },

  //授权
  _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'
          })
        }
      })
    }
  },

  setUserInfo() {
    // 判断有无开卡
    const userInfo = wx.getStorageSync('_baseUserInfo')
    // 新用户去激活领卡 
    if (userInfo && userInfo.member && !userInfo.member.mobile && !userInfo.member.cardNoWeixin) {
      wxService.openCard()
      return false
    }

    this.setData({
      currentHasUserInfo : true,
    });
    // 跳转
    setTimeout(() => {
      this.onLoad();
    }, 200)
  },

  //跳转到我的超值卡
  onTapToMyValueCard(){
    wxService.router(`/subPackage/page/pages/myValueCard/myValueCard`);
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.getValueCardList(true);
  },

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

  },

})