<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <span>登录</span>
        </div>
      </template>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
          <el-button @click="goToRegister">注册</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 { useUserStore } from '../stores/user';
import { ElMessage } from 'element-plus';

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('登录成功');
      router.push('/home');
    } else {
      ElMessage.error('用户名或密码错误');
    }
  }
};

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

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

<style scoped>
.login-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%);
}

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