import { TabList } from '@/common/components/description/interface';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { initData } from '@/common/methods';
import { getThirdMenuData } from '@/common/setup/table/button';
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';

export function handleMartketThirdRouter(code: EnumRouterName) {
    const list = getThirdMenuData();
    const item = list.find((e) => e.code === code);
    const tabList = item
        ? item.children.map((e) => {
            return { lable: e.title, code: e.code };
        })
        : [];
    const router = useRouter();
    const route = useRoute();
    const index = ref<string>('0');
    initData(() => {
        if (tabList.length) {
            const name = route.name as string;
            const result = tabList.findIndex((e) => e.code === name);
            // 处理页面刷新
            if (result !== -1) {
                router.push({ name });
                index.value = result.toString();
            } else {
                router.push({ name: tabList[0].code });
                index.value = '0';
            }
        }
    });
    // 切换tab 跳转路由
    function changeTab(index: number, current: TabList) {
        router.push({ name: current.code });
    }
    return { index, tabList, changeTab }
}