// component/plus/plus.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    skuList: {
      type: Array,
      value: [],
    },
    plusBgi: {
      type: Object,
      value: null
    },
    currentHasUserInfo: {
      type: Boolean,
      type: false
    }
  },
  ready() {
		this.getPlusPurchase()
	},
  /**
   * 组件的初始数据
   */
  data: {
    currentTab: '999',
    currentPrice: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取plus购买信息
    getPlusPurchase() {
      wx.showLoading({
        title: '加载中'
      })
      wxService.request({
        url: app.globalData.ctx + '/sale/premium/query',
        header: {
          'tunnel-token' : 'aa8a1bfb7f6c5031d172d94b6574293ef954a6f2d4e16657da008e0cc15588feD'
        },
        success: (res) => {
          const { result, data } = res.data
          if (result == 0) {
            this.setData({
              skuVOList: data
            })
          }
          wx.hideLoading()
        }
      })
    },
    _getUserInfo(res = {}) {
      const userInfo = res.detail || {}
      console.log('userInfo', userInfo)
      // 先授权再领卡
      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) // 缓存用户信息
          this.setUserInfo()   // 判断有无开卡
        }).catch(err => {
          if (err) {
            wx.showToast({
              title: `会员系统异常请稍后重试!`,
              icon: 'none'
            })
          }
        })
      }
    },
    setUserInfo() {
      const userInfo = wx.getStorageSync('_baseUserInfo')
      // 新用户去激活领卡 
      if (userInfo && userInfo.member && !userInfo.member.mobile) {
        console.log('去开')
        wxService.openCard()
        return false
      } else {
        console.log('已开')
      }
      // 跳转
      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)
    },
    switchNav(e) {
      console.log(e)
      this.setData({
        currentTab: e.currentTarget.dataset.current,
        id: e.currentTarget.dataset.id,
        currentPrice: e.currentTarget.dataset.price
      })
    },
    payPlus() {
      if (!this.data.id) {
        wx.showToast({
          title: '请先选择会员卡',
          icon: 'none',
          mask: true
        })
        return
      }
      // 下单
      wx.showLoading({
        title: '加载中'
      })
      // 获取支付参数
      wxService.post(`/sale/payment/premium/buyer/preorder/wxa`, {
        premiumId: this.data.id
      }).then(res => {
        if (!res) return false;
        const { result, data } = res && res.data || res && res.result
        if (result == 0) {
          console.log('data', data)
          let amount = data.amount || 0
          wx.requestPayment(Object.assign({
            success(res) {
              wx.hideLoading()
              // 支付成功页面
              wxService.router(`/subPackage/page/pages/paymentStatus/paymentStatus?status=S&amount=${amount}`)
            },
            fail(res) {
              wx.hideLoading()
              wxService.router('/subPackage/page/pages/paymentStatus/paymentStatus?status=F')
            }
          }, data.wxParams))
        }
      })
    },

  }
})
