<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>情绪博物馆管理后台</h2>
        <p>Emotion Museum Admin</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="account">
          <el-input
            v-model="loginForm.account"
            placeholder="请输入账号"
            size="large"
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useAdminStore } from '@/stores/admin'
import type { FormInstance, FormRules } from 'element-plus'
import { validateAccount, validatePassword } from '@/utils/validate'

const adminStore = useAdminStore()
const loginFormRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = reactive({
  account: '',
  password: ''
})

const loginRules: FormRules = {
  account: [{ required: true, validator: validateAccount, trigger: 'blur' }],
  password: [{ required: true, validator: validatePassword, trigger: 'blur' }]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await adminStore.login(loginForm)
      } catch (error) {
        console.error('登录失败:', error)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: var(--ls-bg);
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  inset: -20%;
  background:
    radial-gradient(700px 500px at 20% 30%, rgba(255, 171, 145, 0.18), transparent 55%),
    radial-gradient(650px 500px at 80% 20%, rgba(129, 212, 250, 0.14), transparent 55%),
    radial-gradient(800px 600px at 60% 80%, rgba(255, 171, 145, 0.10), transparent 55%);
  filter: blur(0px);
  pointer-events: none;
}

.login-box {
  width: 400px;
  padding: 40px;
  background: var(--ls-glass-bg);
  border: 1px solid var(--ls-glass-border);
  border-radius: var(--ls-radius-xl);
  box-shadow: var(--ls-shadow);
  backdrop-filter: blur(25px) saturate(180%);
  -webkit-backdrop-filter: blur(25px) saturate(180%);
  position: relative;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
  
  h2 {
    font-size: 24px;
    color: var(--ls-text);
    margin-bottom: 10px;
  }
  
  p {
    font-size: 14px;
    color: rgba(226, 232, 240, 0.7);
  }
}

.login-form {
  .login-button {
    width: 100%;
  }
}

.login-footer {
  text-align: center;
  margin-top: 20px;
  
  p {
    font-size: 12px;
    color: rgba(226, 232, 240, 0.6);
  }
}
</style>
