<template>
  <div class="login-page">
    <!-- Ambient background -->
    <div class="bg-grid"></div>
    <div class="bg-glow"></div>

    <div class="login-container animate-in">
      <!-- Brand header -->
      <div class="brand">
        <div class="brand-icon">
          <svg width="36" height="36" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
            <path d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2"/>
            <rect x="9" y="3" width="6" height="4" rx="1"/>
            <path d="M9 14l2 2 4-4"/>
          </svg>
        </div>
        <h1>益选 OCR</h1>
        <p>采购单智能处理系统</p>
      </div>

      <!-- Login form -->
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        @submit.prevent="handleLogin"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="用户名"
            size="large"
            :prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="密码"
            size="large"
            show-password
            :prefix-icon="Lock"
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-button
          type="primary"
          size="large"
          :loading="loading"
          class="login-btn"
          @click="handleLogin"
        >
          {{ loading ? '登录中...' : '登 录' }}
        </el-button>
      </el-form>

      <div class="hint">
        <span class="hint-dot"></span>
        默认账号 admin / admin123
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useAuthStore } from '../stores/auth'

const router = useRouter()
const authStore = useAuthStore()

const formRef = ref()
const loading = ref(false)

const form = reactive({ username: '', password: '' })
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

async function handleLogin() {
  try { await formRef.value?.validate() } catch { return }
  loading.value = true
  try {
    await authStore.login(form.username, form.password)
    ElMessage.success('登录成功')
    router.push('/')
  } catch (err: any) {
    ElMessage.error(err.response?.data?.detail || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fafafa;
  position: relative;
}

.login-container {
  position: relative;
  width: 380px;
  padding: 40px 36px;
  background: #ffffff;
  border: 1px solid #e4e4e7;
  border-radius: 14px;
  box-shadow: 0 1px 3px 0 rgba(0,0,0,0.1), 0 1px 2px -1px rgba(0,0,0,0.1);
}

.brand {
  text-align: center;
  margin-bottom: 32px;
}

.brand-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: #f4f4f5;
  color: var(--text-primary);
  margin-bottom: 16px;
}

.brand h1 {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.5px;
}

.brand p {
  font-size: 14px;
  color: var(--text-muted);
  margin-top: 4px;
}

.login-form {
  margin-bottom: 24px;
}

.login-form :deep(.el-input__wrapper) {
  border: 1px solid #e4e4e7 !important;
  border-radius: 6px !important;
  box-shadow: none !important;
  background: #ffffff !important;
  transition: border-color 0.15s ease;
}

.login-form :deep(.el-input__wrapper:hover) {
  border-color: #a1a1aa !important;
}

.login-form :deep(.el-input__wrapper.is-focus) {
  border-color: #18181b !important;
  box-shadow: 0 0 0 1px #18181b !important;
}

.login-form :deep(.el-input__inner) {
  color: #18181b;
  font-size: 14px;
}

.login-form :deep(.el-input__inner::placeholder) {
  color: #a1a1aa;
}

.login-form :deep(.el-input__prefix .el-icon) {
  color: #a1a1aa;
}

.login-btn {
  width: 100%;
  height: 44px;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
  border-radius: 6px !important;
  background: #18181b !important;
  border: 1px solid #18181b !important;
  color: #fff !important;
  box-shadow: none !important;
  transition: all 0.15s ease;
}

.login-btn:hover {
  background: #27272a !important;
  border-color: #27272a !important;
}

.login-btn:active {
  background: #09090b !important;
}

.hint {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

.hint-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #d4d4d8;
}

/* ── Ambient background ── */
.bg-grid {
  position: absolute;
  inset: 0;
  background-size: 40px 40px;
  background-image:
    linear-gradient(to right, rgba(0,0,0,0.03) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(0,0,0,0.03) 1px, transparent 1px);
  mask-image: radial-gradient(circle at center, black 30%, transparent 70%);
  -webkit-mask-image: radial-gradient(circle at center, black 30%, transparent 70%);
  pointer-events: none;
}

.bg-glow {
  position: absolute;
  width: 600px;
  height: 600px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle, rgba(99,102,241,0.06) 0%, transparent 70%);
  pointer-events: none;
}
</style>
