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

      <div class="header-nav">
        <div
          v-for="item in navItems"
          :key="item.key"
          class="nav-item"
          :class="{ active: activeNav === item.key }"
          @click="handleNavClick(item)"
        >
          <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 v-if="showSearch" class="search-box">
          <input
            v-model="internalSearchKeyword"
            type="text"
            placeholder="输入关键词搜索"
            class="search-input"
            @keyup.enter="handleSearch"
          />
          <button class="search-btn" @click="handleSearch">
            <i class="el-icon-search"></i>
          </button>
        </div>
        <div v-if="showUserAvatar" class="user-avatar">
          <img :src="$store.state.user.avatar" alt="用户头像" />
        </div>-->
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'NavBar',
  props: {
    logoUrl: {
      type: String,
      default: '/img/psp/productCenter/logo.png'
    },
    platformTitle: {
      type: String,
      default: '公共服务平台'
    },
    navItems: {
      type: Array,
      default: () => [
        { key: 'products', label: '产品中心', icon: '/img/psp/productCenter/products.png',route: '/productCenter/index' },
        { key: 'components', label: '公共组件', icon: '/img/psp/productCenter/components.png',route: '/common/index' },
        { key: 'materials', label: '宣传物料', icon: '/img/psp/productCenter/materials.png',route: '/materials/index'},
        { key: 'docs', label: '文档中心', icon: '/img/psp/productCenter/docs.png',route: '/docs/index'}
      ]
    },
    activeNav: {
      type: String,
      default: 'products'
    },
    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 {
      internalSearchKeyword: this.searchKeyword,
      userAvatarUrl: this.userAvatar,
    }
  },
  watch: {
    searchKeyword(newVal) {
      this.internalSearchKeyword = newVal;
    },
    userAvatar(newVal) {
      this.userAvatarUrl = newVal;
    }
  },
  methods: {
    handleNavClick(item) {
      // 发射导航变化事件
      this.$emit('nav-change', item.key);
      // 执行路由跳转
      if (item.route) {
        this.$router.push(item.route);
      }
    },
    handleSearch() {
      // 修复搜索功能，直接使用internalSearchKeyword
      this.$emit('update:searchKeyword', this.internalSearchKeyword);
      this.$emit('search', this.internalSearchKeyword);
    },
  }
}
</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;
}

.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>
