<template>
  <div class="auth-page register-page">
    <div class="auth-split">
      <div class="auth-brand" aria-hidden="true">
        <div class="auth-brand__accent" />
        <div class="auth-brand__inner">
          <img class="auth-brand__logo" src="/brand-logo.png" alt="安澜应急指挥" />
          <h1 class="auth-brand__title">安澜应急指挥</h1>
          <p class="auth-brand__tagline">创建账户 · 加入应急指挥协同</p>
          <ul class="auth-brand__bullets">
            <li>与组织统一权限体系对接</li>
            <li>数据与操作留痕可追溯</li>
            <li>注册后由管理员分配角色</li>
          </ul>
        </div>
      </div>
      <div class="auth-form-side">
        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
          <div class="form-head">
            <h2 class="form-title">{{ title }}</h2>
            <p class="form-sub">注册新账号</p>
          </div>
          <el-form-item prop="username">
            <el-input v-model="registerForm.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="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleRegister"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              auto-complete="off"
              placeholder="确认密码"
              @keyup.enter.native="handleRegister"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </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"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="register-code">
              <img :src="codeUrl" @click="getCode" class="register-code-img">
            </div>
          </el-form-item>
          <el-form-item class="submit-row">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              class="submit-btn"
              @click.native.prevent="handleRegister"
            >
              <span v-if="!loading">注 册</span>
              <span v-else>注 册 中...</span>
            </el-button>
            <div class="extra-link">
              <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <footer class="auth-footer">
      <span>安澜应急指挥平台</span>
    </footer>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login"

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"))
      } else {
        callback()
      }
    }
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      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" }
        ],
        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 => {
            const username = this.registerForm.username
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login")
            }).catch(() => {})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$brand: #242424;
$indigo: #4f46e5;

.auth-page {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--color-background, #fcfcfc);
}

.auth-split {
  flex: 1;
  display: flex;
  min-height: 100vh;
  width: 100%;
}

.auth-brand {
  position: relative;
  flex: 1 1 42%;
  min-width: 280px;
  background: $brand;
  color: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px 40px;
  overflow: hidden;
}

.auth-brand__accent {
  position: absolute;
  top: -20%;
  right: -10%;
  width: 60%;
  height: 70%;
  background: radial-gradient(ellipse at center, rgba($indigo, 0.35) 0%, transparent 70%);
  pointer-events: none;
}

.auth-brand__inner {
  position: relative;
  z-index: 1;
  max-width: 360px;
}

.auth-brand__logo {
  width: 56px;
  height: 56px;
  object-fit: contain;
  border-radius: 12px;
  margin-bottom: 16px;
  display: block;
}

.auth-brand__title {
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: #fafafa;
}

.auth-brand__tagline {
  margin: 0 0 28px 0;
  font-size: 14px;
  color: #a1a1aa;
  font-weight: 500;
}

.auth-brand__bullets {
  list-style: none;
  margin: 0;
  padding: 0;
  li {
    position: relative;
    padding-left: 20px;
    margin-bottom: 12px;
    font-size: 14px;
    line-height: 1.5;
    color: #d4d4d8;
    &:before {
      content: "";
      position: absolute;
      left: 0;
      top: 0.55em;
      width: 6px;
      height: 6px;
      border-radius: 50%;
      background: $indigo;
      box-shadow: 0 0 0 3px rgba($indigo, 0.25);
    }
  }
}

.auth-form-side {
  flex: 1 1 58%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 24px 48px;
  background: linear-gradient(180deg, #fafafa 0%, #f4f4f5 100%);
}

.register-form {
  width: 100%;
  max-width: 400px;
  border-radius: 16px;
  background: #fff;
  padding: 32px 28px 24px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.08);
  border: 1px solid var(--color-border, #e4e4e7);
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 40px;
    width: 14px;
    margin-left: 2px;
  }
}

.form-head {
  margin-bottom: 20px;
}

.form-title {
  margin: 0 0 6px 0;
  font-size: 22px;
  font-weight: 700;
  color: #0f172a;
  letter-spacing: 0.06em;
}

.form-sub {
  margin: 0;
  font-size: 13px;
  color: #71717a;
}

.submit-row {
  width: 100%;
  margin-bottom: 0;
}

.submit-btn {
  width: 100%;
}

.extra-link {
  text-align: right;
  margin-top: 12px;
}

.register-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.register-code-img {
  height: 40px;
}

.auth-footer {
  flex-shrink: 0;
  text-align: center;
  padding: 12px 16px 20px;
  font-size: 12px;
  color: #71717a;
  letter-spacing: 0.06em;
}

@media (max-width: 900px) {
  .auth-split {
    flex-direction: column;
    min-height: auto;
  }
  .auth-brand {
    flex: none;
    min-height: 220px;
    padding: 32px 24px;
  }
  .auth-form-side {
    flex: none;
    padding: 24px 16px 32px;
  }
}
</style>
