<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <span>注册</span>
        </div>
      </template>
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister">注册</el-button>
          <el-button @click="goToLogin">返回登录</el-button>
          <el-button @click="goToHome">返回首页</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { register } from '../api/CommonApi';
import { ElMessage } from 'element-plus';

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

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

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' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  confirmPassword: [{ validator: validatePass, trigger: 'blur' }],
};

const handleRegister = async () => {
  const valid = await registerFormRef.value.validate();
  if (valid) {
    try {
      await register({ username: registerForm.value.username, password: registerForm.value.password });
      ElMessage.success('注册成功');
      router.push('/login');
    } catch (error) {
      ElMessage.error('注册失败，请稍后再试');
    }
  }
};

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

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

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: var(--bg-color-secondary);
  background-image: linear-gradient(135deg, var(--bg-color) 0%, var(--bg-color-secondary) 100%);
}

.register-card {
  width: 420px;
  padding: 20px 30px;
  border-radius: 12px;
  background-color: var(--bg-color);
  box-shadow: var(--box-shadow-dark);
  border: 1px solid var(--border-color);
  animation: slideInDown 0.5s ease-out;
  overflow: hidden;
}

.card-header {
  text-align: center;
  font-size: 24px;
  font-weight: 600;
  color: var(--text-color);
  padding-bottom: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--border-color-light);
}

.el-form-item {
  margin-bottom: 25px;
}

.el-button {
  width: 100%;
  transition: all var(--transition-duration) ease;
}

.el-button--primary {
    height: 40px;
}

.el-button--primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(64, 158, 255, 0.4);
}

.el-button:last-child {
  margin-left: 0;
  margin-top: 10px;
  color: var(--text-color-secondary);
  border: none;
  background: none;
}

.el-button:last-child:hover {
  color: var(--primary-color);
  background-color: var(--primary-color-light);
}
</style>