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

const route = useRoute()
const { $toast } = useNuxtApp()
const redirect = computed(() => route.query.redirect as string || '/')

const loginForm = reactive({
    username: '',
    password: '',
    rememberMe: false,
})

const captcha = reactive({
    id: '',
    svg: '',
    answer: '',
    loading: false,
})

const loginLoading = ref(false)
const { refresh } = useAuthSession()
const router = useRouter()

async function loginWithGithub() {
    window.location.href = '/api/auth/oauth/github/authorize';
}

async function loginWithGitea() {
    window.location.href = '/api/auth/oauth/gitea/authorize';
}

async function fetchCaptcha() {
    captcha.loading = true
    try {
        const res = await $fetch<{ code: number; data: { captchaId: string; imageSvg: string } }>('/api/auth/captcha')
        captcha.id = res.data.captchaId
        captcha.svg = res.data.imageSvg
        captcha.answer = ''
    } catch (e: any) {
        console.error('获取验证码失败', e)
    } finally {
        captcha.loading = false
    }
}

async function handleLogin() {
    loginLoading.value = true
    try {
        await $fetch('/api/auth/login', {
            method: 'POST',
            body: {
                username: loginForm.username,
                password: loginForm.password,
                captchaId: captcha.id,
                captchaAnswer: captcha.answer,
            },
        })
        $toast.success('登录成功！')
        await refresh(true)
        await navigateTo(redirect.value)
    } catch (e: any) {
        $toast.error(e?.data?.statusMessage || e?.message || '登录失败')
        await fetchCaptcha()
    } finally {
        loginLoading.value = false
    }
}

onMounted(() => {
    fetchCaptcha()

    const url = new URL(window.location.href)
    if (url.searchParams.get('oauth_success') === '1') {
        refresh(true)
        router.push('/')
    }
    if (url.searchParams.get('oauth_error')) {
        const error = url.searchParams.get('oauth_error')
        console.error('OAuth error:', error)
        url.searchParams.delete('oauth_error')
        window.history.replaceState({}, document.title, url.href)
        $toast.error(`OAuth 登录失败: ${error}`)
    }
})
</script>

<template>
    <div class="auth-page">
        <div class="auth-bg-deco" aria-hidden="true">
            <div class="bg-gradient-top"></div>
            <div class="bg-gradient-bottom"></div>
            <div class="bg-accent-block bg-accent-block-1"></div>
            <div class="bg-accent-block bg-accent-block-2"></div>
            <div class="bg-accent-block bg-accent-block-3"></div>
            <div class="bg-grid"></div>
            <div class="bg-spike-spots">
                <div class="spike spike-1"></div>
                <div class="spike spike-2"></div>
                <div class="spike spike-3"></div>
                <div class="spike spike-4"></div>
            </div>
        </div>

        <div class="auth-card">
            <div class="form-header">
                <h1 class="form-title">欢迎回来</h1>
                <p class="form-subtitle">继续您的探索之旅</p>
            </div>

            <form class="auth-form" @submit.prevent="handleLogin">
                <div class="form-field">
                    <input id="login-username" v-model="loginForm.username" type="text" placeholder=" " required />
                    <label for="login-username">用户名或邮箱</label>
                </div>

                <div class="form-field">
                    <input id="login-password" v-model="loginForm.password" type="password" placeholder=" " required />
                    <label for="login-password">密码</label>
                    <a href="#" class="field-link">忘记密码？</a>
                </div>

                <div class="form-field captcha-field">
                    <input id="login-captcha" v-model="captcha.answer" type="text" placeholder=" " required />
                    <label for="login-captcha">验证码</label>
                    <div class="captcha-img" :class="{ loading: captcha.loading }" @click="fetchCaptcha" v-html="captcha.svg"></div>
                </div>

                <div class="form-options">
                    <label class="checkbox-field">
                        <input type="checkbox" v-model="loginForm.rememberMe" />
                        <span class="checkbox-custom"></span>
                        <span>记住我</span>
                    </label>
                </div>

                <button type="submit" class="submit-btn" :class="{ loading: loginLoading }" :disabled="loginLoading">
                    <span class="btn-text">{{ loginLoading ? '登录中...' : '登录' }}</span>
                    <span v-if="!loginLoading" class="btn-arrow">→</span>
                </button>
            </form>

            <div class="form-divider">
                <span class="divider-line"></span>
                <span class="divider-text">其他方式</span>
                <span class="divider-line"></span>
            </div>

            <div class="social-row">
                <button class="social-btn" @click="loginWithGithub">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
                        <path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
                    </svg>
                    <span>GitHub</span>
                </button>

                <button class="social-btn" @click="loginWithGitea">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
                        <path d="M2.5 10.5a.5.5 0 0 1 .5.5v1.5a.5.5 0 0 1-1 0V11a.5.5 0 0 1 .5-.5Zm18.5.5v1.5a.5.5 0 0 0 1 0V11a.5.5 0 0 0-1 0ZM12 15.5a1.5 1.5 0 0 1 1.5 1.5v1a.5.5 0 0 0 1 0v-1a2.5 2.5 0 0 0-5 0v1a.5.5 0 0 0 1 0v-1A1.5 1.5 0 0 1 12 15.5Z"/>
                        <path d="M20.5 12A8.5 8.5 0 0 0 12 3.5 8.5 8.5 0 0 0 3.5 12v5.25c0 .69.56 1.25 1.25 1.25h2.5c.69 0 1.25-.56 1.25-1.25V13.5c0-.69-.56-1.25-1.25-1.25h-2.5c-.09 0-.17.01-.25.02v-.27a8.5 8.5 0 0 1 17 0v.27a1.2 1.2 0 0 0-.25-.02h-2.5c-.69 0-1.25.56-1.25 1.25v3.75c0 .69.56 1.25 1.25 1.25H19.25c.69 0 1.25-.56 1.25-1.25V12Z"/>
                    </svg>
                    <span>Gitea</span>
                </button>
            </div>

            <p class="form-footer">
                还没有账户？<NuxtLink to="/auth/register">立即注册</NuxtLink>
            </p>
        </div>
    </div>
