// pages/activateCard/activateCardCallback.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo
var wxbarcode = require('../../codeUtils/index.js');

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad () {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    wx.showLoading({
      title: '加载中'
    })
   // activate_ticket, card_id, code, errCode, wx_activate_after_submit_url
   this.handleGoActivateCard(this.options)
  },
  handleGoActivateCard(options={}) {
    if(!options) {
      this.goUserCenter()
      return false
    }
    options.activate_ticket = decodeURIComponent(options.activate_ticket)
    // 解析 url 参数 {}
    const parmasSubmitUrl = utils.parseUrl(options.wx_activate_after_submit_url)
    // 合并对象
    let currentObj = Object.assign(options, parmasSubmitUrl)
    
    wxService.post(`/member/activateCard`, currentObj).then(res => {
      const {result,data} = res.data
      if(result == 0){
        // 激活成功 变更 _激活状态
        const baseUserInfo = wx.getStorageSync('_baseUserInfo')
        baseUserInfo.memberActivateStatus = true
        baseUserInfo.memberId = data.id
        if (data.code){
          baseUserInfo.memberCardNo = data.code
        }
        // 更新缓存信息手机号码 relogin 
       this.handelReLogin()
      } else {
        wx.hideLoading()
        wx.showToast({
          title: `${res.desc}`,
          icon: 'none'
        })
        this.goUserCenter()
      }
    }).finally(() => {
      wx.hideLoading()
      this.goUserCenter()
    })
  },
  handelReLogin(){
    wx.login({
      success: (res) => {
        wx.setStorageSync('_loginCode', res.code)
        if(res.code){
          wxService.post(`/member/minaLogin`, {
            code: res.code,
            wechatInfo: null,
            brandId: app.globalData.brandId,
          }).then(response=>{
            let dataResp = response.data.data
            wx.setStorageSync('token', dataResp.token)
            wx.setStorageSync('_baseUserInfo', dataResp)
            this.goUserCenter()
            return Promise.resolve({ response })
          }).catch(err=>{
            this.goUserCenter()
            return Promise.reject(err)
          })
        } else {
          wx.hideLoading()
          wx.showToast({
            title: `激活失败`,
            icon: 'none'
          })
          this.goUserCenter()
        }
      },
      fail: (res) => {
        wx.hideLoading()
        this.goUserCenter()
        reject(res)
      }
    },()=>{
      wx.hideLoading()
      this.goUserCenter()
    })
  },
  goUserCenter() {
    wxService.router(`/pages/userCenter/userCenter`).replace()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})