// pages/microShop_apply/microShop_apply.js
// var WxParse = require('../../../wxParse/wxParse.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    data: [],
    bannerImgSrc: "",
    nextUrl: "",
    goodsID: "",
    signature: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkrouter();
    this.getjson();
  },
  checkrouter() {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      let basic_info = wx.getStorageSync("yz_basic_info");
      let stop_info = basic_info.popularize_page.mini.vue_route;
      for (let i = 0; i < stop_info.length; i++) {
        if (stop_info[i] == "microShop_apply") {
          console.log(basic_info.popularize_page.mini.mini_url + "跳转的路径");

          wx.showToast({
            title: "未开启推广权限",
            duration: 1000,
            icon: "none",
            success: function () {
              setTimeout(() => {
                wx.redirectTo({
                  url: basic_info.popularize_page.mini.mini_url,
                });
              }, 1000);
            },
          });
          return;
        }
      }
    } catch (e) {
      console.log(e);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getjson() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.micro.frontend.controllers.MicroShopApply.apply"
    );
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            nextUrl: res.data.create_order_api,
            bannerImgSrc: res.data.micro_thumb,
            data: res.data.levels,
            signature: res.data.signature,
          });
          // let article = that.data.signature;
          /**
           * WxParse.wxParse(bindName , type, data, target,imagePadding)
           * 1.bindName绑定的数据名(必填)
           * 2.type可以为html或者md(必填)
           * 3.data为传入的具体数据(必填)
           * 4.target为Page对象,一般为this(必填)
           * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
           */
          // WxParse.wxParse('article', 'html', article, that);
          if (res.data.signature) {
            wx.setStorage({
              key: "reg_info",
              data: res.data.signature,
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  goodschange(e) {
    let val = e.detail;
    this.setData({
      goodsID: parseInt(val),
    });
  },
  open() {
    wx.navigateTo({
      url:
        "/packageC/microshop/microShop_apply_reg_info/microShop_apply_reg_info?",
    });
    //microShop_apply_reg_info
  },
  //点击支付按钮 进行支付操作 同时弹出支付成功页面
  pay() {
    if (app._isTextEmpty(this.data.goodsID)) {
      wx.showToast({
        icon: "none",
        title: "请选择店主等级",
        duration: 1500,
      });
      return;
    }
    let that = this;
    let urlStr = app.getNetAddresss(this.data.nextUrl);
    urlStr += "&goods_id=" + this.data.goodsID;
    urlStr += "&total=" + 1;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.navigateTo({
            url:
              "/packageD/buy/myOrder_v2/myOrder_v2?tag=-2" +
              "&goodsId=" +
              that.data.goodsID +
              "&optionsId=" +
              "&total=1",
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});
