<script setup lang="ts">
definePageMeta({ layout: false });

const auth = useAuth();
const router = useRouter();
const route = useRoute();
const authForm = ref<InstanceType<typeof AuthForm> | null>(null);
const loading = ref(false);

async function handleSubmit(data: { username: string; password: string; captchaToken: string; captchaCode: string }) {
  loading.value = true;
  try {
    const result = await auth.login(data.username, data.password, data.captchaToken, data.captchaCode);
    if (result.success) {
      const redirect = (route.query.redirect as string) || "/";
      router.push(redirect);
    } else {
      authForm.value?.setError(result.message || "登录失败");
      authForm.value?.refreshCaptcha();
    }
  } catch {
    authForm.value?.setError("网络错误，请重试");
    authForm.value?.refreshCaptcha();
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <div class="flex flex-col items-center justify-center min-h-screen px-5" style="background: #f5f5f7;">
    <AuthForm ref="authForm" mode="login" @submit="handleSubmit" />
  </div>
</template>
