<template>
  <div class="login-container">
    <!-- Animated Background -->
    <div class="bg-shapes">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
      <div class="shape shape-4"></div>
    </div>

    <div class="login-card">
      <div class="login-header">
        <div class="logo-icon">
          <CompassOutlined />
        </div>
        <h2>比赛管理系统</h2>
        <p>请输入您的账户信息</p>
      </div>

      <a-form
        :model="form"
        :rules="rules"
        @finish="handleSubmit"
        class="login-form"
        layout="vertical"
      >
        <a-form-item name="tenantCode" v-if="showTenantInput" label="租户编码">
          <a-input
            v-model:value="form.tenantCode"
            size="large"
            placeholder="请输入租户编码（可选）"
            class="custom-input"
          >
            <template #prefix>
              <GlobalOutlined class="input-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item name="username" label="用户名">
          <a-input
            v-model:value="form.username"
            size="large"
            placeholder="请输入用户名"
            class="custom-input"
          >
            <template #prefix>
              <UserOutlined class="input-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item name="password" label="密码">
          <a-input-password
            v-model:value="form.password"
            size="large"
            placeholder="请输入密码"
            class="custom-input"
          >
            <template #prefix>
              <LockOutlined class="input-icon" />
            </template>
          </a-input-password>
        </a-form-item>

        <div class="form-options">
          <a-checkbox v-model:checked="rememberMe">记住我</a-checkbox>
          <a class="forgot-link">忘记密码？</a>
        </div>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            size="large"
            :loading="loading"
            block
            class="login-btn"
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from "vue"
import { useRouter, useRoute } from "vue-router"
import {
  UserOutlined,
  LockOutlined,
  GlobalOutlined,
  CompassOutlined
} from "@ant-design/icons-vue"
import { useAuthStore } from "@/stores/auth"
import type { Rule } from "ant-design-vue/es/form"

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

const loading = ref(false)
const rememberMe = ref(false)

const form = ref({
  username: "",
  password: "",
  tenantCode: "",
})

onMounted(() => {
  const pathMatch = route.path.match(/^\/([^/]+)\/login$/)
  const tenantCodeFromPath = pathMatch ? pathMatch[1] : null

  if (tenantCodeFromPath) {
    form.value.tenantCode = tenantCodeFromPath
  }
})

const showTenantInput = computed(() => {
  return !form.value.tenantCode
})

const rules: Record<string, Rule[]> = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
}

const handleSubmit = async () => {
  loading.value = true
  try {
    const loginData: any = {
      username: form.value.username,
      password: form.value.password,
    }

    if (form.value.tenantCode) {
      loginData.tenantCode = form.value.tenantCode
    }

    await authStore.login(loginData)

    const userTenantCode = authStore.user?.tenantCode || form.value.tenantCode

    if (!userTenantCode) {
      console.error("登录失败：无法获取租户编码")
      return
    }

    let redirectPath = route.query.redirect as string

    if (!redirectPath) {
      redirectPath = `/${userTenantCode}`
    } else {
      const redirectTenantCode = redirectPath.match(/^\/([^/]+)/)?.[1]

      if (redirectTenantCode && redirectTenantCode !== userTenantCode) {
        redirectPath = redirectPath.replace(
          `/${redirectTenantCode}`,
          `/${userTenantCode}`
        )
      } else if (!redirectTenantCode) {
        const cleanPath = redirectPath.startsWith("/")
          ? redirectPath.slice(1)
          : redirectPath
        redirectPath = `/${userTenantCode}/${cleanPath}`
      }
    }

    router.push(redirectPath)
  } catch (error) {
    console.error("Login failed:", error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
$ocean-blue: #0891b2;
$ocean-light: #06b6d4;
$sunset-orange: #f97316;
$sunset-pink: #ec4899;
$forest-green: #10b981;
$aurora-purple: #8b5cf6;

.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%);
  position: relative;
  overflow: hidden;
  padding: 20px;
}

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

  .shape {
    position: absolute;
    border-radius: 50%;
    filter: blur(80px);
    opacity: 0.4;
    animation: float 20s ease-in-out infinite;
  }

  .shape-1 {
    width: 600px;
    height: 600px;
    background: linear-gradient(135deg, $ocean-blue, $ocean-light);
    top: -200px;
    left: -200px;
    animation-delay: 0s;
  }

  .shape-2 {
    width: 500px;
    height: 500px;
    background: linear-gradient(135deg, $sunset-orange, $sunset-pink);
    bottom: -150px;
    right: -150px;
    animation-delay: -5s;
  }

  .shape-3 {
    width: 400px;
    height: 400px;
    background: linear-gradient(135deg, $forest-green, $ocean-light);
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    animation-delay: -10s;
  }

  .shape-4 {
    width: 300px;
    height: 300px;
    background: linear-gradient(135deg, $aurora-purple, $sunset-pink);
    top: 20%;
    right: 20%;
    animation-delay: -15s;
  }
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0) scale(1);
  }
  25% {
    transform: translate(30px, -30px) scale(1.05);
  }
  50% {
    transform: translate(-20px, 20px) scale(0.95);
  }
  75% {
    transform: translate(-30px, -20px) scale(1.02);
  }
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 48px 40px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
  position: relative;
  z-index: 1;
}

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

  .logo-icon {
    width: 56px;
    height: 56px;
    background: linear-gradient(135deg, $ocean-blue 0%, $aurora-purple 100%);
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    color: white;
    margin: 0 auto 20px;
    box-shadow: 0 8px 24px rgba($ocean-blue, 0.35);
  }

  h2 {
    font-size: 26px;
    font-weight: 700;
    color: #1e293b;
    margin: 0 0 8px 0;
  }

  p {
    color: #64748b;
    margin: 0;
    font-size: 15px;
  }
}

.login-form {
  .custom-input {
    height: 48px;
    border-radius: 10px;
    border: 2px solid #e2e8f0;
    transition: all 0.3s ease;

    &:hover {
      border-color: $ocean-blue;
    }

    &:focus-within {
      border-color: $ocean-blue;
      box-shadow: 0 0 0 3px rgba($ocean-blue, 0.1);
    }

    :deep(.ant-input) {
      font-size: 15px;
    }

    :deep(.ant-input-password-icon) {
      color: #94a3b8;
    }
  }

  .input-icon {
    color: #94a3b8;
    font-size: 16px;
  }

  :deep(.ant-form-item-label > label) {
    color: #475569;
    font-weight: 500;
    font-size: 14px;
  }
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;

  :deep(.ant-checkbox-wrapper) {
    color: #64748b;
    font-size: 14px;
  }

  :deep(.ant-checkbox-checked .ant-checkbox-inner) {
    background-color: $ocean-blue;
    border-color: $ocean-blue;
  }

  .forgot-link {
    color: $ocean-blue;
    font-size: 14px;
    cursor: pointer;
    transition: color 0.3s ease;

    &:hover {
      color: $aurora-purple;
    }
  }
}

.login-btn {
  height: 48px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, $ocean-blue 0%, $aurora-purple 100%);
  border: none;
  box-shadow: 0 8px 24px rgba($ocean-blue, 0.35);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 32px rgba($ocean-blue, 0.45);
    background: linear-gradient(135deg, $ocean-light 0%, $aurora-purple 100%);
  }

  &:active {
    transform: translateY(0);
  }
}

@media (max-width: 480px) {
  .login-container {
    padding: 16px;
  }

  .login-card {
    padding: 32px 24px;
    border-radius: 16px;
  }

  .login-header h2 {
    font-size: 22px;
  }
}
</style>
