const wx2my = require('../../wx2my');
const Behavior = require('../../Behavior');
// 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() {
    wx2my.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 = wx2my.getStorageSync('_baseUserInfo');
        baseUserInfo.memberActivateStatus = true;
        baseUserInfo.memberId = data.id;

        if (data.code) {
          baseUserInfo.memberCardNo = data.code;
        } // 更新缓存信息手机号码 relogin 


        this.handelReLogin();
      } else {
        wx2my.hideLoading();
        wx2my.showToast({
          title: `${res.desc}`,
          icon: 'none'
        });
        this.goUserCenter();
      }
    }).finally(() => {
      wx2my.hideLoading();
      this.goUserCenter();
    });
  },

  handelReLogin() {
    wxService.login({
      success: res => {
        wx2my.setStorageSync('_loginCode', res.authCode);

        if (res.authCode) {
          wxService.post(`/member/alipay/minaLogin?code=${res.authCode}&brandId=${app.globalData.brandId}`, {
            code: res.authCode,
            // wechatInfo: null,
            brandId: app.globalData.brandId
          }).then(response => {
            let dataResp = response.data.data;
            wx2my.setStorageSync('token', dataResp.token);
            wx2my.setStorageSync('_baseUserInfo', dataResp);
            this.goUserCenter();
            return Promise.resolve({
              response
            });
          }).catch(err => {
            this.goUserCenter();
            return Promise.reject(err);
          });
        } else {
          wx2my.hideLoading();
          wx2my.showToast({
            title: `激活失败`,
            icon: 'none'
          });
          this.goUserCenter();
        }
      },
      fail: res => {
        wx2my.hideLoading();
        this.goUserCenter();
        reject(res);
      }
    }, () => {
      wx2my.hideLoading();
      this.goUserCenter();
    });
  },

  goUserCenter() {
    wxService.router(`/pages/userCenter/userCenter`).replace();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

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

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

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

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