// pages/member/rechargeCode/rechargeCode.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: "",
    type_name: "",
    price: "",
    isShowType: false,
    isDisabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.qrCode) {
      this.setData({
        qrCode: options.qrCode
      });
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      console.log(scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split('&');
        console.log(info_arr);

        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          // 这是自提点
          if (chil_arr[0] == 'k') {
            this.setData({
              code: chil_arr[1]
            });
          }
          if(chil_arr[0] == 'mid'){
            app._setMid(chil_arr[1]);
          }
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  codecheck: function (e) {
    let val = e.detail.value;
    this.setData({
      code: val.trim()
    });
  },
  codeinp: function (e) {
    let val = e.detail.value;
    this.setData({
      code: val.trim()
    });
  },
  //验证
  verifyCode() {
    if (app._isTextEmpty(this.data.code)) {
      wx.showToast({
        icon: 'none',
        title: '请填写充值码',
        duration: 1500
      });
      return;
    }
    let that = this;
    let urlStr = app.getNetAddresss("plugin.recharge-code.frontend.code.get-recharge-code");
    urlStr += '&code_key=' + this.data.code;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isShowType: true,
            type_name: res.data.recharge_code.type_name,
            price: res.data.recharge_code.price
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //提交
  submit() {
    if (app._isTextEmpty(this.data.code)) {
      wx.showToast({
        icon: 'none',
        title: '请填写充值码',
        duration: 1500
      });
      return;
    }
    var that = this;
    let urlStr = app.getNetAddresss("plugin.recharge-code.frontend.code.recharge");
    urlStr += '&code_key=' + this.data.code;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        that.setData({
          'isDisabled': true
        });
        if (res.result == 1) {
          if (app._isTextEmpty(res.data.jump_url)) {
            console.log('空的');
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500,
              success: function () {
                setTimeout(function () {
                  //要延时执行的代码
                  wx.reLaunch({
                    url: '/packageG/index/index'
                  });
                }, 1500); //延迟时间
              },
            });

          } else {
            console.log('不空的');
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500,
              success: function () {
                setTimeout(function () {
                  //要延时执行的代码
                  wx.navigateTo({
                    url: res.data.jump_url,
                    icon: 'none'
                  });
                }, 1500); //延迟时间
              },
            });
          }

        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
});
