<script setup lang="ts">
import { ref } from 'vue'
import { useTokenStore } from '@/store/token'
import FgTabbar from '@/tabbar/index.vue'
import { useLoadingStore } from './store/loading'
import { isPageTabbar, tabbarStore } from './tabbar/store'
import { currRoute } from './utils'

const isCurrentPageTabbar = ref(true)
const loadingStore = useLoadingStore()
const tokenStore = useTokenStore()

onLoad((options) => {
    console.log('App.ku.vue onLoad', options)
    // 判断是否是通过分享进入
    const paramKey = 'CCShareId'
    const keyName = 'shareRecordId'
    // 券中心小程序码变更，参数需从scene中获取
    function getSceneData(paramsQuery) {
        const temp = {}
        if (paramsQuery?.scene) {
            const sceneData = decodeURIComponent(paramsQuery.scene)
            const [key, value] = sceneData.split('=')
            if (key === paramKey) {
                temp[keyName] = value
            }
            console.log(temp)
            return temp
        }
        return temp
    }
    const queryParams = getSceneData(options)
    if (!!queryParams && Object.keys(queryParams).includes(keyName)) {
        tokenStore.cacheShareParams(queryParams)
    }
})

onShow(() => {
    const { path, query } = currRoute()
    // “蜡笔小开心”提到本地是 '/pages/index/index'，线上是 '/' 导致线上 tabbar 不见了
    // 所以这里需要判断一下，如果是 '/' 就当做首页，也要显示 tabbar
    isCurrentPageTabbar.value = path === '/' || isPageTabbar(path)
    setTimeout(() => {
        if (isCurrentPageTabbar.value) {
            tabbarStore.setAutoCurIdx(path)
        }
    }, 0)

    loadingStore.hideLoading()
})

const exposeRef = ref('this is form app.Ku.vue')

defineExpose({
    exposeRef,
})
</script>

<template>
    <view>
        <!-- 这个先隐藏了，知道这样用就行 -->
        <!-- <view class="hidden text-center">
            {{ helloKuRoot }}，这里可以配置全局的东西
        </view> -->
        <up-no-network />

        <KuRootView />
        <up-loading-page :loading="loadingStore.isLoading" :z-index="1000" size="50" />
        <FgTabbar v-if="isCurrentPageTabbar" />
    </view>
</template>
