<template>
  <div class="set-nickname-wrap">
    <nav-bar title=""/>
    <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">为了给您的账号提供更好的安全保障，请绑定{{registerType === 'mobile' ? '手机号' : '邮箱'}}</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()"
          @blur="blur()"/>
        <i class="icon icon-shuruqingkong close" v-if="value.length && valueFocus" @click="clean('value')"></i>
      </div>
    </div>
    <div class="protocol">注册即表示同意<i>Hooli服务协议</i></div>
    <van-button
      type="default"
      class="btn"
      :class="{disabled: disabled}"
      @click="loginHandle">
      {{registerType === 'mobile' ? '绑定手机' : '绑定邮箱'}}
    </van-button>
  </div>
</template>

<script>
import NavBar from "@/components/common/NavBar";

export default {
  name: "Register",
  components: {
    NavBar
  },
  data() {
    return {
      registerType: "mobile",
      loading: false,
      lang: this.$store.state.langStr,
      areaCodeItem: {
        id: "317",
        name: "中国",
        code: "+86"
      },
      value: "",
      valueFocus: false,
      passwordFocus: false,
      disabled: true
    };
  },
  created: function() {
    // 获取国家区号
    let areaCodeItem = this.$util.storage.fetch("areaCodeItem");
    if (areaCodeItem) this.areaCodeItem = areaCodeItem;
  },
  watch: {
    value(val) {
      val.length ? (this.disabled = false) : (this.disabled = true);
    }
  },
  mounted: function() {},
  methods: {
    // 输入框获取焦点
    focus() {
      this.valueFocus = true;
    },
    // 输入框失去焦点
    blur() {
      this.valueFocus = false;
    },
    // 切换手机、邮箱
    toggleRegisterType(val) {
      this.registerType = val;
      this.value = "";
    },
    // 验证手机号邮箱
    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 params = {};
      params.url = this.$apis.api_user_verification;
      params.data = {
        status: "4"
      };
      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 (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("sendType", 4);
            this.$router.push({
              path: "/" + this.lang + "/verificationCode",
              query: {
                value: this.value
              }
            });
          }
        },
        e => {
          this.disabled = false;
          this.$Toast(e.msg);
        }
      );
    },
    // 跳转国家区号选择
    getAreaCode() {
      this.$router.push({
        path: "/" + this.lang + "/areaCode"
      });
    },
    // 清空
    clean(val) {
      this[val] = "";
    }
  }
};
</script>

<style scoped lang="less">
.set-nickname-wrap {
  padding: 0 20px;
  min-height: 100vh;
  .van-nav-bar {
    margin-bottom: 30px;
  }
  .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 {
      color: #ddd;
      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>
