<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

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

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

async function handleLogin() {
  if (!username.value || !password.value) {
    errorMsg.value = '请输入用户名和密码'
    return
  }
  loading.value = true
  errorMsg.value = ''
  try {
    await authStore.login(username.value, password.value)
    router.push('/dashboard')
  } catch (e: any) {
    console.error('登录失败:', e)
    errorMsg.value =
      e?.response?.data?.message || e?.message || '登录失败，请检查用户名和密码'
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-page">
    <div class="login-card">
      <div class="login-header">
        <div class="brand-icon">G</div>
        <h2>GymManage 后台管理</h2>
        <p>请输入您的账号信息登录</p>
      </div>
      <form class="login-form" @submit.prevent="handleLogin">
        <div class="form-group">
          <label>用户名</label>
          <div class="input-wrapper">
            <i class="fas fa-user"></i>
            <input
              v-model="username"
              type="text"
              placeholder="请输入用户名"
              autocomplete="username"
              :disabled="loading"
            />
          </div>
        </div>
        <div class="form-group">
          <label>密码</label>
          <div class="input-wrapper">
            <i class="fas fa-lock"></i>
            <input
              v-model="password"
              type="password"
              placeholder="请输入密码"
              autocomplete="current-password"
              :disabled="loading"
            />
          </div>
        </div>
        <transition name="fade">
          <p v-if="errorMsg" class="error-msg">{{ errorMsg }}</p>
        </transition>
        <button class="btn-login" :class="{ 'is-loading': loading }" :disabled="loading" type="submit">
          <span class="btn-spinner" v-if="loading">
            <i class="fas fa-circle-notch fa-spin"></i>
          </span>
          <span class="btn-text">{{ loading ? '登录中...' : '登录' }}</span>
        </button>
      </form>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f6f8fc 0%, #eef2f8 100%);
}

.login-card {
  background: white;
  padding: 48px 40px;
  border-radius: 24px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  border: 1px solid #edf2f9;
  width: 400px;
  max-width: 90vw;
  animation: scaleIn 0.35s ease;
}

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

.brand-icon {
  width: 48px;
  height: 48px;
  background: linear-gradient(145deg, #4f7cff, #3b5fd9);
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  font-weight: 700;
  box-shadow: 0 8px 16px rgba(79, 124, 255, 0.25);
  margin-bottom: 16px;
}

.login-header h2 {
  font-size: 22px;
  font-weight: 600;
  color: #0b1a33;
  margin-bottom: 6px;
}

.login-header p {
  font-size: 14px;
  color: #6b7d94;
}

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

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 6px;
}

.input-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #f8fafd;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 10px 14px;
  transition: 0.15s;
}

.input-wrapper:focus-within {
  border-color: #4f7cff;
  box-shadow: 0 0 0 3px rgba(79, 124, 255, 0.12);
}

.input-wrapper i {
  color: #8b9ab0;
  font-size: 14px;
  width: 16px;
}

.input-wrapper input {
  border: none;
  outline: none;
  background: transparent;
  flex: 1;
  font-size: 14px;
  color: #1e293b;
}

.input-wrapper input:disabled {
  opacity: 0.5;
}

.error-msg {
  color: #ef4444;
  font-size: 13px;
  text-align: center;
  background: #fef2f2;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid #fecaca;
}

.btn-login {
  padding: 12px;
  border-radius: 12px;
  border: none;
  background: #4f7cff;
  color: white;
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 4px 12px rgba(79, 124, 255, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  overflow: hidden;
}

.btn-login:hover:not(:disabled) {
  background: #3f6ae0;
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(79, 124, 255, 0.35);
}

.btn-login:active:not(:disabled) {
  transform: translateY(0);
}

.btn-login:disabled {
  opacity: 0.8;
  cursor: not-allowed;
}

.btn-login.is-loading {
  background: #4f7cff;
}

.btn-spinner {
  display: flex;
  align-items: center;
  font-size: 16px;
}

/* ---- animations ---- */
@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(10px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.25s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>