<!-- @format -->

<template>
  <div class="register">
    <div class="login-box">
      <el-form
        ref="registerRef"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
        :status-icon="true"
      >
        <h3 class="title">英才卡申请注册</h3>
        <el-form-item prop="userName">
          <el-input
            v-model="registerForm.userName"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="用户名"
          >
            <template #prefix
              ><svg-icon icon-class="user" class="el-input__icon input-icon"
            /></template>
          </el-input>

          <span class="remark"
            >3-20位，字母、数字、下划线的组合，以字母开头</span
          >
        </el-form-item>

        <el-form-item prop="passWord">
          <el-input
            v-model="registerForm.passWord"
            type="password"
            size="large"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter="handleRegister"
          >
            <template #prefix
              ><svg-icon
                icon-class="password"
                class="el-input__icon input-icon"
            /></template>
          </el-input>
          <span class="remark">
            8-20位英文（区分大小写）、数字、特殊字符的组合，特殊字符包括 " ! ""
            " * " " _ "
          </span>
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            size="large"
            auto-complete="off"
            placeholder="确认密码"
            @keyup.enter="handleRegister"
          >
            <template #prefix
              ><svg-icon
                icon-class="password"
                class="el-input__icon input-icon"
            /></template>
          </el-input>
          <span class="remark"> 保持两次密码一致 </span>
        </el-form-item>

        <el-form-item prop="realName">
          <el-input
            v-model="registerForm.realName"
            size="large"
            auto-complete="off"
            placeholder="姓名"
            @keyup.enter="handleRegister"
          >
            <template #prefix
              ><svg-icon icon-class="user" class="el-input__icon input-icon"
            /></template>
          </el-input>
          <span class="remark"> 请填写真实姓名 </span>
        </el-form-item>
        <el-form-item prop="idNumber">
          <el-input
            v-model="registerForm.idNumber"
            size="large"
            auto-complete="off"
            placeholder="身份证号"
            @keyup.enter="handleRegister"
          >
            <template #prefix
              ><el-icon><Postcard /></el-icon
            ></template>
          </el-input>
          <span class="remark"> 请填合法身份证号 </span>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input
            v-model="registerForm.phone"
            size="large"
            auto-complete="off"
            placeholder="手机号码"
            @keyup.enter="handleRegister"
          >
            <template #prefix>
              <el-icon><Iphone /></el-icon>
            </template>
          </el-input>
          <span class="remark"> 请填11位有效的手机号 </span>
        </el-form-item>

        <el-form-item prop="eMail">
          <el-input
            v-model="registerForm.eMail"
            size="large"
            auto-complete="off"
            placeholder="电子邮箱"
            @keyup.enter="handleRegister"
          >
            <template #prefix>
              <el-icon><Position /></el-icon>
            </template>
          </el-input>
          <span class="remark"> 请填写有效的电子邮箱 </span>
        </el-form-item>

        <!-- <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            size="large"
            v-model="registerForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter="handleRegister"
          >
            <template #prefix
              ><svg-icon
                icon-class="validCode"
                class="el-input__icon input-icon"
            /></template>
          </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%; margin: 20px 0">
          <el-button
            round
            :loading="loading"
            size="large"
            type="primary"
            style="width: 100%"
            @click.prevent="handleRegister"
          >
            <span v-if="!loading">注 册</span>
            <span v-else>注 册 中...</span>
          </el-button>
          <div style="float: right; margin-top: 10px">
            <router-link class="link-type" :to="'/login'"
              >使用已有账户登录</router-link
            >
          </div>
        </el-form-item>
      </el-form>
      <!--  底部  -->
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from "element-plus";
import { getCodeImg, register } from "@/api/login";

const router = useRouter();
const { proxy } = getCurrentInstance();

const registerForm = ref({
  userName: "",
  passWord: "",
  confirmPassword: "",
  code: "",
  uuid: "",
  realName: "",
  phone: "",
  eMail: "",
  idNumber: "",
});

const equalToPassword = (rule, value, callback) => {
  if (registerForm.value.passWord !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};

const registerRules = {
  userName: [
    { required: true, trigger: "blur", message: "请输入您的账号" },
    {
      min: 2,
      max: 20,
      message: "用户账号长度必须介于 2 和 20 之间",
      trigger: "blur",
    },
  ],
  passWord: [
    { required: true, trigger: "blur", message: "请输入您的密码" },
    {
      min: 5,
      max: 20,
      message: "用户密码长度必须介于 5 和 20 之间",
      trigger: "blur",
    },
    {
      pattern: /^[^<>"'|\\]+$/,
      message: "不能包含非法字符：< > \" ' \\\ |",
      trigger: "blur",
    },
  ],
  confirmPassword: [
    { required: true, trigger: "blur", message: "请再次输入您的密码" },
    { required: true, validator: equalToPassword, trigger: "blur" },
  ],
  realName: [{ required: true, trigger: "blur", message: "请填写真实姓名" }],
  idNumber: [
    { required: true, trigger: "blur", message: "请填写身份证号" },
    {
      pattern: /^(^\d{15}$|^\d{17}([0-9]|X)$)$/,
      message: "请检查身份证的格式",
      trigger: "blur",
    },
  ],
  phone: [
    { required: true, trigger: "blur", message: "请填写手机号" },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "请检查手机号是否正确",
      trigger: "blur",
    },
  ],
  eMail: [
    { required: true, trigger: "blur", message: "请填写邮箱" },
    {
      pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
      message: "请检查邮箱是否正确",
      trigger: "blur",
    },
  ],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};

const codeUrl = ref("");
const loading = ref(false);
const captchaEnabled = ref(true);

function handleRegister() {
  proxy.$refs.registerRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      register(registerForm.value)
        .then((res) => {
          const userName = registerForm.value.userName;
          ElMessageBox.alert(
            "<font>恭喜你，您的账号 " +
              userName +
              " 注册成功！返回进行重新登录</font>",
            "系统提示",
            {
              dangerouslyUseHTMLString: true,
              type: "success",
            }
          )
            .then(() => {
              router.push("/login");
            })
            .catch(() => {});
        })
        .catch(() => {
          loading.value = false;
          if (captchaEnabled) {
            getCode();
          }
        });
    }
  });
}

function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value =
      res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      registerForm.value.uuid = res.uuid;
    }
  });
}

getCode();
</script>

<style lang="scss" scoped>
::v-deep .el-input__suffix-inner {
  font-size: 22px;
  color: rgba(19, 206, 102, 0.8);
}
::v-deep .el-form-item--default {
  margin-bottom: 5px;
}
::v-deep .el-form-item__error {
  top: 68%;
  width: 100%;
  background: #fff;
}
.remark {
  color: #999;
  font-size: 12px;
}
.login-box {
  width: 32%;
  display: flex;
  background: #fff;
  box-shadow: 1.5px 3.99px 27px 0px rgba(0, 0, 0, 0.1);
  transition: 0.3s;
}
.left-icon {
  margin: auto;
  padding: 60px 40px;
  border-right: 1px solid #cccccc5e;
  text-align: center;
  flex: 0 0 48.333333%;
  max-width: 48.333333%;
}
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-bg.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  font-family: "montserrat";
}

.register-form {
  padding: 60px 50px;
  border-left: 1px solid #cccccc5e;
  margin: auto;
  background-color: rgba(255, 255, 255, 0.72);
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 20%;
  height: 40px;
  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: 40px;
  padding-left: 12px;
}
</style>
