<template>
    <div class="backdrop" :style="{backgroundImage: 'url({'+(this.baseImg)+')'}">
        <el-container>
            <el-header style="display:block;position:relative;margin:auto;">
                <!-- logo -->
                <img :src="this.baseLogo" alt="" class="rightulliimg" />
            </el-header>
             <el-main>
                <!-- 中间部分 -->
                <div class="login">
                  <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
                    <el-form-item prop="username">
                        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="手机号">
                            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="code">
                        <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 53%;" @keyup.enter.native="handleLogin">
                            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                        </el-input>
                        <div class="login-code">
                            <img :src="codeUrl" @click="getCode" class="login-code-img" />
                        </div>
                    </el-form-item>
                    <el-form-item prop="cade">
                        <el-input v-model="loginForm.shortMessageCode" type="text"  maxlength="" auto-complete="off" placeholder="短信验证码" id="" onkeydown="enterHandler(event)" style="  width: 120px; ">
                            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                        </el-input>
                     <el-button @click="sendMessage" :disabled="sendShortMessageBtn">{{codeBtnWord}}</el-button>
                    </el-form-item>

                    <el-button :loading="loading" size="medium" type="primary" @click.native.prevent="handleLogin" style="width: 190px;border-radius: 50px;margin-left:10%;margin-top:10%">
                        <span v-if="!loading">注 册</span>
                        <span v-else>注 册 中...</span>
                    </el-button>
                  </el-form>
                </div>
              </el-main>
        </el-container>
    </div>
</template>
<script>
import { getCodeImg, sendShortMessage, chooseCompanyLogin} from "@/api/login";
export default {
    name: "register",
    data() {
        return {
            loginForm: {
                shortMessageCode: '',
                username: "",
                code: "",
                uuid: "",
                unionId : ''
            },
            codeUrl : '',
            loading : false,
            sendShortMessageBtn : false,
            codeBtnWord : '获取验证码',
            loginRules: {
                username: [
                    {
                        required: true,
                        trigger: "blur",
                        message: "用户名不能为空",
                    },
                ],
                code: [
                    {
                        required: true,
                        trigger: "change",
                        message: "验证码不能为空",
                    },
                ]
            },
            baseLogo: require('../assets/images/lgo.png'),
            baseImg: require('../assets/images/logi_bg1.jpg'),
        };
    },
    created() {
      this.loginForm.unionId = this.getUrlKey("unionId");
      this.getCode();
    },
    methods:{
      getCode() {
          getCodeImg().then((res) => {
              this.codeUrl = "data:image/gif;base64," + res.img;
              this.loginForm.uuid = res.uuid;
          });
      },
      handleLogin() {
          this.$refs.loginForm.validate((valid) => {
              if (valid) {
                  this.loading = true;
                  this.loginForm.type = "2";
                  // 密码不能为空
                  if (this.loginForm.shortMessageCode == null || this.loginForm.shortMessageCode == "") {
                    this.msgError("请输入短信验证码!");
                    this.loading = false;
                    return;
                  }

                  this.$store
                      .dispatch("Login", this.loginForm)
                      .then((data) => {
                        console.log("跳转认证!")
                        this.loading = false;
                        this.$router.push({ path: "/certification" });
                      })
                      .catch(() => {
                          this.loading = false;
                          this.getCode();
                      });
              }
          });
      },
      sendMessage(){
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                  // 调用获取短信验证码接口
                  sendShortMessage(this.loginForm.username, this.loginForm.code, this.loginForm.uuid).then(response => {
                    this.sendShortMessageBtn = true;
                    this.msgSuccess("发送成功!");

                    // 因为下面用到了定时器，需要保存this指向
                    let that = this
                    that.waitTime = 60;
                    this.codeBtnWord = `${this.waitTime}s 后重新获取`
                    let timer = setInterval(function(){
                        if(that.waitTime>1){
                            that.waitTime--
                            that.codeBtnWord = `${that.waitTime}s 后重新获取`
                        }else{
                            clearInterval(timer)
                            that.codeBtnWord = '获取验证码'
                            that.waitTime = 60
                            that.getCode();
                            that.sendShortMessageBtn = false;
                        }
                    },1000)

                  }).catch((response)=>{
                    this.getCode();
                  });

                }
            })
      },
      getUrlKey(name) {
          return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
      },
    }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-size: cover;
}
// 中间背景图
.login-form {
    border-radius: 6px;
    width: 935px;
    height: 434px;
    padding: 73px 138px 103px 597px;
    background-image: url("../assets/images/login_bg2.png");
    background-size: 935px 434px;
    background-repeat: no-repeat;
    margin-top: 40px;
    // 输入框大小
    .el-input {
        height: 40px;
        width: 100%;
        input {
            height: 40px;
            width: 100%;
        }
    }
    .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 2px;
    }
}
.login-tip {
    font-size: 13px;
    text-align: center;
}
// 验证码
.login-code {
    width: 33%;
    height: 38px;
    float: right;
        margin-right: 6%;

    img {
        cursor: pointer;
        vertical-align: middle;
    }
}
.login-code-img {
    height: 38px;
}
// 大背景图
.backdrop {

    background-repeat: no-repeat;
    background-size: 1536px 752px;
    background-size: cover;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-position: right top;
    background-attachment: fixed;
}
.rightulliimg{
    width: 860px;

}
.divider_left{
    margin-left: -40px;
}.el-button.disabled-style {
    background-color: #EEEEEE;
    color: #CCCCCC;
}
</style>
