<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import Particles from '@/components/Particles/Particles.vue'
import GlareHover from '@/components/GlareHover/GlareHover.vue'
import BlurText from '@/components/BlurText/BlurText.vue'
import StarBorder from '@/components/StarBorder/StarBorder.vue'

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

const username = ref('')
const password = ref('')
const errorMsg = ref('')
const isLoading = ref(false)

const handleAnimationComplete = () => {
  console.log('All animations complete!');
};

const handleLogin = async () => {
  errorMsg.value = ''

  if (!username.value || !password.value) {
    errorMsg.value = '请输入用户名和密码'
    return
  }

  isLoading.value = true

  try {
    const success = await authStore.login({
      name: username.value,
      password: password.value
    })

    if (success) {
      router.push('/character')
    } else {
      errorMsg.value = '用户名或密码错误'
    }
  } catch (error: any) {
    errorMsg.value = error?.message || '登录失败，请稍后重试'
  } finally {
    isLoading.value = false
  }
}
</script>

<template>
  <div class="login-page">
    <Particles :particle-count="50" :particle-colors="['#ffffff', '#cccccc', '#aaaaaa']" class="particles-bg" />

    <div class="login-container">
      <div class="login-card">
        <div class="login-header">
          <BlurText text="Build Own God" animate-by="words" direction="top" :delay="200" class="blur-title"
            class-name="text-2xl font-semibold text-center" @animation-complete="handleAnimationComplete" />
          <p class="subtitle">从零开始·创造神明</p>
        </div>

        <form class="login-form" @submit.prevent="handleLogin">
          <div class="form-group">
            <label class="form-label">用户名</label>
            <input v-model="username" type="text" class="form-input" autocomplete="username"
              @keyup.enter="handleLogin" />
          </div>

          <div class="form-group">
            <label class="form-label">密码</label>
            <input v-model="password" type="password" class="form-input" autocomplete="current-password"
              @keyup.enter="handleLogin" />
          </div>

          <div v-if="errorMsg" class="error-message">
            {{ errorMsg }}
          </div>

          <StarBorder as="div" color="greenyellow" speed="3s" :thickness="3" @click="handleLogin">
            <div class="btn-login">
              <span v-if="isLoading" class="loading-text">登录中...</span>
              <span v-else class="button-text">登 录</span>
            </div>
          </StarBorder>

        </form>

        <div class="login-footer">
          <span class="footer-text">还没有账号？</span>
          <router-link to="/register" class="footer-link">
            立即注册
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000000;
  position: relative;
  overflow: hidden;
}

.particles-bg {
  position: fixed !important;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 100% !important;
}

.login-container {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 380px;
  padding: 20px;
}

.login-card {
  background: rgba(255, 255, 255, 0.03);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 20px;
  padding: 36px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.8);
}

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

.blur-title {
  justify-content: center;
  /* 水平居中 flex 项目 */
  text-align: center;
  /* 文字居中 */
  color: white;
  width: 100%;
  /* 确保占满宽度 */
}

.title-glow {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
}

.title-text {
  font-size: 1.75rem;
  font-weight: 300;
  letter-spacing: 0.2em;
  color: #ffffff;
  text-shadow: 0 0 20px rgba(255, 255, 255, 0.3);
}

.subtitle {
  color: #666666;
  font-size: 0.875rem;
  letter-spacing: 0.1em;
  margin-top: 8px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-label {
  color: #888888;
  font-size: 0.75rem;
  font-weight: 400;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.form-input {
  width: 100%;
  padding: 10px 16px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 15px;
  color: #ffffff;
  font-size: 0.95rem;
  transition: all 0.3s ease;
  outline: none;
}

.form-input::placeholder {
  color: #444444;
}

.form-input:focus {
  border-color: rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.05);
  box-shadow: 0 0 20px rgba(255, 255, 255, 0.05);
}

.error-message {
  color: #888888;
  font-size: 0.8rem;
  text-align: center;
  padding: 10px;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
}

.login-button {
  margin-top: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.btn-login {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px dashed greenyellow;
  border-radius: 15px;
  background: rgba(255, 255, 255, 0.03);
  padding: 12px 16px;
  cursor: pointer;
  height: 100%;
}

.button-text,
.loading-text {
  color: #ffffff;
  font-weight: bold;
  letter-spacing: 0.2em;
}

.login-footer {
  margin-top: 28px;
  text-align: center;
  display: flex;
  justify-content: center;
  gap: 8px;
}

.footer-text {
  color: #444444;
  font-size: 0.8rem;
}

.footer-link {
  color: #888888;
  font-size: 0.8rem;
  text-decoration: none;
  transition: color 0.3s ease;
}

.footer-link:hover {
  color: #ffffff;
}

/* Mobile responsive */
@media (max-width: 480px) {
  .login-card {
    padding: 28px 20px;
  }

  .title {
    font-size: 1.5rem;
  }

  .form-input {
    padding: 12px 14px;
    font-size: 16px;
  }
}
</style>
