<template>
  <div class="register">
    <el-form ref="registerRef" :model="registerForm" :rules="registerRules" class="register-form">
      <div class="title-container">
        <h3 class="title">{{ title }}</h3>
      </div>
      <el-form-item prop="username">
        <el-input 
          v-model="registerForm.username" 
          type="text" 
          size="large" 
          auto-complete="off" 
          placeholder="账号"
          class="register-input"
        >
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          size="large" 
          auto-complete="off"
          placeholder="密码"
          class="register-input"
          show-password
        >
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input
          v-model="registerForm.confirmPassword"
          type="password"
          size="large" 
          auto-complete="off"
          placeholder="确认密码"
          class="register-input"
          show-password
          @keyup.enter="handleRegister"
        >
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <div class="captcha-container">
          <el-input
            size="large" 
            v-model="registerForm.code"
            auto-complete="off"
            placeholder="验证码"
            class="register-input captcha-input"
            @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>
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large" 
          type="primary"
          class="register-btn"
          @click.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
      </el-form-item>
      <div class="form-footer">
        <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
      </div>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>{{ footerContent }}</span>
    </div>
  </div>
</template>

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

const title = import.meta.env.VITE_APP_TITLE
const footerContent = defaultSettings.footerContent
const router = useRouter()
const { proxy } = getCurrentInstance()

const registerForm = ref({
  username: "",
  password: "",
  confirmPassword: "",
  code: "",
  uuid: ""
})

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" }
  ],
  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 color='red'>恭喜你，您的账号 " + 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>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  position: relative;
  overflow: hidden;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  background-position: center;
}

/* 注册卡片 - 与登录页面统一的深色玻璃风格 */
.register-form {
  position: relative;
  border-radius: 12px;
  background: rgba(13, 22, 45, 0.7);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(59, 130, 246, 0.2);
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.6), 0 0 20px rgba(37, 99, 235, 0.1);
  width: 440px;
  padding: 45px 40px;
  z-index: 1;
}

.title-container {
  text-align: center;
  margin-bottom: 35px;
  
  .title {
    margin: 0;
    color: #fff;
    font-size: 28px;
    font-weight: 700;
    letter-spacing: 2px;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
  }
}

/* 输入框样式 - 与登录页面统一 */
:deep(.el-input) {
  --el-input-bg-color: rgba(0, 0, 0, 0.3);
  --el-input-border-color: rgba(59, 130, 246, 0.2);
  --el-input-hover-border-color: rgba(59, 130, 246, 0.5);
  --el-input-focus-border-color: #3b82f6;
  --el-input-text-color: #fff;
  --el-input-placeholder-color: rgba(255, 255, 255, 0.25);
  
  .el-input__wrapper {
    background-color: var(--el-input-bg-color);
    box-shadow: none !important;
    border: 1px solid var(--el-input-border-color);
    border-radius: 6px;
    padding: 1px 15px;
    transition: all 0.3s;
    height: 44px;
    
    &:hover {
      border-color: var(--el-input-hover-border-color);
      background-color: rgba(0, 0, 0, 0.4);
    }
    
    &.is-focus {
      background-color: rgba(0, 0, 0, 0.5);
      border-color: var(--el-input-focus-border-color);
      box-shadow: 0 0 15px rgba(59, 130, 246, 0.15) !important;
    }
  }
  
  .el-input__inner {
    height: 44px;
    font-size: 15px;
  }
  
  .input-icon {
    color: rgba(59, 130, 246, 0.7);
    font-size: 16px;
  }
}

/* 验证码布局 */
.captcha-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  
  .captcha-input {
    flex: 1;
    margin-right: 15px;
  }
  
  .register-code {
    width: 120px;
    height: 44px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 6px;
    overflow: hidden;
    cursor: pointer;
    border: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    
    .register-code-img {
      width: 100%;
      height: 100%;
      object-fit: fill;
    }
  }
}

/* 注册按钮 */
.register-btn {
  width: 100%;
  height: 48px;
  background: #3b82f6;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 6px;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(59, 130, 246, 0.4);
  
  &:hover {
    background: #2563eb;
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(59, 130, 246, 0.5);
  }
  
  &:active {
    transform: scale(0.98);
  }
}

/* 底部链接 */
.form-footer {
  text-align: center;
  margin-top: 15px;
  
  .link-type {
    color: #60a5fa;
    font-size: 14px;
    text-decoration: none;
    transition: all 0.3s;
    
    &:hover {
      color: #93c5fd;
      text-shadow: 0 0 10px rgba(59, 130, 246, 0.5);
    }
  }
}

/* 底部版权 */
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.3);
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 1;
}
</style>
