<script setup lang="ts">
/**
 * 用户端登录页
 *
 * 流程：
 *   1. 校验表单 → 调 userStore.login()（请求 POST /api/user/login）
 *   2. 后端返回 LoginResp（token + userId + nickname），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: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 50, message: '用户名长度 3-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
      })
      ElMessage.success('登录成功')
      const redirect = (route.query.redirect as string) || '/'
      await router.push(redirect)
    } catch {
      /* 拦截器已提示错误 */
    } 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 class="blob blob-3" />
    </div>

    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 左侧品牌面板 -->
      <div class="login-left">
        <div class="brand">
          <div class="brand-icon">🍪</div>
          <div class="brand-text">
            <h1>零食商城</h1>
            <p>Snack Mall</p>
          </div>
        </div>

        <div class="welcome">
          <h2>欢迎回来！</h2>
          <p>精选美味零食，品质保证，快速送达</p>
        </div>

        <ul class="features">
          <li>
            <span class="feature-icon">🔥</span>
            每日精选 · 热门零食推荐
          </li>
          <li>
            <span class="feature-icon">⚡</span>
            限时抢购 · 超值优惠不停
          </li>
          <li>
            <span class="feature-icon">📦</span>
            快速配送 · 新鲜直达您手中
          </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>
            <el-button
              type="primary"
              :loading="loading"
              class="submit-btn"
              @click="handleLogin"
            >
              登 录
            </el-button>
          </el-form-item>
        </el-form>

        <div class="tips">
          <span>演示账号：user001 / 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, #FFF7F0 0%, #FFF0EA 40%, #FFE4D6 100%);
  position: relative;
  overflow: hidden;
}

// 背景装饰光斑
.login-bg {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;

  .blob {
    position: absolute;
    border-radius: 50%;
    filter: blur(100px);
    opacity: 0.35;
  }

  .blob-1 {
    width: 500px;
    height: 500px;
    background: $color-primary;
    top: -120px;
    left: -100px;
  }

  .blob-2 {
    width: 380px;
    height: 380px;
    background: $color-secondary;
    bottom: -80px;
    right: -60px;
  }

  .blob-3 {
    width: 280px;
    height: 280px;
    background: $color-cta;
    top: 50%;
    right: 15%;
  }
}

// 卡片容器
.login-card {
  position: relative;
  z-index: 1;
  display: flex;
  width: 920px;
  max-width: 95vw;
  min-height: 540px;
  background: $bg-surface;
  border: 2px solid $color-border;
  border-radius: $radius-xl;
  overflow: hidden;
  box-shadow: $clay-shadow-lg;
}

// 左侧品牌面板
.login-left {
  flex: 1;
  padding: 48px 40px;
  background: linear-gradient(160deg, $color-primary 0%, $color-cta 100%);
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

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

  .brand-icon {
    width: 52px;
    height: 52px;
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.18);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 26px;
  }

  .brand-text {
    h1 {
      margin: 0;
      font-size: 22px;
      font-weight: 600;
      font-family: 'Fredoka', 'PingFang SC', sans-serif;
    }
    p {
      margin: 2px 0 0;
      font-size: 13px;
      opacity: 0.75;
    }
  }
}

.welcome {
  margin-top: 16px;

  h2 {
    font-size: 30px;
    font-weight: 700;
    margin: 0 0 10px;
    font-family: 'Fredoka', 'PingFang SC', sans-serif;
  }
  p {
    margin: 0;
    font-size: 15px;
    opacity: 0.9;
    line-height: 1.5;
  }
}

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

  li {
    display: flex;
    align-items: center;
    gap: 10px;
  }

  .feature-icon {
    font-size: 18px;
  }
}

// 右侧表单
.login-right {
  flex: 1;
  padding: 56px 56px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.form-title {
  font-size: 26px;
  font-weight: 600;
  margin: 0 0 4px;
  color: $color-text-primary;
  font-family: 'Fredoka', 'PingFang SC', sans-serif;
}

.form-subtitle {
  font-size: $font-size-md;
  color: $color-text-secondary;
  margin: 0 0 36px;
}

.login-form {
  :deep(.el-input__wrapper) {
    padding: 4px 14px;
    border-radius: $radius-sm;
    border: 2px solid $color-border;
    box-shadow: none;
    transition: border-color 0.2s, box-shadow 0.2s;

    &:hover {
      border-color: $color-border-dark;
    }

    &.is-focus {
      border-color: $color-primary;
      box-shadow: $clay-shadow-focus;
    }
  }
}

.submit-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 2px;
  border: 2px solid $color-border-dark;
  border-radius: $radius-full;
  background: linear-gradient(135deg, $color-primary, $color-cta);
  box-shadow: 0 4px 0 0 $color-primary-dark;
  transition: transform 0.2s ease, box-shadow 0.2s ease, filter 0.2s ease;

  &:hover {
    filter: brightness(1.04);
    transform: translateY(-1px);
  }

  &:active {
    box-shadow: 0 1px 0 0 $color-primary-dark;
    transform: translateY(3px);
  }
}

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

// 响应式
@media (max-width: 768px) {
  .login-card {
    width: 95vw;
    min-height: auto;
    flex-direction: column;
  }

  .login-left {
    display: none;
  }

  .login-right {
    padding: 40px 28px;
  }
}
</style>
