// packageH/member/upgrade_code/group_details/group_details.js
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    codeId: null,
    codeKey: null,
    codeInfo: null,
    codeLevelDescription: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.key) {
      this.setData(
        {
          codeKey: options.key,
        },
        () => {
          this.getCodeDetailsByKey();
        }
      );
    } else if (options.code_id) {
      this.setData(
        {
          codeId: options.code_id,
        },
        () => {
          this.getCodeDetails();
        }
      );
    } else {
      wx.showToast({
        title: "升级码不存在",
        icon: "none",
        duration: 1500,
        success: (result) => {
          wx.redirectTo({
            url: "/packageH/member/upgrade_code/index/index",
          });
        },
      });
      return;
    }
    wx.setNavigationBarTitle({
      title: "升级码",
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.codeKey) {
      this.getCodeDetailsByKey();
    } else {
      this.getCodeDetails();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let title = "升级码 " + this.data.codeInfo.code.member_level.level_name;
    if (this.data.codeInfo.code.dividend_level) {
      title += " + " + this.data.codeInfo.code.dividend_level.level_name;
    }
    return {
      title,
      path:
        "/packageH/member/upgrade_code/group_details/group_details?key=" +
        this.data.codeInfo.code.key,
    };
  },
  onShareTimeline() {
    let title = "升级码 " + this.data.codeInfo.code.member_level.level_name;
    if (this.data.codeInfo.code.dividend_level) {
      title += " + " + this.data.codeInfo.code.dividend_level.level_name;
    }
    return {
      title,
      path:
        "/packageH/member/upgrade_code/group_details/group_details?key=" +
        this.data.codeInfo.code.key,
    };
  },
  getCodeDetails() {
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    let url = App.getNetAddresss(
      "plugin.upgrade-code.Frontend.controllers.code.index"
    );
    App._getNetWork({
      url,
      data: {
        id: this.data.codeId,
      },
      success: ({ data: { data: response, result, msg } }) => {
        wx.stopPullDownRefresh();
        if (result === 0) {
          wx.hideLoading();
          wx.showToast({
            title: msg,
            duration: 1500,
            mask: false,
            icon:"none",
            success: (result) => {
              wx.redirectTo({
                url: "/packageH/member/upgrade_code/index/index",
              });
            },
          });
          return;
        }
        let codeLevelDescription = "";
        if (response['code']["member_level"] && response['code']["dividend_level"]) {
          codeLevelDescription =
          response['code']["member_level"]["level_name"] +
            " + " +
            response['code']["dividend_level"]["level_name"];
        } else if (response['code']["member_level"]) {
          codeLevelDescription = response['code']["member_level"]["level_name"];
        } else if (response['code']["dividend_level"]) {
          codeLevelDescription = response['code']["dividend_level"]["level_name"];
        }
        this.setData({
          codeInfo: response,
          codeLevelDescription,
        });
        wx.hideLoading();
      },
    });
  },
  getCodeDetailsByKey() {
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    let url = App.getNetAddresss(
      "plugin.upgrade-code.Frontend.controllers.code.share"
    );
    App._getNetWork({
      url,
      data: {
        key: this.data.codeKey,
      },
      success: ({ data: { data: response, result, msg } }) => {
        wx.stopPullDownRefresh();
        if (result === 0) {
          wx.hideLoading();
          wx.showToast({
            title: msg,
            duration: 1500,
            mask: false,
            success: (result) => {
              wx.navigateBack({
                delta: 1,
              });
            },
          });
          return;
        }
        let codeLevelDescription = response.code.member_level.level_name;
        if (response.code.dividend_level) {
          codeLevelDescription +=
            " + " + response.code.dividend_level.level_name;
        }
        this.setData({
          codeInfo: response,
          codeLevelDescription,
        });
        wx.hideLoading();
      },
    });
  },
});