</template>

<style scoped>
.auth-page {
    min-height: 100vh;
    background: var(--color-canvas);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
}

.auth-bg-deco {
    position: absolute;
    inset: 0;
    pointer-events: none;
    overflow: hidden;
}

.bg-gradient-top {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 280px;
    background: linear-gradient(180deg, rgba(204, 120, 92, 0.08) 0%, transparent 100%);
}

.bg-gradient-bottom {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 200px;
    background: linear-gradient(0deg, rgba(204, 120, 92, 0.05) 0%, transparent 100%);
}

.bg-accent-block {
    position: absolute;
    border-radius: 50%;
    filter: blur(80px);
}

.bg-accent-block-1 {
    width: 500px;
    height: 500px;
    background: var(--color-primary);
    top: -150px;
    right: 10%;
    opacity: 0.1;
}

.bg-accent-block-2 {
    width: 400px;
    height: 400px;
    background: var(--color-accent-amber);
    bottom: -100px;
    left: 20%;
    opacity: 0.06;
}

.bg-accent-block-3 {
    width: 300px;
    height: 300px;
    background: var(--color-primary);
    bottom: 20%;
    right: -100px;
    opacity: 0.08;
}

.bg-grid {
    position: absolute;
    inset: 0;
    background-image: radial-gradient(circle, var(--color-hairline) 1px, transparent 1px);
    background-size: 20px 20px;
    opacity: 0.4;
}

.bg-spike-spots {
    position: absolute;
    inset: 0;
}

.spike {
    position: absolute;
    width: 40px;
    height: 40px;
    opacity: 0.06;
}

.spike::before,
.spike::after {
    content: '';
    position: absolute;
    background: var(--color-ink);
}

.spike::before {
    width: 100%;
    height: 2px;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
}

.spike::after {
    width: 2px;
    height: 100%;
    left: 50%;
    top: 0;
    transform: translateX(-50%);
}

.spike-1 {
    top: 15%;
    left: 10%;
    transform: rotate(15deg);
}

.spike-2 {
    top: 25%;
    right: 15%;
    transform: rotate(45deg);
}

.spike-3 {
    bottom: 20%;
    left: 15%;
    transform: rotate(30deg);
}

.spike-4 {
    bottom: 30%;
    right: 10%;
    transform: rotate(60deg);
}

.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--color-canvas);
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(20, 20, 19, 0.06);
    padding: 40px;
    position: relative;
    z-index: 1;
    margin: 24px;
}

.form-header {
    margin-bottom: 32px;
}

