// mycomponent/yz_bindMobile/yz_bindMobile.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showVer: {
      type: null,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    showVerification: false, //绑定手机
    imgcode: "",
    count: "",
    form: {
      mobile: "",
      //验证码
      code: "",
      captcha: "",
    },
    showContent: false,
    protocol_title: "",
    protocol_content: "",
  },
  observers: {
    showVer: function (show) {
      this.setData({
        showVerification: show,
      });
      console.log(show, ";sdasdasd");
    },
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.initShow();
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    showContentTap(){
      this.setData({
        showContent:true
      });
    },
    initShow() {
      this.getimgdata();
      this.getProtocolInfo();
      this.showVerification = true;
    },
    closeShow() {
      this.setData({
        showVerification: false,
      });
    },
    hide() {
      this.setData({
        showVerification: false,
      });
    },
    getProtocolInfo() {
      var that = this;
      var urlStr = app.getNetAddresss("setting.get-member-protocol");
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              protocol_content: res.data.content,
              protocol_title: res.data.title,
            });
          }
        },
        fail: function (res) {},
      });
    },
    getimgdata() {
      var that = this;
      var urlStr = app.getNetAddresss("home-page.getCaptcha");
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            if (res.data.captcha.status && res.data.captcha.status == "0") {
              that.setData({
                imgcode: null,
              });
            }
            if (res.data.captcha.img) {
              that.setData({
                imgcode: res.data.captcha.img,
              });
            }
          }
        },
        fail: function (res) {},
      });
    },
    tapPhone(e) {
      console.log(e);
      let key = e.currentTarget.dataset.key;
      let tag = e.detail.value;
      if (key == "phone") {
        this.setData({
          "form.mobile": tag,
        });
      } else if (key == "cap") {
        this.setData({
          "form.captcha": tag,
        });
      } else if (key == "code") {
        this.setData({
          "form.code": tag,
        });
      }
    },
    // 获取验证码
    VerificationCode() {
      if (app._isTextEmpty(this.data.form.captcha) && this.data.imgcode) {
        wx.showToast({
          title: "请填图形验证码",
          icon: "none",
          duration: 1000,
        });
        return;
      }
      if (app._isTextEmpty(this.data.form.mobile)) {
        wx.showToast({
          title: "请填写手机号",
          icon: "none",
          duration: 1000,
        });
      } else {
        wx.showLoading({
          title: "发送中",
        });
        var that = this;
        var urlStr = app.getNetAddresss("member.register.sendCodeV2");
        app._getNetWork({
          showToastIn: false,
          url: urlStr,
          data: {
            mobile: this.data.form.mobile,
            captcha: this.data.form.captcha,
          },
          success: function (resdata) {
            var res = resdata.data;
            wx.hideLoading({
              success: (res) => {},
            });
            if (res.result == 1) {
              that.getCode();
            } else {
              that.setData({
                "form.captcha": "",
              });
              that.getimgdata();
            }
          },
          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);
      }
    },
    bindMobile() {
      //发送获取验证码的请求
      if (app._isTextEmpty(this.data.form.mobile)) {
        wx.showToast({
          title: "手机号码不能为空",
          icon: "none",
          duration: 1000,
        });
        return;
      }
      if (app._isMoblie(this.data.form.mobile)) {
        wx.showToast({
          title: "请输入正确的手机号",
          icon: "none",
          duration: 1000,
        });
        return;
      }
      if (app._isTextEmpty(this.data.form.code)) {
        wx.showToast({
          title: "请填写验证码",
          icon: "none",
          duration: 1000,
        });
        return;
      }
      if (this.imgcode) {
        if (app._isTextEmpty(this.data.form.captcha)) {
          wx.showToast({
            title: "图形验证码不能为空",
            icon: "none",
            duration: 1000,
          });
          return;
        }
      }
      var that = this;
      var urlStr = app.getNetAddresss("member.member.justBindMobile");
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        data: this.data.form,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              title: "绑定成功！",
              icon: "none",
              duration: 1000,
            });
            that.setData({
              showContent: false,
              showVerification: false,
            });
            that.triggerEvent("bindSuccess");
          }
        },
        fail: function (res) {},
      });
    },
  },
});
