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

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isAuthorization: Boolean,
    isAuthorizationActiveText: {
      type: String,
      value: ''
    },
    isAuthorizationType: {
      type: Number,
      value: 2
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    couponNum: '2',
    userInfo: {},
    hasUserInfo: false,
    env: envInfo.env,
    checkAll: true
  },
  /**
   * 组件的方法列表
   */
  methods: {
    checkAll() {
      this.setData({
        checkAll: !this.data.checkAll
      })
    },
    handelToMemberRules() {
      wxService.router(`/pages/memberRules/memberRules?type=${this.data.isAuthorizationType}`)
    },
    //再想想
    close() {
      this.setData({
        isAuthorization: false
      })
    },
    //立即兑换
    query() {},
    _getUserInfo(res = {}) {
      this.setData({
        isAuthorization: false
      })
      const userInfo = res.detail || {}
      if (res.detail.userInfo) {
        utils.getUserInfoByBtn(userInfo).then( (data = {} ) => {
          const {token} = data
          wx.setStorageSync('_accreditUserInfo', userInfo)
          wx.hideNavigationBarLoading()
          if (token) {
            wx.setStorageSync('token', token)
          } else {
            wx.setStorageSync('token', '')
          }
          wx.setStorageSync('_baseUserInfo', data)
          // 存储用户开卡状态
          // wx.setStorageSync('memberActivateStatus', data.memberActivateStatus)
          this.setUserInfo()
        }).catch(err=> {
          if (err) {
            wx.showToast({
              title: `会员系统异常请稍后重试!`,
              icon: 'none'
            })
          }
        })
      } else {
        this.setData({
          isAuthorization: false
        })
      }
    },
    handleActiveAgree() {
      // 子组件触发父组件 去开卡
      wxService.nextTick(() => {
        this.triggerEvent('activeTocard', true)
      })
      this.setData({
        isAuthorization: false
      })
    },
    setUserInfo () {
      // 判断有无开卡
      // const {memberActivateStatus} = wx.getStorageSync('_baseUserInfo')
      const userInfo = wx.getStorageSync('_baseUserInfo')
      // 新用户去激活领卡 
      if (userInfo && userInfo.member && !userInfo.member.mobile) {
        wxService.openCard()
        return false
      }
      // 没有开卡并且需要开卡
      // if(!memberActivateStatus) {
      //   wxService.openCard()
      //   return false
      // }  
      // 跳转
      const urls = getCurrentPages()
      setTimeout(() => {
        const item = urls[0].options
        if (item.route) {
          try {
            const options = JSON.parse(item.options)
            wxService.router(`/${item.route}`).search(options).replace()
          } catch (e) {
            wxService.router('/pages/userCenter/userCenter').replace()
          }
        } else {
          wxService.router('/pages/userCenter/userCenter').replace()
        }
      }, 200)
    }
  }
})
