<script setup>
import { ref, computed, onUnmounted, nextTick } from 'vue'
import { showToast } from 'vant'
import ClickCaptcha from '@/components/ClickCaptcha.vue'
import { useAliyunCaptcha } from "@/composables/useAliyunCaptcha";

const router = useRouter()
const phoneNumber = ref('')
const verificationCode = ref('')
const password = ref('')
const isPasswordLogin = ref(false)
const isAgreed = ref(false)
const isCountingDown = ref(false)
const countdown = ref(60)
let timer = null

const { runWithCaptcha } = useAliyunCaptcha();

// 验证组件状态
const showCaptcha = ref(false)
const captchaVerified = ref(false)

// 聚焦状态变量
const phoneFocused = ref(false)
const codeFocused = ref(false)
const passwordFocused = ref(false)

const isPhoneNumberValid = computed(() => {
    return /^1[3-9]\d{9}$/.test(phoneNumber.value)
})

const canLogin = computed(() => {
    if (!isPhoneNumberValid.value) return false
    if (isPasswordLogin.value) {
        return password.value.length >= 6
    } else {
        return verificationCode.value.length === 6
    }
})

async function sendVerificationCode() {
    if (isCountingDown.value || !isPhoneNumberValid.value) return
    if (!isPhoneNumberValid.value) {
        showToast({ message: "请输入有效的手机号" });
        return
    }
    await runWithCaptcha(
        (captchaVerifyParam) =>
            useApiFetch('auth/sendSms')
                .post({ mobile: phoneNumber.value, actionType: 'login', captchaVerifyParam })
                .json(),
        (res) => {
            if (res.code === 200) {
                showToast({ message: "获取成功" });
                startCountdown();
                nextTick(() => {
                    const verificationCodeInput = document.getElementById('verificationCode');
                    if (verificationCodeInput) {
                        verificationCodeInput.focus();
                    }
                });
            } else {
                showToast({ message: res.msg || "发送失败" });
            }
        }
    );
}

function startCountdown() {
    isCountingDown.value = true
    countdown.value = 60
    timer = setInterval(() => {
        if (countdown.value > 0) {
            countdown.value--
        } else {
            clearInterval(timer)
            isCountingDown.value = false
        }
    }, 1000)
}

async function handleLogin() {
    if (!isPhoneNumberValid.value) {
        showToast({ message: "请输入有效的手机号" });
        return
    }
    if (isPasswordLogin.value) {
        if (password.value.length < 6) {
            showToast({ message: "密码长度不能小于6位" });
            return
        }
    } else {
        if (verificationCode.value.length !== 6) {
            showToast({ message: "请输入有效的验证码" });
            return
        }
    }
    if (!isAgreed.value) {
        showToast({ message: "请先同意用户协议" });
        return
    }
    // 显示验证组件
    showCaptcha.value = true
}

// 验证成功回调
function handleCaptchaSuccess() {
    captchaVerified.value = true
    showCaptcha.value = false
    // 执行实际的登录逻辑
    performLogin()
}

// 验证关闭回调
function handleCaptchaClose() {
    showCaptcha.value = false
}

// 执行实际的登录逻辑
async function performLogin() {
    const { data, error } = await useApiFetch('/user/mobileCodeLogin')
        .post({ mobile: phoneNumber.value, code: verificationCode.value })
        .json()

    if (data.value && !error.value) {
        if (data.value.code === 200) {
            localStorage.setItem('token', data.value.data.accessToken)
            localStorage.setItem('refreshAfter', data.value.data.refreshAfter)
            localStorage.setItem('accessExpire', data.value.data.accessExpire)
            window.location.href = '/'
        }
    }
}
function toUserAgreement() {
    router.push(`/userAgreement`)
}

function toPrivacyPolicy() {
    router.push(`/privacyPolicy`)
}

onUnmounted(() => {
    if (timer) {
        clearInterval(timer)
    }
})
const onClickLeft = () => {
    router.replace('/')
}
</script>

