<script setup lang="ts">
/**
 * 管理员登录页
 *
 * 流程：
 *   1. 校验表单 → 调 userStore.login()（请求 POST /api/admin/login）
 *   2. 后端返回 LoginVO（token + adminInfo），store 自动写入
 *   3. 跳到 redirect 参数指定的页面（默认 /）
 *
 * Token 失效由 request.ts 拦截器统一处理（401 → 清登录态 + 跳本页）
 */
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/modules/user'
import type { LoginParams } from '@/types/auth'

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

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

const form = reactive<LoginParams>({
  username: 'admin',
  password: '123456',
  rememberMe: false
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 50, message: '用户名长度 2-50', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 50, message: '密码长度 6-50', trigger: 'blur' }
  ]
}

async function handleLogin() {
  if (!formRef.value) return
  await formRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    loading.value = true
    try {
      await userStore.login({
        username: form.username,
        password: form.password,
        rememberMe: form.rememberMe
      })
      ElMessage.success('登录成功')
      const redirect = (route.query.redirect as string) || '/'
      await router.push(redirect)
    } catch {
      /* 拦截器已提示错误，组件内不再 toast */
    } finally {
      loading.value = false
    }
  })
}
</script>

<template>
  <div class="login-page">
    <div class="login-bg">
      <div class="blob blob-1" />
      <div class="blob blob-2" />
    </div>

    <div class="login-card">
      <div class="login-left">
        <div class="brand">
          <div class="brand-logo">S</div>
          <div class="brand-text">
            <h1>零食商城</h1>
            <p>Snack Mall Admin Console</p>
          </div>
        </div>
        <div class="welcome">
          <h2>欢迎回来 👋</h2>
          <p>登录管理后台，开启高效运营之旅</p>
        </div>
        <ul class="features">
          <li>📊 实时数据可视化</li>
          <li>🛒 完整商品/订单管理</li>
          <li>🎯 营销活动精准运营</li>
        </ul>
      </div>

      <div class="login-right">
        <h2 class="form-title">账号登录</h2>
        <p class="form-subtitle">请使用您的管理员账号登录</p>

        <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          size="large"
          class="login-form"
          @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>
            <div class="form-extra">
              <el-checkbox v-model="form.rememberMe">记住我</el-checkbox>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              class="submit-btn"
              @click="handleLogin"
            >
              登 录
            </el-button>
          </el-form-item>
        </el-form>

        <div class="tips">
          <span>默认账号：admin / 123456</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-page {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 50%, #bfdbfe 100%);
  position: relative;
  overflow: hidden;
}

.login-bg {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;

  .blob {
    position: absolute;
    border-radius: 50%;
    filter: blur(80px);
    opacity: 0.4;
  }
  .blob-1 {
    width: 480px;
    height: 480px;
    background: #3b82f6;
    top: -120px;
    left: -120px;
  }
  .blob-2 {
    width: 360px;
    height: 360px;
    background: #1e40af;
    bottom: -100px;
    right: -80px;
  }
}

.login-card {
  position: relative;
  z-index: 1;
  display: flex;
  width: 920px;
  max-width: 95vw;
  min-height: 540px;
  background: #fff;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 24px 64px rgba(30, 64, 175, 0.12);
}

.login-left {
  flex: 1;
  padding: 48px 40px;
  background: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;

  .brand-logo {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    font-weight: 700;
  }

  .brand-text h1 {
    margin: 0;
    font-size: 20px;
    font-weight: 600;
  }
  .brand-text p {
    margin: 2px 0 0;
    font-size: 12px;
    opacity: 0.8;
  }
}

.welcome h2 {
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 8px;
}
.welcome p {
  margin: 0;
  font-size: 14px;
  opacity: 0.9;
}

.features {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 14px;
  opacity: 0.9;
}

.login-right {
  flex: 1;
  padding: 56px 56px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.form-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 4px;
  color: $color-text-primary;
}
.form-subtitle {
  font-size: 14px;
  color: $color-text-secondary;
  margin: 0 0 32px;
}

.login-form {
  :deep(.el-input__wrapper) {
    padding: 4px 12px;
    border-radius: 10px;
  }
}

.form-extra {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  margin-bottom: 4px;
}

.submit-btn {
  width: 100%;
  height: 44px;
  font-size: 15px;
  font-weight: 500;
  letter-spacing: 4px;
}

.tips {
  text-align: center;
  font-size: 12px;
  color: $color-text-secondary;
  margin-top: 16px;
}

@media (max-width: 768px) {
  .login-card {
    width: 95vw;
    min-height: auto;
  }
  .login-left {
    display: none;
  }
  .login-right {
    padding: 40px 28px;
  }
}
</style>