.form-title {
    font-family: var(--font-display);
    font-size: 32px;
    font-weight: 400;
    color: var(--color-ink);
    letter-spacing: -1px;
    line-height: 1.1;
    margin: 0 0 8px;
}

.form-subtitle {
    font-family: var(--font-body);
    font-size: 15px;
    color: var(--color-muted);
    margin: 0;
}

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

.form-field {
    position: relative;
}

.form-field input {
    width: 100%;
    padding: 20px 16px 8px;
    font-family: var(--font-body);
    font-size: 16px;
    color: var(--color-ink);
    background: var(--color-canvas);
    border: 1.5px solid var(--color-hairline);
    border-radius: 8px;
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    box-sizing: border-box;
}

.form-field input::placeholder {
    color: transparent;
}

.form-field label {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    font-family: var(--font-body);
    font-size: 15px;
    color: var(--color-muted);
    pointer-events: none;
    transition: all 0.2s ease;
}

.form-field input:focus {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 4px rgba(204, 120, 92, 0.1);
}

.form-field input:focus + label,
.form-field input:not(:placeholder-shown) + label {
    top: 12px;
    font-size: 11px;
    color: var(--color-primary);
    letter-spacing: 0.5px;
}

.field-link {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 12px;
    color: var(--color-primary);
    text-decoration: none;
    font-weight: 500;
}

.field-link:hover {
    text-decoration: underline;
}

.captcha-field input {
    padding-right: 100px;
}

.captcha-img {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    height: 36px;
    border-radius: 4px;
    cursor: pointer;
    transition: opacity 0.2s;
}

.captcha-img :deep(svg) {
    display: block;
    height: 36px;
}

.captcha-img.loading {
    opacity: 0.5;
}

.form-options {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.checkbox-field {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
}

.checkbox-field input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.checkbox-custom {
    width: 18px;
    height: 18px;
    border: 1.5px solid var(--color-hairline);
    border-radius: 4px;
    background: var(--color-canvas);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.checkbox-field input:checked + .checkbox-custom {
    background: var(--color-primary);
    border-color: var(--color-primary);
}

.checkbox-field input:checked + .checkbox-custom::after {
    content: '';
    width: 10px;
    height: 6px;
    border: 2px solid var(--color-on-primary);
    border-top: none;
    border-right: none;
    transform: rotate(-45deg) translateY(-2px);
}

.checkbox-field span:last-child {
    font-size: 13px;
    color: var(--color-muted);
}

.submit-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    width: 100%;
    padding: 18px 24px;
    font-family: var(--font-body);
    font-size: 15px;
    font-weight: 500;
    color: var(--color-on-primary);
    background: var(--color-primary);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s ease, transform 0.15s ease;
    position: relative;
    overflow: hidden;
}

.submit-btn::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.15), transparent);
    transform: translateX(-100%);
    transition: transform 0.5s ease;
}

.submit-btn:hover::before {
    transform: translateX(100%);
}

.submit-btn:hover {
    background: var(--color-primary-active);
}

.submit-btn:active {
    transform: scale(0.98);
}

.submit-btn.loading {
    opacity: 0.7;
    cursor: not-allowed;
}

.btn-arrow {
    font-size: 18px;
    transition: transform 0.2s ease;
}

.submit-btn:hover .btn-arrow {
    transform: translateX(4px);
}

.form-divider {
    display: flex;
    align-items: center;
    gap: 16px;
    margin: 24px 0;
}

.divider-line {
    flex: 1;
    height: 1px;
    background: var(--color-hairline);
}

.divider-text {
    font-size: 12px;
    color: var(--color-muted-soft);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.social-row {
    display: flex;
    gap: 12px;
}

.social-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 14px 16px;
    font-family: var(--font-body);
    font-size: 14px;
    font-weight: 500;
    color: var(--color-muted);
    background: var(--color-canvas);
    border: 1.5px solid var(--color-hairline);
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.social-btn:hover {
    color: var(--color-ink);
    background: var(--color-surface-soft);
    border-color: var(--color-hairline-soft);
}

.social-btn:active {
    transform: scale(0.97);
}

.form-footer {
    text-align: center;
    margin-top: 24px;
    font-size: 14px;
    color: var(--color-muted);
}

.form-footer a {
    color: var(--color-primary);
    text-decoration: none;
    font-weight: 500;
}

.form-footer a:hover {
    text-decoration: underline;
}
</style>