<template>
  <div class="smsLogin-wrap">
    <div class="title">
      <i class="icon icon-guanbi" @click="goBack"></i>
    </div>
    <div class="tab">
      <p>短信验证码登录</p>
    </div>
    <p class="info">未注册的手机号将自动创建Hooli账户</p>
    <div class="login">
      <div class="flex align-c" :class="{active: valueFocus}">
        <p class="areaCode" @click="getAreaCode">
          <span>{{areaCodeItem.code}}</span>
          <i class="icon icon-xialatishi"></i>
        </p>
        <input
          v-model="value"
          placeholder="请输入手机号"
          @focus="focus('value')"
          @blur="blur('value')"/>
        <i class="icon icon-shuruqingkong close" v-if="value.length && valueFocus" @click="clean('value')"></i>
      </div>
    </div>
    <van-button
      type="default"
      class="btn"
      :class="{disabled: disabled}"
      @click="loginHandle"
    >确认</van-button>
    <p class="other" @click="goNextPage('login')">使用密码登录</p>
  </div>
</template>

<script>
export default {
  name: "SmsLogin",
  data() {
    return {
      loading: false,
      value: "",
      disabled: true,
      areaCodeItem: {
        id: "317",
        name: "中国",
        code: "+86"
      },
      lang: this.$store.state.langStr,
      valueFocus: false
    };
  },
  created: function() {
    // 获取国家区号
    let areaCodeItem = this.$util.storage.fetch("areaCodeItem");
    if (areaCodeItem) this.areaCodeItem = areaCodeItem;
  },
  mounted: function() {},
  watch: {
    value(val) {
      val.length ? (this.disabled = false) : (this.disabled = true);
    }
  },
  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;
      }
    },
    // 跳转国家区号选择
    getAreaCode() {
      this.$router.push({
        path: "/" + this.lang + "/areaCode"
      });
    },
    goNextPage(val) {
      this.$router.push({
        path: "/" + this.lang + "/" + val
      });
    },
    loginHandle: function() {
      if (this.disabled === true) {
        return;
      }
      this.disabled = true;
      let regMobile = /^[0-9]*$/;
      let params = {};
      params.url = this.$apis.api_user_verification;
      params.data = {
        status: "5",
        mobile: this.value,
        countryId: this.areaCodeItem.id
      };
      if (!regMobile.test(this.value)) {
        this.$Toast("手机格式不正确");
        this.disabled = false;
        return false;
      }
      this.$net.req(params, this).then(
        res => {
          this.disabled = false;
          if (Number(res.code) === 0) {
            this.$util.sessionStorage.save("sendType", 5);
            let data = {
              key: "mobile",
              value: this.value
            };
            this.$util.sessionStorage.save("registerItem", data);
            this.$router.push({
              path: "/" + this.lang + "/verificationCode",
              query: {
                value: this.value
              }
            });
          }
        },
        e => {
          this.disabled = false;
          this.$Toast(e.msg);
        }
      );
    },
    // 清空
    clean(val) {
      this[val] = "";
    },
    // 返回
    goBack() {
      window.history.length > 1 ? this.$router.go(-1) : this.$router.push("/");
    }
  }
};
</script>

<style scoped lang="less">
.smsLogin-wrap {
  padding: 0 20px;
  background: #fff;
  min-height: 100vh;
  .title {
    display: flex;
    justify-content: flex-end;
    margin: 5px -5px 30px 0;
    i {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 34px;
      height: 34px;
      font-size: 14px;
      color: #ccc;
    }
  }
  .tab {
    display: flex;
    p {
      font-size: 24px;
      color: #222;
      font-weight: 500;
      line-height: 34px;
      margin: 0 20px 0 0;
    }
  }
  .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;
    }
    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;
      color: #ddd;
    }
    .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;
  }
  .other {
    margin-top: 20px;
    font-size: 13px;
    font-weight: 400;
    color: #999;
    line-height: 18px;
  }
}
</style>
