<template>
  <div class="register-container">
    <div class="register-card">
      <div class="card-header">
        <h2>创建您的账户</h2>
        <p>加入我们，开启您的旅程</p>
      </div>
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="设置一个独特的用户名" size="large">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="创建一个安全的密码" show-password size="large">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" show-password size="large">
            <template #prefix>
              <el-icon><CircleCheck /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="注册码" prop="registrationCode">
          <el-input v-model="registerForm.registrationCode" placeholder="请输入注册码" size="large">
            <template #prefix>
              <el-icon><Key /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <div class="button-group">
            <el-button type="primary" @click="handleRegister" class="register-button">立即注册</el-button>
            <el-button @click="goToLogin" class="login-button">返回登录</el-button>
          </div>
        </el-form-item>
      </el-form>
      <div class="card-footer">
        <el-link @click="goToHome">返回首页</el-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { register } from '../api/CommonApi';
import { ElMessage } from 'element-plus';
import { User, Lock, CircleCheck, Key } from '@element-plus/icons-vue';

const router = useRouter();
const registerFormRef = ref(null);

const registerForm = ref({
  username: '',
  password: '',
  confirmPassword: '',
  registrationCode: '',
});

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

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 8, message: '用户名长度必须在2-8位之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 12, message: '密码长度必须在6-12位之间', trigger: 'blur' }
  ],
  confirmPassword: [{ validator: validatePass, trigger: 'blur' }],
  registrationCode: [{ required: true, message: '请输入注册码', trigger: 'blur' }],
};

const handleRegister = async () => {
  const valid = await registerFormRef.value.validate();
  if (valid) {
    try {
      await register(registerForm.value);
      ElMessage.success('注册成功');
      router.push('/login');
    } catch (error) {
      // 现在error.message就是后端返回的msg
      ElMessage.error(error.message || '注册失败，请稍后再试');
    }
  }
};

const goToLogin = () => {
  router.push('/login');
};

const goToHome = () => {
  router.push('/home');
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 2rem;
  background: var(--bg-gradient);
  background-attachment: fixed;
}

.register-card {
  width: 100%;
  max-width: 420px;
  padding: 2.5rem 2rem;
  border-radius: var(--border-radius);
  background-color: #ffffff;
  border: 1px solid #e4e7ed;
  animation: fadeIn 0.5s ease-out;
  overflow: hidden;
}

.dark-theme .register-card {
  background-color: #1e1e2f;
  border: 1px solid #2c2c3d;
}

.card-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.card-header h2 {
  margin: 0 0 0.5rem 0;
  font-size: 1.75rem;
  font-weight: 600;
  color: var(--text-color);
}

.card-header p {
  margin: 0;
  color: var(--text-color-secondary);
}

.el-form-item {
  margin-bottom: 1.5rem;
}

:deep(.el-form-item__label) {
  color: var(--text-color-secondary);
  line-height: 1.5;
  margin-bottom: 0.5rem;
}

:deep(.el-input__wrapper) {
  border-radius: var(--border-radius);
  background-color: var(--bg-color-secondary);
  box-shadow: none;
  transition: border-color var(--transition-duration) ease, box-shadow var(--transition-duration) ease;
  border: 1px solid var(--border-color);
}

:deep(.el-input__wrapper.is-focus) {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(var(--primary-color), 0.2);
}

.button-group {
  display: flex;
  width: 100%;
  gap: 1rem;
}

.register-button, .login-button {
  flex: 1;
  height: 44px;
  border-radius: var(--border-radius);
  font-size: 1rem;
  font-weight: 600;
  border: none;
  transition: background-color var(--transition-duration) ease, transform var(--transition-duration) ease;
}

.register-button {
  background-color: var(--primary-color);
}

.login-button {
  background-color: var(--secondary-color);
  color: var(--text-color);
  border: 1px solid var(--border-color);
}

.register-button:hover, .login-button:hover {
  transform: translateY(-2px);
  box-shadow: var(--box-shadow);
}

.register-button:hover {
  background-color: var(--primary-color-dark);
}

.login-button:hover {
  background-color: var(--border-color);
}

.card-footer {
  margin-top: 2rem;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  font-size: 0.875rem;
}

.link-highlight {
  color: var(--primary-color);
  font-weight: 600;
}

:deep(.el-link) {
  color: var(--text-color-secondary);
}

:deep(.el-link:hover) {
  color: var(--primary-color);
}
</style>
