<script setup lang="ts">
const route = useRoute()
const activeTab = ref<'login' | 'register'>(route.query.tab === 'register' ? 'register' : 'login')

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

const registerForm = reactive({
    username: '',
    password: '',
    confirmPassword: '',
})

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

const loginError = ref('')
const registerError = ref('')
const loginLoading = ref(false)
const registerLoading = ref(false)
const { refresh } = useAuthSession()

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() {
    loginError.value = ''
    loginLoading.value = true
    try {
        await $fetch('/api/auth/login', {
            method: 'POST',
            body: {
                username: loginForm.username,
                password: loginForm.password,
                captchaId: captcha.id,
                captchaAnswer: captcha.answer,
            },
        })
        await refresh(true)
        await navigateTo('/')
    } catch (e: any) {
        loginError.value = e?.data?.statusMessage || e?.message || '登录失败'
        await fetchCaptcha()
    } finally {
        loginLoading.value = false
    }
}

async function handleRegister() {
    registerError.value = ''
    if (registerForm.password !== registerForm.confirmPassword) {
        registerError.value = '两次密码输入不一致'
        return
    }
    registerLoading.value = true
    try {
        await $fetch('/api/auth/register', {
            method: 'POST',
            body: {
                username: registerForm.username,
                password: registerForm.password,
                captchaId: captcha.id,
                captchaAnswer: captcha.answer,
            },
        })
        activeTab.value = 'login'
        registerForm.username = ''
        registerForm.password = ''
        registerForm.confirmPassword = ''
        loginForm.username = registerForm.username
        await nextTick()
        await fetchCaptcha()
    } catch (e: any) {
        registerError.value = e?.data?.statusMessage || e?.message || '注册失败'
        await fetchCaptcha()
    } finally {
        registerLoading.value = false
    }
}

onMounted(fetchCaptcha)

watch(activeTab, fetchCaptcha)
</script>

<template>
    <div class="auth-page">
        <!-- Decorative background elements -->
        <div class="auth-bg-deco" aria-hidden="true">
            <div class="bg-accent-block bg-accent-block-1"></div>
            <div class="bg-accent-block bg-accent-block-2"></div>
            <div class="bg-grid"></div>
        </div>

        <div class="auth-container">
            <!-- Left: Editorial typography panel -->
            <div class="auth-editorial">
                <div class="editorial-content">
                    <span class="editorial-label">curate / collect / share</span>
                    <h2 class="editorial-headline">
                        你的<br />灵感<br />宇宙
                    </h2>
                    <div class="editorial-rule"></div>
                    <p class="editorial-body">
                        在这里存储和展示您的收藏，与志同道合的人分享您的视觉故事
                    </p>
                </div>

                <div class="editorial-visual">
                    <div class="visual-card visual-card-1">
                        <img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=300&fit=crop" alt="" />
                    </div>
                    <div class="visual-card visual-card-2">
                        <img src="https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=400&h=300&fit=crop" alt="" />
                    </div>
                </div>
            </div>

            <!-- Right: Auth form -->
            <div class="auth-form-section">
                <div class="auth-form-card">
                    <div class="form-header">
                        <div class="form-tabs">
                            <button class="form-tab" :class="{ active: activeTab === 'login' }" @click="activeTab = 'login'">
                                <span class="tab-marker"></span>
                                登录
                            </button>
                            <button class="form-tab" :class="{ active: activeTab === 'register' }" @click="activeTab = 'register'">
                                <span class="tab-marker"></span>
                                注册
                            </button>
                        </div>

                        <Transition name="title-slide" mode="out-in">
                            <h1 :key="activeTab" class="form-title">
                                {{ activeTab === 'login' ? '欢迎回来' : '创建账户' }}
                            </h1>
                        </Transition>
                        <Transition name="title-slide" mode="out-in">
                            <p :key="activeTab + '-sub'" class="form-subtitle">
                                {{ activeTab === 'login' ? '继续您的探索之旅' : '加入我们开始策展' }}
                            </p>
                        </Transition>
                    </div>

                    <Transition name="form-fade" mode="out-in">
                        <!-- Login Form -->
                        <form v-if="activeTab === 'login'" key="login" class="auth-form" @submit.prevent="handleLogin">
                            <div v-if="loginError" class="form-error">{{ loginError }}</div>

                            <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>

                        <!-- Register Form -->
                        <form v-else key="register" class="auth-form" @submit.prevent="handleRegister">
                            <div v-if="registerError" class="form-error">{{ registerError }}</div>

                            <div class="form-field">
                                <input id="register-username" v-model="registerForm.username" type="text" placeholder=" " required />
                                <label for="register-username">用户名</label>
                            </div>

                            <div class="form-field">
                                <input id="register-password" v-model="registerForm.password" type="password" placeholder=" " required />
                                <label for="register-password">密码</label>
                            </div>

                            <div class="form-field">
                                <input id="register-confirm" v-model="registerForm.confirmPassword" type="password" placeholder=" " required />
                                <label for="register-confirm">确认密码</label>
                            </div>

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

                            <button type="submit" class="submit-btn" :class="{ loading: registerLoading }" :disabled="registerLoading">
                                <span class="btn-text">{{ registerLoading ? '注册中...' : '创建账户' }}</span>
                                <span v-if="!registerLoading" class="btn-arrow">→</span>
                            </button>
                        </form>
                    </Transition>

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

                    <button class="social-btn">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
                            <path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z" />
                            <path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" />
                            <path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" />
                            <path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" />
                        </svg>
                        使用 Google 登录
                    </button>

                    <p class="form-footer">
                        <template v-if="activeTab === 'login'">
                            还没有账户？<a href="#" @click.prevent="activeTab = 'register'">立即注册</a>
                        </template>
                        <template v-else>
                            已有账户？<a href="#" @click.prevent="activeTab = 'login'">立即登录</a>
                        </template>
                    </p>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
