<template>
  <div class="container">
    <div style="height: 80%; position: relative">
      <div class="register-main" style="overflow-y: hidden; overflow-y: scroll">
        <div style="height: 400px">
          <div class="header-title">
            <span class="left-text">注册账号</span>
          </div>
          <el-row :gutter="10" class="register-body">
            <el-col
              :xs="24"
              :sm="12"
              :md="12"
              :lg="12"
              class="el-col-sm-push-6 el-col-md-push-6 el-col-lg-push-6"
            >
              <el-form
                ref="account"
                :model="account"
                :rules="rules"
                label-width="100px"
              >
                <el-form-item prop="username" label="用户名：">
                  <el-input
                    type="text"
                    v-model="account.username"
                    auto-complete="off"
                    :autofocus="true"
                    placeholder="请输入用户名"
                  ></el-input>
                </el-form-item>
                <el-form-item prop="realName" label="真实姓名：">
                  <el-input
                    type="text"
                    v-model="account.realName"
                    auto-complete="off"
                    :autofocus="true"
                    placeholder="请输入真实姓名"
                  ></el-input>
                </el-form-item>
                <el-form-item prop="password" label="密码：">
                  <el-input
                    type="password"
                    v-model="account.password"
                    auto-complete="off"
                    :autofocus="true"
                    placeholder="请输入密码"
                  ></el-input>
                </el-form-item>
                <el-form-item prop="password1" label="确认密码：">
                  <el-input
                    type="password"
                    v-model="account.password1"
                    auto-complete="off"
                    :autofocus="true"
                    placeholder="请确认密码"
                  ></el-input>
                </el-form-item>
                <el-form-item prop="phone" label="手机号：">
                  <el-input
                    type="text"
                    v-model="account.phone"
                    auto-complete="off"
                    :autofocus="true"
                    placeholder="请输入注册手机号"
                  ></el-input>
                </el-form-item>
                <el-form-item prop="email" label="邮箱：">
                  <el-input
                    type="text"
                    v-model="account.email"
                    auto-complete="off"
                    :autofocus="true"
                    placeholder="请输入注册邮箱"
                  ></el-input>
                </el-form-item>
                <el-form-item class="code" prop="code" label="邀请码：">
                  <el-input
                    type="text"
                    v-model="account.code"
                    auto-complete="off"
                    placeholder="请输入邀请码"
                  ></el-input>
                </el-form-item>
                <el-form-item style="margin-bottom: 0; width: 100%">
                  <el-button
                    type="primary"
                    style="width: 100%"
                    @click.native.prevent="handleRegister"
                    :loading="loading"
                    >注册</el-button
                  >
                </el-form-item>
                <el-form-item class="extra-text">
                  <router-link
                    :to="{ path: '/login' }"
                    class="login-text"
                    title="立即登录"
                    >已经拥有账户？登录</router-link
                  >
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { ajax as request } from "@/request.js";
import md5 from "js-md5";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.account.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var validpassword = (rule, value, callback) => {
      let reg = /[0-9a-zA-Z]{6,18}/;
      if (!reg.test(value)) {
        callback(new Error("密码必须是由6-18位数字和字母组合"));
      } else {
        callback();
      }
    };
    var validemail = (rule, value, callback) => {
      var regEmail =
        /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
      if (!regEmail.test(value)) {
        callback(new Error("邮箱格式不正确"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      account: {
        username: "",
        password: "",
        password1: "",
        realName: "",
        phone: "",
        email: "",
        code: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          //   { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
        ],
        realName: [
          { required: true, message: "请输入真实姓名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: validpassword, trigger: "blur" },
        ],
        code: [{ required: true, message: "请输入邀请码", trigger: "blur" }],
        password1: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: validatePass, trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入11位手机号", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: validemail, trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    handleRegister() {
      //注册
      this.$refs.account.validate((valid) => {
        if (valid) {
          let that = this;
          request({
            url: "/user/register",
            method: "post",
            params: {
              username: that.account.username,
              realName: that.account.realName,
              password: md5(md5(that.account.password)),
              phone: that.account.phone,
              email: that.account.email,
              code: that.account.code,
            },
          }).then(function (response) {
            //获取数据存入config
            if (response.data.success == true) {
              that.$notify.success({
                title: "提示",
                message: "注册成功！",
              });
              that.loading = true;
              that.$router.push({ path: "/login" });
            } else {
              that.$notify.error({
                title: "提示",
                message: response.data.message,
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
body {
  margin: 0px;
  background: url(../../assets/images/background.jpg) no-repeat;
  background-size: 100% 100%;
  background-attachment: fixed;
}
.container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: url(../../assets/images/background.jpg) no-repeat;
  background-size: 100% 100%;
  background-attachment: fixed;
}
.register-main {
  margin-top: 150px;
  position: absolute;
  padding: 18px;
  margin-left: 18%;
  width: 64%;
  height: 80%;
  background: #f7f8f1;
  box-shadow: 0 2px 40px 0 rgba(157, 187, 231, 0.5);
  border-radius: 4px;
  border-radius: 4px;

  background: -ms-linear-gradient(top, #ace, #00c1de); /* IE 10 */
  background: -moz-linear-gradient(top, #ace, #00c1de); /*火狐*/
  background: -webkit-gradient(
    linear,
    0% 0%,
    0% 100%,
    from(#ace),
    to(#00c1de)
  ); /*谷歌*/
  background: -webkit-linear-gradient(top, #ace, #00c1de);
  background: -o-linear-gradient(top, #ace, #00c1de); /*Opera 11.10+*/
}

.header-title {
  position: relative;
  padding: 15px 10px 15px;
  border-bottom: 5px solid #264672;

  .left-text {
    font-family: PingFangSC-Regular;
    font-size: 20px;
    color: #264672;
  }
}
.el-form-item__label {
  color: #264672 !important;
}
.el-input__inner {
  &::placeholder {
    font-family: PingFangSC-Regular;
  }
}

.register-body {
  padding: 40px 3%;
}
.extra-text {
  position: relative;
  margin-bottom: 0;
  padding-left: 2px;
}

.login-text {
  position: absolute;
  top: 4px;
  right: 2px;
  font-size: 12px;
  color: #aaa;
}

@media all and (max-width: 768px) {
  .register-main {
    margin: 20 auto 24px;
  }
}
</style>
