<template>
    <div class="header">
        <div class="header-content">
            <div class="header-left">
                <div class="logo-section" @click="handleLogoClick">
                    <img :src="logoUrl" alt="Logo" class="logo" />
                    <span class="platform-title">{{ platformTitle }}</span>
                </div>
            </div>

            <div class="header-nav">
                <div
                    class="nav-item"
                    :key="item.routePath"
                    v-for="item in navItems"
                    @click="handleNavClick(item)"
                    :class="{ active: activeNav === item.routePath }"
                >
                    <img :src="item.icon" :alt="item.label" class="nav-icon" />
                    <span class="nav-text">{{ item.label }}</span>
                </div>
            </div>

            <div v-if="showRightSection" class="header-right"> </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'NavBar',
    props: {
        logoUrl: {
            type: String,
            default: '/img/psp/productCenter/logo.png',
        },
        platformTitle: {
            type: String,
            default: '公共服务平台',
        },
        showRightSection: {
            type: Boolean,
            default: true,
        },
        showSearch: {
            type: Boolean,
            default: true,
        },
        showUserAvatar: {
            type: Boolean,
            default: true,
        },
        userAvatar: {
            type: String,
            default: '',
        },
        searchKeyword: {
            type: String,
            default: '',
        },
    },
    data() {
        return {
            activeNav: 'productCenter', // 默认选中产品中心
            userAvatarUrl: this.userAvatar,
            internalSearchKeyword: this.searchKeyword,
            navItems: [
                {
                    label: '产品中心',
                    routePath: 'productCenter',
                    icon: '/img/psp/productCenter/products.png',
                },
                {
                    label: '公共组件',
                    routePath: 'commonCom',
                    icon: '/img/psp/productCenter/components.png',
                },
                {
                    label: '宣传物料',
                    routePath: 'proMaterials',
                    icon: '/img/psp/productCenter/materials.png',
                },
                {
                    label: '文档中心',
                    routePath: 'docCenter',
                    icon: '/img/psp/productCenter/docs.png',
                },
            ],
        }
    },
    watch: {
        searchKeyword(newVal) {
            this.internalSearchKeyword = newVal
        },
        userAvatar(newVal) {
            this.userAvatarUrl = newVal
        },
    },
    methods: {
        handleNavClick(item) {
            this.activeNav = item.routePath
            this.$router.push(item.routePath)
        },
        handleSearch() {
            // 修复搜索功能，直接使用internalSearchKeyword
            this.$emit('update:searchKeyword', this.internalSearchKeyword)
            this.$emit('search', this.internalSearchKeyword)
        },

        // 跳转后台
        handleLogoClick() {
            this.$router.push('/')
        },
    },
}
</script>

<style scoped>
.header {
    background-image: url('/img/psp/productCenter/topbg.png');
    color: white;
    padding: 0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    max-width: 100vw;
    margin: 0 auto;
    padding: 12px 20px;
}

.header-left {
    display: flex;
    align-items: center;
}

.logo-section {
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
}

.logo {
    width: 32px;
    height: 32px;
    border-radius: 6px;
}

.platform-title {
    font-size: 18px;
    font-weight: 600;
    color: white;
}

.header-nav {
    display: flex;
    gap: 32px;
    justify-content: space-between;
}

.nav-item {
    padding: 8px 16px;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.2s ease;
    font-size: 14px;
    display: flex;
    align-items: center;
}

.nav-icon {
    width: 20px;
    height: 20px;
    object-fit: contain;
    margin-right: 10px;
    flex-shrink: 0;
}

.nav-text {
    white-space: nowrap;
}

.nav-item:hover,
.nav-item.active {
    background-color: rgba(255, 255, 255, 0.15);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.search-box {
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 20px;
    padding: 6px 12px;
}

.search-input {
    background: transparent;
    border: none;
    outline: none;
    color: white;
    font-size: 14px;
    width: 200px;
}

.search-input::placeholder {
    color: rgba(255, 255, 255, 0.7);
}

.search-btn {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    padding: 4px;
}

.user-avatar img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
    .header-content {
        flex-direction: column;
        gap: 12px;
        padding: 16px;
    }

    .header-nav {
        gap: 16px;
    }

    .search-input {
        width: 150px;
    }
}
</style>
