// packageH/member/upgrade_code/upgrade_result/upgrade_result.js
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    upgradeStatus: "upgradeing",
    codeKey: null,
    codeMid: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let key = null;
    let codeMid=null;
    if (options.scene) {
      const scene = this.sceneDecode(options.scene);
      key = scene.key;
      codeMid = scene.mid;
    } else {
      key = options.key;
      codeMid = scene.mid;
    }
    App._setMid(codeMid);
    wx.setNavigationBarTitle({
      title: "升级结果"
    });
    this.setData(
      {
        codeKey: key,
      },
      () => {
        this.upgradeLevel();
      }
    );
  },
  sceneDecode(scene) {
    scene = decodeURIComponent(scene);
    let paramArr = scene.split(",");
    const senceValues = {};

    paramArr.forEach((item, index) => {
      let key = item.split("=")[0];
      let value = item.split("=")[1];
      senceValues[key] = value;
    });
    return senceValues;
  },

  upgradeLevel() {
    let url = App.getNetAddresss(
      "plugin.upgrade-code.Frontend.controllers.code.handle"
    );
    App._getNetWork({
      url,
      data: {
        key: this.data.codeKey
      },
      success: ({ data, msg }) => {
        if (data.result == 1) {
          this.setData({
            upgradeStatus: "success",
          });
        } else {
          wx.showToast({
            title: data.msg,
            icon: "none",
            duration: 1500,
            mask: false,
          });
          this.setData({
            upgradeStatus: "fail",
          });
        }
        setTimeout(() => {
          wx.redirectTo({
            url: "/packageG/member_v2/member_v2",
          });
        }, 1000);
      },
    });
  },
});
