<script setup lang="ts">
import { ref, shallowRef } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Lock } from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/useAuthStore'
import { useUserSettingsStore } from '@/stores/useUserSettingsStore'

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

const password = ref('')
const loading = ref(false)
const error = ref('')

const redirect = (route.query.redirect as string) || '/'

async function handleLogin() {
  if (!password.value) return
  loading.value = true
  error.value = ''
  try {
    const ok = await authStore.login(password.value)
    if (ok) {
      await userSettingsStore.fetchAndSync()
      router.replace(redirect)
    } else {
      error.value = authStore.error || '密码错误'
    }
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-wrapper">
    <div class="decoration-star" style="top: 10%; right: 15%; animation-delay: 0s;"></div>
    <div class="decoration-star" style="top: 70%; left: 10%; animation-delay: 1s;"></div>
    <div class="decoration-star" style="top: 30%; left: 20%; animation-delay: 2s;"></div>

    <div class="login-card">
      <div class="login-header">
        <div class="logo-icon">✿</div>
        <h1 class="site-name">{{ userSettingsStore.siteName }}</h1>
        <p class="subtitle">需要密码才能进入哦~</p>
      </div>

      <el-form @submit.prevent="handleLogin" class="login-form">
        <el-input
          v-model="password"
          type="password"
          placeholder="请输入密码"
          size="large"
          show-password
          :prefix-icon="Lock"
          @keyup.enter="handleLogin"
        />
        <p v-if="error" class="error-msg">{{ error }}</p>
        <el-button
          type="primary"
          size="large"
          :loading="loading"
          class="login-btn"
          @click="handleLogin"
        >
          进入
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #fce4ec 0%, #f8bbd0 30%, #f48fb1 60%, #fce4ec 100%);
  position: relative;
  overflow: hidden;
}

.login-card {
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(16px);
  border-radius: 24px;
  padding: 48px 40px;
  width: 400px;
  max-width: 90vw;
  box-shadow: 0 8px 32px rgba(255, 183, 197, 0.3);
  z-index: 1;
}

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

  .logo-icon {
    font-size: 48px;
    color: var(--primary);
    animation: twinkle 2s ease-in-out infinite;
    margin-bottom: 12px;
  }

  .site-name {
    font-size: 24px;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin: 0 0 8px;
  }

  .subtitle {
    font-size: 14px;
    color: var(--text-secondary);
    margin: 0;
  }
}

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

.error-msg {
  color: #f56c6c;
  font-size: 13px;
  margin: 0;
  text-align: center;
}

.login-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
  border: none;
  border-radius: 12px;

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(255, 183, 197, 0.5);
  }
}

@keyframes twinkle {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.7; transform: scale(1.05); }
}
</style>
