// packageE/findpwd/findpwd.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    start1: false,
    imgcode: "",
    country_code: "",
    from: {
      code: "",
      country: "86",
      mobile: "",
      mobileErr: "",
      password: "",
      passwordErr: "",
      confirm_password: "",
      confirm_passwordErr: "",
      captcha: "",
    },
    codetext: "获取短信验证码",
    visibleSet: false,
    visibleSure: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getimgdata();
    // this.getCountryCode();
  },
  getyzm() {
    console.log("获取验证码");
    if (this.data.start1) {
      return false;
    }
    let time = 60;
    let that = this;
    that.setData({
      start1: true,
      codetext: "(" + time + ")秒后重新获取",
    });
    let set = setInterval(function () {
      that.setData({
        codetext: "(" + --time + ")秒后重新获取",
      });
    }, 1000);
    setTimeout(function () {
      that.setData({
        codetext: "获取短信验证码",
        start1: false,
      });
      clearInterval(set);
    }, 60000);
    that.verificationCode(set);
  },
  //发送验证码
  verificationCode(set) {
    let that = this;
    if (app._isTextEmpty(this.data.from.mobile)) {
      wx.showToast({
        icon: "none",
        title: "请填写手机号",
        duration: 1500,
      });
      that.setData({
        codetext: "获取短信验证码",
        start1: false,
      });
      clearInterval(set);
      return;
    }
    if (
      this.data.from.country == "86" &&
      app._isMoblie(this.data.from.mobile)
    ) {
      wx.showToast({
        icon: "none",
        title: "请填写正确的手机号",
        duration: 1500,
      });
      return;
    }

    if (app._isTextEmpty(this.data.from.captcha) && this.data.imgcode) {
      wx.showToast({
        icon: "none",
        title: "请填图形验证码",
        duration: 1500,
      });
      that.setData({
        codetext: "获取短信验证码",
        start1: false,
      });
      clearInterval(set);
      return;
    } else {
      let urlStr = app.getNetAddresss("member.register.sendCodeV2");
      urlStr += "&mobile=" + this.data.from.mobile;
      urlStr += "&state=" + this.data.from.country;
      urlStr += "&captcha=" + this.data.from.captcha;
      urlStr += "&code=" + this.data.from.code;
      urlStr += "&reset=1";
      urlStr += "&sms_type=2";
      // 发送获取验证码的请求
      app._getNetWork({
        url: urlStr,
        validateBol: 2,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              icon: "none",
              title: "已发送",
              duration: 1500,
            });
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
            //刷新图形验证码
            that.setData({
              "form.captcha": "",
            });
            that.getimgdata();
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  },
  value_one(e) {
    console.log(e.detail.value);
    this.setData({
      "from.mobile": e.detail.value,
    });
    console.log(this.data.from);
  },
  value_two(e) {
    if(e.currentTarget.dataset.key == 'img'){
      // 图形验证码
      this.setData({
        "from.captcha":e.detail.value
      });
    }else{
      this.setData({
        "from.code": e.detail.value,
      });
    }
  },
  value_thr(e) {
    this.setData({
      "from.password": e.detail.value,
    });
    console.log(this.data.from.password);
  },
  value_four(e) {
    this.setData({
      "from.confirm_password": e.detail.value,
    });
  },
  confirm() {
    if (app._isTextEmpty(this.data.from.mobile)) {
      wx.showToast({
        title: "请填写手机号",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (
      app._isMoblie(this.data.from.mobile) &&
      this.data.from.country == "86"
    ) {
      wx.showToast({
        title: "请填写正确的手机号",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (app._isTextEmpty(this.data.from.code)) {
      wx.showToast({
        title: "请填验证码",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (app._isTextEmpty(this.data.from.password)) {
      wx.showToast({
        title: "请填写密码",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (app._isTextEmpty(this.data.from.confirm_password)) {
      wx.showToast({
        title: "请填写确认密码",
        icon: "none",
        duration: 1000,
      });
      return;
    }

    if (this.data.from.password != this.data.from.confirm_password) {
      wx.showToast({
        title: "两次密码不一致",
        icon: "none",
        duration: 1000,
      });
      return;
    }

    if (this.data.imgcode) {
      if (app._isTextEmpty(this.data.form.captcha)) {
        wx.showToast({
          title: "图形验证码不能为空",
          icon: "none",
          duration: 1000,
        });
        return;
      }
    }

    var that = this;
    let urlStr = app.getNetAddresss("member.register.changePassword");
    let json = that.data.from;
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: "修改成功",
            icon: "none",
            duration: 1000,
            success() {
              // setTimeout(() => {
              //   wx.navigateTo({
              //     url: '/packageG/index/index',
              //   })
              // }, 1000);
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  //获取图片验证码
  getimgdata() {
    let that = this;
    let urlStr = app.getNetAddresss("home-page.getCaptcha");
    app._getNetWork({
      url: urlStr,
      validateBol: 2,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.captcha) {
            that.setData({
              imgcode: res.data.captcha.img,
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getCountryCode() {
    let that = this;
    let urlStr = app.getNetAddresss("member.register.getInviteCode");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            country_code: res.data.country_code,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  SetPwdClick () {
    this.setData({
      visibleSet: !this.data.visibleSet
    });
  },
  surePwdClick () {
    this.setData({
      visibleSure: !this.data.visibleSure
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

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

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