<template>
  <div class="login-page">
    <div class="login-card">
      <!-- 品牌区域 -->
      <div class="brand-section">
        <h1 class="brand-name">玉 宗</h1>
        <p class="brand-en">YUZONG JEWELRY</p>
        <p class="brand-subtitle">珠宝设计大师</p>
      </div>

      <!-- 登录表单 -->
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <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"
            :prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-button"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 底部链接 -->
      <div class="footer-links">
        <span class="text">没有账号？</span>
        <router-link to="/register" class="link">立即注册</router-link>
      </div>
    </div>
  </div>
</template>

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

const router = useRouter()
const userStore = useUserStore()

const formRef = ref<FormInstance>()
const loading = ref(false)

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

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

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

  loading.value = true
  try {
    await userStore.login(form.username, form.password)
    ElMessage.success('登录成功')
    router.push('/design')
  } catch (error: any) {
    // 错误已在拦截器中处理
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
$primary-color: #5B7E6B;
$primary-dark: #3D5A4A;
$primary-lighter: #D4E5DB;

.login-page {
  min-height: calc(100vh - 64px);
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #FAF8F5 0%, #f0f4f1 40%, #e8efe9 100%);
  padding: 24px;
  position: relative;

  &::before {
    content: '';
    position: absolute;
    top: 10%;
    right: 10%;
    width: 300px;
    height: 300px;
    background: radial-gradient(circle, rgba($primary-color, 0.05) 0%, transparent 70%);
    border-radius: 50%;
    pointer-events: none;
  }
}

.login-card {
  width: 100%;
  max-width: 420px;
  padding: 48px 44px;
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.06);
  position: relative;
  z-index: 1;
}

.brand-section {
  text-align: center;
  margin-bottom: 40px;

  .brand-name {
    font-size: 36px;
    font-weight: 700;
    color: $primary-color;
    letter-spacing: 10px;
    margin: 0 0 4px 0;
  }

  .brand-en {
    font-size: 10px;
    color: #999;
    letter-spacing: 3px;
    margin: 0 0 12px 0;
    opacity: 0.7;
  }

  .brand-subtitle {
    font-size: 14px;
    color: #999;
    letter-spacing: 4px;
    margin: 0;
  }
}

.login-form {
  :deep(.el-input__wrapper) {
    border-radius: 10px;
    padding: 6px 14px;
    box-shadow: 0 0 0 1px #E8E4DF inset !important;
    transition: all 0.25s ease;

    &:hover, &.is-focus {
      box-shadow: 0 0 0 1px $primary-color inset !important;
    }
  }

  :deep(.el-input__prefix) {
    color: #bbb;
  }

  :deep(.el-form-item) {
    margin-bottom: 24px;
  }
}

.login-button {
  width: 100%;
  height: 50px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, $primary-color, $primary-dark);
  border-color: $primary-color;
  letter-spacing: 3px;

  &:hover,
  &:focus {
    background: linear-gradient(135deg, $primary-dark, #2d4638);
    border-color: $primary-dark;
  }
}

.footer-links {
  text-align: center;
  margin-top: 28px;
  font-size: 14px;

  .text {
    color: #999;
  }

  .link {
    color: $primary-color;
    text-decoration: none;
    margin-left: 4px;
    font-weight: 600;

    &:hover {
      color: $primary-dark;
      text-decoration: underline;
    }
  }
}
</style>