.auth-page {
    min-height: 100vh;
    background: var(--color-canvas);
    position: relative;
    overflow: hidden;
}

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

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

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

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

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

.auth-container {
    display: grid;
    grid-template-columns: 1fr;
    min-height: 100vh;
    position: relative;
    z-index: 1;
}

@media (min-width: 1024px) {
    .auth-container {
        grid-template-columns: 1fr 1fr;
    }
}

.auth-editorial {
    display: none;
    background: var(--color-surface-card);
    padding: 64px 48px;
    position: relative;
    overflow: hidden;
}

@media (min-width: 1024px) {
    .auth-editorial {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }
}

.editorial-content {
    position: relative;
    z-index: 2;
}

.editorial-label {
    display: inline-block;
    font-family: var(--font-body);
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: var(--color-muted);
    margin-bottom: 24px;
    padding: 6px 12px;
    background: var(--color-canvas);
    border-radius: 4px;
}

.editorial-headline {
    font-family: var(--font-display);
    font-size: clamp(56px, 8vw, 80px);
    font-weight: 400;
    color: var(--color-ink);
    letter-spacing: -2px;
    line-height: 1;
    margin: 0 0 32px;
}

.editorial-rule {
    width: 60px;
    height: 3px;
    background: var(--color-primary);
    margin-bottom: 24px;
}

.editorial-body {
    font-family: var(--font-body);
    font-size: 16px;
    color: var(--color-body);
    line-height: 1.6;
    margin: 0;
    max-width: 320px;
}

.editorial-visual {
    position: relative;
    height: 240px;
    margin-top: auto;
}

.visual-card {
    position: absolute;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 16px 48px rgba(20, 20, 19, 0.15);
}

.visual-card img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.visual-card-1 {
    width: 200px;
    height: 160px;
    bottom: 0;
    left: 0;
    transform: rotate(-3deg);
    z-index: 2;
}

.visual-card-2 {
    width: 160px;
    height: 120px;
    bottom: 40px;
    right: 40px;
    transform: rotate(5deg);
    z-index: 1;
}

.auth-form-section {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 48px 24px;
}

@media (min-width: 1024px) {
    .auth-form-section {
        padding: 64px 48px;
    }
}

.auth-form-card {
    width: 100%;
    max-width: 420px;
}

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

.form-tabs {
    display: flex;
    gap: 0;
    margin-bottom: 32px;
    padding: 4px;
    background: var(--color-surface-card);
    border-radius: 6px;
}

.form-tab {
    flex: 1;
    padding: 10px 16px;
    font-family: var(--font-body);
    font-size: 13px;
    font-weight: 500;
    color: var(--color-muted);
    background: transparent;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: color 0.2s ease;
}

.tab-marker {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--color-muted);
    transition: background 0.2s ease, transform 0.2s ease;
}

.form-tab.active {
    background: var(--color-canvas);
    color: var(--color-ink);
    box-shadow: 0 2px 8px rgba(20, 20, 19, 0.06);
}

.form-tab.active .tab-marker {
    background: var(--color-primary);
    transform: scale(1.2);
}

.form-title {
    font-family: var(--font-display);
    font-size: 36px;
    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;
}

.title-slide-enter-active,
.title-slide-leave-active {
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.title-slide-enter-from {
    opacity: 0;
    transform: translateY(8px);
}

.title-slide-leave-to {
    opacity: 0;
    transform: translateY(-8px);
}

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

.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-error {
    padding: 12px 16px;
    background: rgba(198, 69, 69, 0.08);
    border: 1px solid rgba(198, 69, 69, 0.2);
    border-radius: 6px;
    color: var(--color-error);
    font-size: 13px;
}

.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;
    margin-top: 8px;
    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-fade-enter-active,
.form-fade-leave-active {
    transition: opacity 0.25s ease, transform 0.25s ease;
}

.form-fade-enter-from {
    opacity: 0;
    transform: translateY(16px);
}

.form-fade-leave-to {
    opacity: 0;
    transform: translateY(-16px);
}

.form-divider {
    display: flex;
    align-items: center;
    gap: 16px;
    margin: 32px 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-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    width: 100%;
    padding: 16px 20px;
    font-family: var(--font-body);
    font-size: 14px;
    font-weight: 500;
    color: var(--color-ink);
    background: var(--color-canvas);
    border: 1.5px solid var(--color-hairline);
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s ease, border-color 0.2s ease;
}

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

.form-footer {
    text-align: center;
    margin-top: 32px;
    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>