<template>
    <div class="login-layout ">
        <van-nav-bar fixed placeholder title="用户登录" left-text="" left-arrow @click-left="onClickLeft" />
        <div class="login px-4 relative z-10">
            <div class="mb-8 pt-20 text-left">
                <div class="flex flex-col items-center">
                    <img class="h-16 w-16 rounded-full shadow" src="/logo.png" alt="Logo" />
                    <div class="text-3xl mt-4 text-slate-700 font-bold">天远数据</div>
                </div>
            </div>

            <!-- 登录表单 -->
            <div class="login-form">
                <!-- 手机号输入 -->
                <div class="form-item">
                    <div class="form-label">手机号</div>
                    <input v-model="phoneNumber" class="form-input" type="tel" placeholder="请输入手机号" maxlength="11" />
                </div>

                <!-- 验证码输入 -->
                <div class="form-item">
                    <div class="form-label">验证码</div>
                    <div class="verification-input-wrapper">
                        <input v-model="verificationCode" id="verificationCode" class="form-input verification-input"
                            placeholder="请输入验证码" maxlength="6" />
                        <button class="get-code-btn" :class="{ 'disabled': isCountingDown || !isPhoneNumberValid }"
                            @click="sendVerificationCode" :disabled="isCountingDown || !isPhoneNumberValid">
                            {{ isCountingDown ? `${countdown}s` : '获取验证码' }}
                        </button>
                    </div>
                </div>

                <!-- 协议同意框 -->
                <div class="agreement-wrapper">
                    <input type="checkbox" v-model="isAgreed" class="agreement-checkbox accent-primary"
                        id="agreement" />
                    <label for="agreement" class="agreement-text">
                        我已阅读并同意
                        <a class="agreement-link" @click="toUserAgreement">《用户协议》</a>
                        <a class="agreement-link" @click="toPrivacyPolicy">《隐私政策》</a>
                    </label>
                </div>

                <!-- 提示文字 -->
                <div class="notice-text">
                    未注册手机号登录后将自动生成账号，并且代表您已阅读并同意
                </div>

                <!-- 登录按钮 -->
                <button class="login-btn" :class="{ 'disabled': !canLogin }" @click="handleLogin" :disabled="!canLogin">
                    登 录
                </button>
            </div>
        </div>

        <!-- 点击验证组件 -->
        <ClickCaptcha :visible="showCaptcha" @success="handleCaptchaSuccess" @close="handleCaptchaClose" />

    </div>
</template>

<style scoped>
.login-layout {
    background-image: url('@/assets/images/login_bg.png');
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    height: 100vh;
    position: relative;
    overflow: hidden;
}

.login {}

/* 登录表单 */
.login-form {
    background-color: var(--color-bg-primary);
    padding: 2rem;
    margin-top: 0.5rem;
    box-shadow: 0px 0px 24px 0px #3F3F3F0F;
    border-radius: 8px;
}

/* 表单项 */
.form-item {
    margin-bottom: 1.5rem;
    display: flex;
    align-items: center;
    border: none;
    border-bottom: 1px solid var(--color-border-primary);
}

.form-label {
    font-size: 0.9375rem;
    color: var(--color-text-primary);
    margin-bottom: 0;
    margin-right: 1rem;
    font-weight: 500;
    min-width: 4rem;
    flex-shrink: 0;
}

.form-input {
    width: 100%;
    padding: 0.875rem 0;

    font-size: 0.9375rem;
    color: var(--color-text-primary);
    outline: none;
    background-color: transparent;
}

.form-input::placeholder {
    color: var(--color-text-tertiary);
}

.form-input:focus {
    border-bottom-color: var(--color-text-primary);
}

/* 验证码输入 */
.verification-input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    flex: 1;
}

.verification-input {
    flex: 1;
    padding-right: 6rem;
}

.get-code-btn {
    position: absolute;
    right: 0;
    background: none;
    border: none;
    color: var(--color-primary);
    font-size: 0.875rem;
    cursor: pointer;
    padding: 0.5rem;
    font-weight: 500;
}

.get-code-btn.disabled {
    color: var(--color-gray-400);
    cursor: not-allowed;
}

/* 协议同意 */
.agreement-wrapper {
    display: flex;
    align-items: center;
    margin-top: 1.5rem;
    margin-bottom: 1rem;
}

.agreement-checkbox {
    flex-shrink: 0;
    margin-right: 0.5rem;
}

.agreement-text {
    font-size: 0.75rem;
    color: var(--color-text-secondary);
    line-height: 1.4;
}

.agreement-link {
    color: var(--color-primary);
    cursor: pointer;
    text-decoration: none;
}

/* 提示文字 */
.notice-text {
    font-size: 0.6875rem;
    color: var(--color-text-tertiary);
    line-height: 1.5;
    margin-bottom: 2rem;
}

/* 登录按钮 */
.login-btn {
    width: 100%;
    padding: 0.875rem;
    background-color: var(--color-primary);
    color: var(--color-text-white);
    border: none;
    border-radius: 1.5rem;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: opacity 0.3s;
    letter-spacing: 0.25rem;
}

.login-btn:hover {
    opacity: 0.9;
}

.login-btn.disabled {
    background-color: var(--color-gray-300);
    cursor: not-allowed;
}
</style>
