<template>
  <div class="register">
    <div class="btn" @click="openDialog">
      <slot></slot>
    </div>
    <el-dialog
      title="注册账号"
      :close-on-click-modal="false"
      :visible.sync="visible"
      lock-scroll
      append-to-body
      width="600px"
      :modal-append-to-body="false"
      class="dialog-box"
      destroy-on-close
    >
      <el-form
        ref="registerForm"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
        label-width="100px"
      >
        <el-form-item prop="account" label="账号：">
          <el-input
            v-model="registerForm.account"
            type="text"
            auto-complete="off"
            placeholder="账号"
          />
        </el-form-item>
        <el-form-item prop="password" label="密码：">
          <el-input
            v-model="registerForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleRegister"
          />
        </el-form-item>
        <el-form-item prop="confirmPassword" label="确认密码：">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            auto-complete="off"
            placeholder="确认密码"
            @keyup.enter.native="handleRegister"
          />
        </el-form-item>
        <div>
          <el-button
            :loading="register_loading"
            size="medium"
            type="primary"
            style="width: 100%"
            @click.native.prevent="handleRegister"
          >
            <span v-if="!register_loading">注 册</span>
            <span v-else>注 册 中...</span>
          </el-button>
        </div>
        <el-form-item style="width: 100%">
          <div style="float: right">
            <el-button type="text" @click="visible = false" size="mini"
              >使用已有账户登录</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { register } from "@/api/index";
export default {
  name: "RegisterForm",
  props: {},
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      register_loading: false,
      registerForm: {
        account: "",
        password: "",
        confirmPassword: "",
      },
      registerRules: {
        account: [
          { 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" },
        ],
      },
    };
  },
  watch: {},
  mounted() {},
  created() {},
  methods: {
    openDialog() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.registerForm.resetFields();
      });
    },

    submit() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.register_loading = true;
          register(this.registerForm).then((res) => {
            const account = this.registerForm.account;
            this.$alert(
              "<font color='red'>恭喜你，您的账号 " +
                account +
                " 注册成功！</font>",
              "系统提示",
              {
                type: "success",
              }
            )
              .then(() => {
                this.visible = false;
              })
              .catch(() => {});
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.dialog-box {
  :deep(.el-dialog__body) {
    max-height: 60vh;
    overflow-y: scroll;
  }
}
</style>
