<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <h2>Welcome Back</h2>
        <p>Sign in to manage your AI characters</p>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <div class="form-label">Email Address</div>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="admin@example.com">
          </el-input>
        </el-form-item>
        <div class="form-label">Password</div>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="********">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" class="login-button">Login</el-button>
        </el-form-item>
        <div class="register-link">
          Don't have an account? <a href="#">Register</a>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'
import { useStore } from '../store'

export default {
  name: 'LoginPage',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: 'Please enter your email', trigger: 'blur' }
        ],
        password: [
          { required: true, message: 'Please enter your password', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          const store = useStore()
          const { username, password } = this.loginForm
          if (store.login(username, password)) {
            ElMessage.success('Login successful')
            this.$router.push('/')
          } else {
            ElMessage.error('Invalid email or password')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 400px;
  border-radius: 8px;
}

.login-header {
  text-align: center;
  margin-bottom: 20px;
}

.login-header h2 {
  font-size: 24px;
  margin-bottom: 8px;
}

.login-header p {
  color: #606266;
  font-size: 14px;
  margin: 0;
}

.form-label {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.login-button {
  width: 100%;
  height: 40px;
  border-radius: 4px;
  background-color: #ff6b00;
  border-color: #ff6b00;
  margin-top: 10px;
}

.login-button:hover {
  background-color: #e66000;
  border-color: #e66000;
}

.register-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
}

.register-link a {
  color: #ff6b00;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}

:deep(.el-input__inner) {
  height: 40px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}
</style>