<template>
    <div class="register">
        <div class="register-card-container">
            <div class="title">
                艺考报名网页
            </div>
            <el-form
                    ref="registerForm"
                    :model="registerForm"
                    :rules="registerRules"
                    class="register-form"
            >
                <h3 class="register-title">个人注册</h3>
                <el-form-item prop="studentName">
                    <el-input v-model="registerForm.studentName" type="text" auto-complete="off"
                              placeholder="请输入姓名">
                    </el-input>
                </el-form-item>
                <el-form-item prop="idCard">
                    <el-input v-model="registerForm.idCard" type="text" auto-complete="off"
                              placeholder="请输入身份证号">
                    </el-input>
                </el-form-item>
                <el-form-item prop="phoneNumber">
                    <el-input v-model="registerForm.phoneNumber" type="text" auto-complete="off"
                              placeholder="请输入手机号">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                            v-model="registerForm.password"
                            type="text"
                            style="text-security:disc; -webkit-text-security:disc;"
                            auto-complete="off"
                            placeholder="请输入密码"
                            @keyup.enter.native="handleRegister"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item prop="confirmPassword">
                    <el-input
                            v-model="registerForm.confirmPassword"
                            type="text"
                            style="text-security:disc; -webkit-text-security:disc;"
                            auto-complete="off"
                            placeholder="请再次输入密码确认"
                            @keyup.enter.native="handleRegister"
                    >
                    </el-input>
                </el-form-item>
                <!-- <el-form-item prop="code" v-if="captchaEnabled">
                  <el-input
                    v-model="registerForm.code"
                    auto-complete="off"
                    placeholder="验证码"
                    style="width: 63%"
                    @keyup.enter.native="handleRegister"
                  >
                  </el-input>
                  <div class="register-code">
                    <img :src="codeUrl" @click="getCode" class="register-code-img"/>
                  </div>
                </el-form-item> -->
                <el-form-item style="width:100%;">
                    <el-button
                            :loading="loading"
                            size="medium"
                            type="primary"
                            class="register-button"
                            @click.native.prevent="handleRegister"
                    >
                        <span v-if="!loading">注 册</span>
                        <span v-else>注 册 中...</span>
                    </el-button>
                    <div style="float: right;">
                        <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
                    </div>
                </el-form-item>
            </el-form>
        </div>
        <!--  底部  -->
        <div class="el-register-footer">
            <span></span>
        </div>
    </div>
</template>

<script>
import {register} from "@/api/login";
import {checkIdcard, validatePhone} from "@/utils/validate/index"

export default {
    name: "Register",
    data() {
        // 确认密码
        const equalToPassword = (rule, value, callback) => {
            if (this.registerForm.password !== value) {
                callback(new Error("两次输入的密码不一致"));
            } else {
                callback();
            }
        };

        return {
            codeUrl: "",
            confirmPassword: "", // 确认密码
            registerForm: {
                studentName: "", // 学生姓名
                idCard: "", // 身份证
                phoneNumber: "",
                password: "", // 密码
                // code: "",
                // uuid: ""
            },
            registerRules: {
                studentName: [
                    {required: true, trigger: "blur", message: "请输入您的姓名"},
                ],
                idCard: [
                    {required: true, trigger: "blur", message: "请输入您的身份证号"},
                    {required: true, validator: checkIdcard, trigger: "blur"}
                ],
                phoneNumber: [
                    {required: true, trigger: "blur", message: "请输入您的手机号"},
                    {required: true, validator: validatePhone, trigger: "blur"}
                ],
                password: [
                    {required: true, trigger: "blur", message: "请输入您的密码"},
                  {pattern:/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,20}$/, message: '用户密码长度为 6 到 20 个字符，且必须包含字母、数字以及特殊符号', trigger: 'blur'}

                ],
                confirmPassword: [
                    {required: true, trigger: "blur", message: "请再次输入您的密码"},
                    {required: true, validator: equalToPassword, trigger: "blur"}
                ],
                // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
            },
            loading: false,
            captchaEnabled: true
        };
    },
    created() {
        // this.getCode();
    },
    methods: {
        // getCode() {
        //   getCodeImg().then(res => {
        //     this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        //     if (this.captchaEnabled) {
        //       this.codeUrl = "data:image/gif;base64," + res.img;
        //       this.registerForm.uuid = res.uuid;
        //     }
        //   });
        // },
        handleRegister() {
            this.$refs.registerForm.validate(valid => {
                if (valid) {
                    this.loading = true;
                    register(this.registerForm).then(res => {
                        this.$message({
                            type: 'success',
                            message: "注册成功"
                        });
                        this.$router.push({
                            path: '/login'
                        });
                        // const studentName = this.registerForm.studentName;
                        // this.$alert(
                        //   "恭喜你，您的账号 " + studentName + " 注册成功！", '系统提示', {
                        //   confirmButtonText: '确定',
                        //   callback: action => {

                        //   }
                        // }).then(() => {

                        // })
                    }).catch(() => {
                        this.loading = false;
                        if (this.captchaEnabled) {
                            this.getCode();
                        }
                    })
                }
            });
        }
    }
};
</script>

<style lang="less" scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url("../assets/image/background.png");
  // background-size: cover;

  .register-card-container {
    display: flex;
    position: relative;
    width: 520px;
    // height: 411px;
    background-color: #ffffff;
    border-radius: 6px;
    //padding: 20px;
    //justify-content: center;
    //align-items: center;

  }
}

.title {
  width: 310px;
  height: 56px;
  line-height: 56px;
  position: absolute;
  left: 105px;
  top: -28px;
  text-align: center;
  justify-content: center;
  color: #ffffff;
  font-size: 18px;
  font-weight: 900;
  background-size: cover;
  background-image: url("../assets/image/title.png");
}

.register-title {
  margin: 30px auto 30px;
  text-align: center;
  color: #000000;
  font-weight: 900;
}

.register-form {

  border-radius: 6px;
  // border: 1px solid;
  width: 413px;
  // height: 411px;
  margin: 0 auto;
  // padding: 26px 85px 26px 50px;

  /deep/ .el-input .el-input__inner {
    height: 48px;

  }
}

.register-button {
  width: 413px;
  height: 60px;
  background: #416af2;
  border-radius: 6px;
}

.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.register-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.register-code-img {
  height: 38px;
}
</style>
