<template xmlns="">
  <div class="container">
    <!-- <div class="logo"></div> -->
    <!-- 登录区域 -->
    <div class="content">
      <!-- 配图 -->
      <div class="pic"></div>
      <!-- 表单 -->
      <div class="field">
        <!-- [移动端]标题 -->
        <h2 class="mobile-title">
          <h3 class="title">爱思系统</h3>
        </h2>

        <!-- 表单 -->
        <div class="form-cont">
          <div class="login-title">账号注册</div>
          <div style="width: 320px;">
            <el-form ref="loginForm" :model="loginForm" :rules="LoginRules" class="login-form">

              <el-form-item prop="mobile">
                <el-input v-model="loginForm.mobile" type="text" auto-complete="off" placeholder="请输入手机号">
                  <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item>
              <el-form-item prop="mobileCode">
                <el-input v-model="loginForm.mobileCode" type="text" auto-complete="off" placeholder="短信验证码"
                  class="sms-login-mobile-code-prefix" @keyup.enter.native="handleLogin">
                  <template>
                    <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                  </template>
                  <template slot="append">
                    <span v-if="mobileCodeTimer <= 0" class="getMobileCode" @click="getSmsCode"
                      style="cursor: pointer;">获取验证码</span>
                    <span v-if="mobileCodeTimer > 0" class="getMobileCode">{{ mobileCodeTimer }}秒后可重新获取</span>
                  </template>
                </el-input>
              </el-form-item>
              <div class="service-laws">
                点击注册表明你已阅读并同意
                <a class="terms" href="https://www.jiandaoyun.com/index/term" target="_blank">《服务条款》</a>
                和
                <a class="terms" href="https://www.jiandaoyun.com/index/term/privacy" target="_blank">《隐私声明》</a>
              </div>
              <!-- 下方的下一步按钮 -->
              <el-form-item style="width:100%;">
                <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
                  @click.native.prevent="handleRegister()">
                  <span v-if="!loading">注 册</span>
                  <span v-else>操 作 中...</span>
                </el-button>
              </el-form-item>
              <!--  去登录 -->
              <div class="register-tip">
                已有账号？
                <span class="register-btn" @click="doLogin()">直接登录</span>
              </div>

            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { sendSmsCode } from "@/api/login";
import {setTenantId} from "@/utils/auth";
export default {
  data() {
    return {
      mobileCodeTimer: 0,
      loginForm: {
        mobile: "",
        mobileCode: "",
      },
      scene: 5,
      LoginRules: {
        // mobileCode: [
        //   { required: true, trigger: "blur", message: "密码不能为空" }
        // ],
        mobile: [
          { required: true, trigger: "blur", message: "手机号不能为空" },
          {
            validator: function (rule, value, callback) {
              if (/^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/.test(value) === false) {
                callback(new Error("手机号格式错误"));
              } else {
                callback();
              }
            }, trigger: "blur"
          }
        ]
      },
      loading: false,
    };
  },
  created() {

  },
  methods: {
    //跳转登录
    doLogin() {
      this.$router.push({ path: '/login' })
    },
    handleRegister() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          //设置默认租户
          setTenantId(1)
          this.$store.dispatch("SmsRegister", this.loginForm).then(() => {
            this.$router.push({ path: "/" }).catch(() => {
            });
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    },
    /** ========== 以下为升级短信登录 ========== */
    getSmsCode() {
      if (this.mobileCodeTimer > 0) return;
      this.$refs.loginForm.validate(valid => {
        if (!valid) return;
        sendSmsCode(this.loginForm.mobile, this.scene).then(res => {
          this.$modal.msgSuccess("获取验证码成功")
          this.mobileCodeTimer = 60;
          let msgTimer = setInterval(() => {
            this.mobileCodeTimer = this.mobileCodeTimer - 1;
            if (this.mobileCodeTimer <= 0) {
              clearInterval(msgTimer);
            }
          }, 1000);
        });
      });
    }
  }
};
</script>
<style lang="scss" scoped>
@import "~@/assets/styles/login.scss";

.login-title {
  color: #141e31;
  font-size: 32px;
  font-weight: 500;
  line-height: 48px;
  margin-bottom: 20px;
}

.service-laws {
  color: #525967;
  font-size: 14px;
  margin-bottom: 10px;
}

.service-laws .terms {
  color: #1890ff;
}

a {
  outline: none;
  text-decoration: none;
}

.register-tip {
  color: #141e31;
  font-size: 14px;
  line-height: 20px;
  margin: 10px 0 10px;
}

.register-btn {
  color: #1890ff;
  cursor: pointer;
}

.oauth-login {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.oauth-login-item {
  display: flex;
  align-items: center;
  margin-right: 10px;
}

.oauth-login-item img {
  height: 25px;
  width: 25px;
}

.oauth-login-item span:hover {
  text-decoration: underline red;
  color: red;
}

.sms-login-mobile-code-prefix {
  :deep(.el-input__prefix) {
    top: 22%;
  }
}
</style>
