<template>
  <div class="forget-wrap">
    <div class="title">
      <i class="icon icon-jiantou-zuo" @click="goBack"></i>
    </div>
    <div class="tab">
      <p :class="{active: registerType === 'mobile'}" @click="toggleRegisterType('mobile')">手机找回密码</p>
      <p :class="{active: registerType === 'mail'}" @click="toggleRegisterType('mail')">邮箱找回密码</p>
    </div>
    <p class="info">为了您的账号安全，请设置密码<br>8-16位，字母+数字</p>
    <div class="login">
      <div class="flex align-c" :class="{active: valueFocus}">
        <i class="icon icon-youxiang" v-if="registerType === 'mail'"></i>
        <p class="areaCode" v-else @click="getAreaCode">
          <span>{{areaCodeItem.code}}</span>
          <i class="icon icon-xialatishi"></i>
        </p>
        <input
          v-model="value"
          :placeholder="registerType === 'mail' ? '请输入邮箱地址' : '请输入手机号'"
          @focus="focus('value')"
          @blur="blur('value')"/>
        <i class="icon icon-shuruqingkong close" v-if="value.length && valueFocus" @click="clean('value')"></i>
      </div>
      <div class="flex align-c" :class="{active: passwordFocus}">
        <i
          class="icon"
          :class="[showPassowrd ? 'icon-mimakejian' : 'icon-mimayincang']"
          @click="showPassowrd = !showPassowrd"
        ></i>
        <input
          v-model="password"
          :type="showPassowrd ? 'text' : 'password'"
          placeholder="设置密码"
          @focus="focus('password')"
          @blur="blur('password')"/>
        <i class="icon icon-shuruqingkong close" v-if="password.length && passwordFocus" @click="clean('password')"></i>
      </div>
    </div>
    <van-button
      type="default"
      class="btn"
      :class="{disabled: disabled}"
      @click="loginHandle">
      {{registerType === 'mobile' ? '验证手机' : '验证邮箱'}}
    </van-button>
  </div>
</template>

<script>
export default {
  name: "Forget",
  data() {
    return {
      registerType: "mobile",
      loading: false,
      password: "",
      areaCodeItem: {
        id: "317",
        name: "中国",
        code: "+86"
      },
      value: "",
      valueFocus: false,
      passwordFocus: false,
      disabled: true,
      showPassowrd: false
    };
  },
  created: function() {
    // 获取国家区号
    let areaCodeItem = this.$util.storage.fetch("areaCodeItem");
    if (areaCodeItem) this.areaCodeItem = areaCodeItem;
  },
  watch: {
    value(val) {
      val.length && this.password.length
        ? (this.disabled = false)
        : (this.disabled = true);
    },
    password(val) {
      val.length && this.value.length
        ? (this.disabled = false)
        : (this.disabled = true);
    }
  },
  mounted: function() {},
  methods: {
    // 输入框获取焦点
    focus(value) {
      if (value === "value") {
        this.valueFocus = true;
      } else if (value === "password") {
        this.passwordFocus = true;
      }
    },
    // 输入框失去焦点
    blur(value) {
      if (value === "value") {
        this.valueFocus = false;
      } else if (value === "password") {
        this.passwordFocus = false;
      }
    },
    // 切换手机、邮箱
    toggleRegisterType(val) {
      this.registerType = val;
      this.value = "";
      this.password = "";
      this.showPassowrd = false;
    },
    // 验证手机号邮箱
    loginHandle() {
      if (this.disabled === true) {
        return;
      }
      this.disabled = true;
      let regMail = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
      let regMobile = /^[0-9]*$/;
      let regPassword = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,16}$/;
      let lang = this.$store.state.langStr;
      let params = {};
      params.url = this.$apis.api_user_verification;
      params.data = {
        status: "2"
      };
      if (this.registerType === "mail" && !regMail.test(this.value)) {
        this.$Toast("邮箱格式不正确");
        this.disabled = false;
        return false;
      }
      if (this.registerType === "mobile" && !regMobile.test(this.value)) {
        this.$Toast("手机格式不正确");
        this.disabled = false;
        return false;
      }
      if (!regPassword.test(this.password)) {
        this.$Toast("密码格式不正确");
        this.disabled = false;
        return false;
      }
      if (this.registerType === "mobile") {
        params.data.mobile = this.value;
        params.data.countryId = this.areaCodeItem.id;
      } else if (this.registerType === "mail") {
        params.data.email = this.value;
      }
      this.$net.req(params, this).then(
        res => {
          this.disabled = false;
          if (Number(res.code) === 0) {
            let data = {
              key: this.registerType,
              value: this.value
            };
            this.$util.sessionStorage.save("registerItem", data);
            this.$util.sessionStorage.save(
              "password",
              this.$util.aesEncrypt(this.password)
            );
            this.$util.sessionStorage.save("sendType", 2);
            this.$router.push({
              path: "/" + lang + "/verificationCode",
              query: {
                value: this.value
              }
            });
          }
        },
        e => {
          this.disabled = false;
          this.$Toast(e.msg);
        }
      );
    },
    // 跳转国家区号选择
    getAreaCode() {
      let lang = this.$store.state.langStr;
      this.$router.push({
        path: "/" + lang + "/areaCode"
      });
    },
    // 清空
    clean(val) {
      this[val] = "";
    },
    // 返回
    goBack() {
      window.history.length > 1 ? this.$router.go(-1) : this.$router.push("/");
    }
  }
};
</script>

