<template>
  <div class="login-container">
    <div class="login-card">
      <div class="card-header">
        <h2>欢迎回来</h2>
        <p>使用您的凭据继续</p>
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.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="loginForm.password" type="password" placeholder="请输入您的密码" show-password size="large" @keyup.enter="handleLogin">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <div class="button-group">
            <el-button type="primary" @click="handleLogin" class="login-button">安全登录</el-button>
            <el-button @click="goToRegister" class="register-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, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '../stores/user';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue';

const router = useRouter();
const userStore = useUserStore();
const loginFormRef = ref(null);

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

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
};

const handleLogin = async () => {
  const valid = await loginFormRef.value.validate();
  if (valid) {
    const success = await userStore.login(loginForm.value.username, loginForm.value.password);
    if (success) {
      ElMessage.success('登录成功');
      loginForm.value.password = '';
      router.push('/home');
    }
  }
};

const goToRegister = () => {
  router.push('/register');
};

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

</script>

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

.login-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 .login-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;
}

.login-button, .register-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;
}

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

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

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

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

.register-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>