// packageD/quickPay/member_remove_bank/member_remove_bank.js
const app = getApp();
Page({

  /**
     * 页面的初始数据
     */
  data: {
    member_phone: "",
    v2_code: "",
    count: "",
    isOpenCaptcha: false,
    imgCodeLink: "",
    captcha: "",
  },
  onLoad: function (options) {
    this.setData({
      card_id: options.card_id,
    });
    this.getMemberPhone();
  },
  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {

  },

  changeCaptcha (e) {
    let val = e.detail.value;
    this.setData({captcha: val});
  },
  v2CodeInp (e) {
    let val = e.detail.value;
    this.setData({
      v2_code: val,
    });
  },
  // 获取会员绑定的手机号
  getMemberPhone () {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-mobile");
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result !== 1) {
          app.tips(res.msg);
          return;
        }
        this.setData({
          check_open: res.data.sms_check_open
        });
        if (res.data.get_captcha && res.data.get_captcha.captcha) {
          this.setData({
            imgCodeLink: res.data.get_captcha.captcha.img,
            isOpenCaptcha: res.data.get_captcha.captcha.status
          });
        }
        if (res.data.mobile) {
          that.setData({
            member_phone: res.data.mobile
          });
        } else {
          wx.showModal({
            title: '提示',
            content: '商城会员没有绑定手机号，请绑定手机号',
            success (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/packageA/member/editmobile/editmobile',
                });
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1,
                });
              }
            }
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },

  // 刷新图形验证码
  getCaptchaImg () {
    let urlStr = app.getNetAddresss("plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-mobile");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: (response) => {
        let res = response.data;
        if (res.result !== 1) {
          app.tips(res.msg);
        }
        if (res.data.get_captcha && res.data.get_captcha.captcha) {
          this.setData({
            imgCodeLink: res.data.get_captcha.captcha.img
          });
        }
      }
    });
  },

  // 获取验证码
  VerificationCode() {
    if (this.data.isOpenCaptcha == 1 && app._isTextEmpty(this.data.captcha)) {
      app.tips("请填写图形验证码");
      return;
    }
    wx.showLoading({
      title: "发送中",
    });
    var that = this;
    var urlStr = app.getNetAddresss("plugin.converge_pay.frontend.controllers.quick-pay-bank-card.send-code");
    app._getNetWork({
      showToastIn: false,
      url: urlStr,
      data: {captcha: this.data.captcha},
      success: function (resdata) {
        var res = resdata.data;
        wx.hideLoading();
        if (res.result == 1) {
          that.getCode();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {},
    });
  },
  // 60秒倒计时
  getCode() {
    const TIME_COUNT = 60;
    if (!this.data.timer) {
      this.data.count = TIME_COUNT;
      this.data.timer = setInterval(() => {
        if (this.data.count > 0 && this.data.count <= TIME_COUNT) {
          this.data.count--;
          this.setData({
            count:this.data.count
          });
        } else {
          clearInterval(this.timer);
          this.data.count = 0;
          this.data.timer = null;
          this.setData({
            count:this.data.count,
            timer:this.data.timer
          });
        }
      }, 1000);
    }
  },

  confirmRemoveBank () {
    if (app._isTextEmpty(this.data.v2_code)) {
      app.tips("请输入验证码");
      return;
    }
    var urlStr = app.getNetAddresss("plugin.converge_pay.frontend.controllers.quick-pay-bank-card.del");
    app._getNetWork({
      showToastIn: false,
      url: urlStr,
      data: {
        card_id: this.data.card_id, 
        code: this.data.v2_code
      },
      success: function (resdata) {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result == 1) {
          wx.navigateBack({
            delta: 1,
          });
        }
      },
      fail: function (res) {},
    });
  },

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

  },

  onHide () {
    this.data.timer && clearInterval(this.data.timer);
  },
});