<template>
    <div class="module-tabs">
        <div class="module-tabs__logo">
            <img src="@/assets/images/home/logo@2x.png" alt="logo" />
        </div>
        <ul class="module-tabs__list">
            <li
                v-for="m in MODULE_LIST"
                :key="m.key"
                class="module-tabs__item"
                :class="{
                    'is-active': activeKey === m.key,
                    'is-disabled': !m.implemented,
                }"
                @click="onClick(m)"
            >
                {{ m.title }}
            </li>
        </ul>
    </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { storeToRefs } from 'pinia'
import useAppStore from '@/store/modules/app'
import {
    MODULE_LIST,
    ModuleKey,
    type ModuleDefinition,
    resolveModuleByPath,
} from '@/enums/ModuleEnum'

const appStore = useAppStore()
const router = useRouter()
const route = useRoute()

const { activeModule } = storeToRefs(appStore)

// 当前激活：优先以 store 中的 activeModule 为准；store 未初始化时按当前路径反查
const activeKey = computed<ModuleKey>(() => {
    return activeModule.value || resolveModuleByPath(route.path)
})

const onClick = (m: ModuleDefinition) => {
    appStore.setActiveModule(m.key)
    router.push(m.entry || m.path)
}
</script>

<style lang="scss" scoped>
.module-tabs {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 20px;
    background: linear-gradient(90deg, #1e88e5 0%, #42a5f5 100%);
    color: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    user-select: none;

    &__logo {
        display: flex;
        align-items: center;
        margin-right: 32px;

        img {
            height: 28px;
            display: block;
        }
    }

    &__list {
        display: flex;
        align-items: center;
        gap: 8px;
        list-style: none;
        margin: 0;
        padding: 0;
        height: 100%;
    }

    &__item {
        height: 100%;
        padding: 0 18px;
        display: flex;
        align-items: center;
        font-size: 14px;
        cursor: pointer;
        position: relative;
        opacity: 0.85;
        transition: opacity 0.15s;

        &:hover {
            opacity: 1;
        }

        &.is-active {
            opacity: 1;
            font-weight: 600;

            &::after {
                content: '';
                position: absolute;
                left: 18px;
                right: 18px;
                bottom: 0;
                height: 3px;
                background: #fff;
                border-radius: 2px;
            }
        }

        &.is-disabled {
            opacity: 0.55;
        }
    }
}
</style>
