<template>
  <div class="login-page">
    <div class="login-box card-shadow">
      <div class="login-header">
        <el-icon size="48" color="#1a2b4a"><Collection /></el-icon>
        <h1 class="login-title">DataPointer</h1>
        <p class="login-subtitle">财险数据分级分类管理平台</p>
      </div>

      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        size="large"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="用户名"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-btn"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <!-- 生产环境不显示默认账户 -->
      </div>
    </div>
  </div>
</template>

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

const router = useRouter()
const userStore = useUserStore()
const loading = ref(false)
const formRef = ref()

const form = reactive({
  username: 'admin',
  password: '',
})

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

async function handleLogin() {
  const valid = await formRef.value?.validate().catch(() => false)
  if (!valid) return

  loading.value = true
  try {
    await userStore.login(form.username, form.password)
    ElMessage.success('登录成功')
    await router.push('/')
  } catch (e: any) {
    if (!e?.handled) {
      ElMessage.error(e?.message || '登录失败')
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1a2b4a 0%, #2d4a7c 100%);
  padding: 16px;
}

.login-box {
  width: 100%;
  max-width: 420px;
  padding: 40px 32px;
  background: #fff;
  border-radius: 12px;
}

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

  .login-title {
    font-size: 24px;
    font-weight: 700;
    color: #1a2b4a;
    margin-top: 12px;
  }

  .login-subtitle {
    font-size: 14px;
    color: #909399;
    margin-top: 8px;
  }
}

.login-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  border-radius: 6px;
}

.login-footer {
  margin-top: 24px;
  text-align: center;
  font-size: 12px;
  color: #c0c4cc;
}
</style>
