<template>
    <view v-if="!appStore.configLoaded" class="app-loading-mask">
        <view class="app-loading-spinner">
            <view class="app-loading-circle"></view>
        </view>
        <text class="app-loading-text">{{ text }}</text>

        <view v-if="timeout" class="app-loading-tip">
            <text class="app-loading-tip__title">⚠️ 加载缓慢</text>
            <text class="app-loading-tip__desc">请检查网络后关闭并重新打开 App</text>
            <view class="app-loading-tip__btn" @tap="retry">
                <text>重试</text>
            </view>
        </view>
    </view>
</template>

<script setup lang="ts">
import { ref, watch, onMounted, onBeforeUnmount } from 'vue'
import { useAppStore } from '@/stores/app'

defineProps({
    text: {
        type: String,
        default: '加载中...'
    }
})

const appStore = useAppStore()
const timeout = ref(false)
let timer: any = null

const startTimer = () => {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
        if (!appStore.configLoaded) {
            timeout.value = true
        }
    }, 5000)
}

const retry = () => {
    timeout.value = false
    startTimer()
    appStore.getConfig().catch((err: any) => {
        console.error('[AppLoading] 重试加载配置失败:', err)
    })
}

onMounted(() => {
    if (!appStore.configLoaded) startTimer()
})

watch(() => appStore.configLoaded, (val) => {
    if (val) {
        timeout.value = false
        if (timer) {
            clearTimeout(timer)
            timer = null
        }
    } else {
        startTimer()
    }
})

onBeforeUnmount(() => {
    if (timer) clearTimeout(timer)
})
</script>

<style lang="scss" scoped>
.app-loading-mask {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.app-loading-spinner {
    width: 80rpx;
    height: 80rpx;
    margin-bottom: 24rpx;
}

.app-loading-circle {
    width: 100%;
    height: 100%;
    border: 6rpx solid #f0f0f0;
    border-top-color: #0b55eb;
    border-radius: 50%;
    animation: app-loading-spin 0.8s linear infinite;
}

@keyframes app-loading-spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.app-loading-text {
    font-size: 26rpx;
    color: #999999;
}

.app-loading-tip {
    margin-top: 64rpx;
    padding: 32rpx 40rpx;
    background: #fff8e6;
    border: 2rpx solid #ffd591;
    border-radius: 16rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    max-width: 560rpx;

    &__title {
        font-size: 30rpx;
        font-weight: 600;
        color: #d46b08;
        margin-bottom: 12rpx;
    }

    &__desc {
        font-size: 24rpx;
        color: #ad6800;
        text-align: center;
        line-height: 1.6;
        margin-bottom: 24rpx;
    }

    &__btn {
        padding: 14rpx 48rpx;
        background: #0b55eb;
        border-radius: 40rpx;

        text {
            font-size: 26rpx;
            color: #ffffff;
        }
    }
}
</style>
