<template>
  <div class="auth-page">
    <div class="auth-card">
      <div class="tabs">
        <button @click="mode = 'login'" :class="{ active: mode === 'login' }">登录</button>
        <button @click="mode = 'register'" :class="{ active: mode === 'register' }">注册</button>
      </div>

      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <label>用户名</label>
          <input v-model="form.username" type="text" placeholder="请输入用户名" required />
        </div>
        
        <div v-if="mode === 'register'" class="form-group">
          <label>邮箱</label>
          <input v-model="form.email" type="email" placeholder="请输入邮箱" required />
        </div>
        
        <div class="form-group">
          <label>密码</label>
          <input v-model="form.password" type="password" placeholder="请输入密码" required />
        </div>
        
        <div v-if="mode === 'register'" class="form-group">
          <label>确认密码</label>
          <input v-model="confirmPassword" type="password" placeholder="请再次输入密码" required />
        </div>

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

        <button type="submit" :disabled="loading" class="btn-submit">
          <span v-if="loading" class="spinner"></span>
          <span v-else>{{ mode === 'login' ? '登录' : '注册' }}</span>
        </button>
      </form>

      <p class="switch-mode">
        {{ mode === 'login' ? '没有账户？' : '已有账户？' }}
        <button @click="mode = mode === 'login' ? 'register' : 'login'">
          {{ mode === 'login' ? '立即注册' : '立即登录' }}
        </button>
      </p>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { authAPI } from '../utils/api.js'
import { useAuth } from '../utils/useAuth.js'

const router = useRouter()
const { login } = useAuth()

const mode = ref('login')
const loading = ref(false)
const error = ref('')
const confirmPassword = ref('')
const form = reactive({ username: '', email: '', password: '' })

const handleSubmit = async () => {
  error.value = ''
  loading.value = true
  
  try {
    if (mode.value === 'register' && form.password !== confirmPassword.value) {
      throw new Error('两次密码不一致')
    }
    
    const api = mode.value === 'login' ? authAPI.login : authAPI.register
    const response = await api(mode.value === 'login' 
      ? { username: form.username, password: form.password }
      : form
    )
    
    if (response.success) {
      if (mode.value === 'login' && response.data?.access_token) {
        login(response.data.access_token, response.data.user)
        router.push('/')
      } else {
        mode.value = 'login'
        form.username = form.password = form.email = ''
        confirmPassword.value = ''
      }
    } else {
      throw new Error(response.message || '操作失败')
    }
  } catch (err) {
    error.value = err.message || '操作失败，请重试'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.auth-page { min-height: calc(100vh - 70px); display: flex; align-items: center; justify-content: center; padding: 2rem; }
.auth-card { width: 100%; max-width: 400px; }
.tabs { display: flex; margin-bottom: 2rem; background: var(--code-bg); border-radius: 12px; padding: 0.25rem; }
.tabs button { flex: 1; padding: 0.75rem; background: transparent; border: none; border-radius: 8px; font-size: 1rem; font-weight: 500; color: var(--text); cursor: pointer; transition: all 0.2s; }
.tabs button.active { background: var(--bg); color: var(--text-h); box-shadow: 0 2px 8px var(--shadow); }
.form-group { margin-bottom: 1.25rem; }
.form-group label { display: block; margin-bottom: 0.5rem; font-weight: 500; color: var(--text-h); }
.form-group input { width: 100%; padding: 0.875rem 1rem; border: 1px solid var(--border); border-radius: 10px; font-size: 1rem; background: var(--bg); color: var(--text); box-sizing: border-box; }
.form-group input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }
.error-msg { padding: 0.75rem 1rem; background: #fef2f2; border: 1px solid #fecaca; border-radius: 8px; color: #dc2626; font-size: 0.9rem; margin-bottom: 1rem; }
.btn-submit { width: 100%; padding: 1rem; background: var(--accent); color: white; border: none; border-radius: 10px; font-size: 1.1rem; font-weight: 600; cursor: pointer; transition: all 0.2s; display: flex; align-items: center; justify-content: center; }
.btn-submit:hover:not(:disabled) { background: var(--accent-border); }
.btn-submit:disabled { opacity: 0.7; cursor: not-allowed; }
.spinner { width: 20px; height: 20px; border: 2px solid rgba(255,255,255,0.3); border-top-color: white; border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.switch-mode { text-align: center; color: var(--text); margin-top: 1.5rem; }
.switch-mode button { background: none; border: none; color: var(--accent); cursor: pointer; font-weight: 500; text-decoration: underline; }
</style>