<style scoped lang="less">
.forget-wrap {
  padding: 0 20px;
  background: #fff;
  min-height: 100vh;
  .title {
    display: flex;
    margin: 5px 0 30px -5px;
    i {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 34px;
      height: 34px;
      font-size: 18px;
      color: #ccc;
    }
  }
  .tab {
    display: flex;
    p {
      font-size: 24px;
      color: #eee;
      font-weight: 500;
      line-height: 34px;
      margin: 0 20px 0 0;
    }
    p.active {
      color: #222;
    }
  }
  .info {
    font-size: 13px;
    font-weight: 400;
    color: rgba(204, 204, 204, 1);
    line-height: 22px;
    margin: 10px 0 28px;
  }
  .login {
    > .active {
      border-color: #ff523d;
    }
    > div {
      border-bottom: 1px solid #eee;
      height: 60px;
      padding: 18px 0 18px 10px;
    }
    i {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 24px;
      height: 24px;
      font-size: 20px;
      color: #eee;
      flex-shrink: 0;
    }
    input {
      height: 25px;
      margin-left: 20px;
      border: 0;
      width: 100%;
      color: #444;
      font-size: 18px;
      background: transparent;
    }
    ::-webkit-input-placeholder {
      color: #ccc;
    }
    ::-moz-placeholder {
      color: #ccc;
    }
    ::-moz-placeholder {
      color: #ccc;
    }
    :-ms-input-placeholder {
      color: #ccc;
    }
    .close {
      margin-right: 10px;
    }
    .areaCode {
      height: 25px;
      background: rgba(238, 238, 238, 1);
      border-radius: 4px;
      display: flex;
      align-items: center;
      padding: 0 4px;
      margin-right: -10px;
      span {
        color: #444;
        font-size: 13px;
        flex-shrink: 0;
      }
      i {
        font-size: 8px;
        margin-left: 5px;
        flex-shrink: 0;
        width: auto;
        height: auto;
        color: #444;
      }
    }
  }
  .btn {
    margin: 40px 0 0;
    height: 45px;
    width: 100%;
    font-size: 18px;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
    line-height: 28px;
    background: linear-gradient(
      135deg,
      rgba(255, 143, 85, 1) 0%,
      rgba(255, 108, 80, 1) 51%,
      rgba(255, 84, 61, 1) 100%
    );
    border-radius: 25px;
  }
  .btn.disabled {
    background: #eee;
  }
}
</style>
