<template>
  <div class="search-filter-bar">
    <div class="search-input-wrapper">
      <el-input
        v-model="searchKeyword"
        placeholder="请输入关键词搜索项目"
        clearable
        size="small"
        class="search-input"
        @keyup.enter.native="handleSearch"
        @clear="handleSearch"
      />
      <el-button type="primary" size="small" @click="handleSearch">搜索</el-button>
    </div>
    <div class="tab-filters">
      <div
        v-for="tab in visibleTabs"
        :key="tab.value"
        :class="['tab-item', { active: activeTab === tab.value }]"
        @click="handleTabChange(tab.value)"
      >
        {{ tab.label }}({{ tab.count }})
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SearchBar',
  props: {
    showSearch: {
      type: Boolean,
      default: true
    },
    tabCounts: {
      type: Object,
      default: () => ({
        all: 0,
        '0': 0,
        '1': 0,
        '2': 0,
        '3': 0,
        '4': 0,
        '5': 0
      })
    },
    showDeletedTab: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      searchKeyword: '',
      activeTab: 'all',
      tabs: [
        { label: '全部项目', value: 'all', count: 0 },
        { label: '进行中', value: '0', count: 0 },
        { label: '已完成', value: '1', count: 0 },
        { label: '已归档', value: '2', count: 0 },
        { label: '打标中', value: '3', count: 0 },
        { label: '打标失败', value: '4', count: 0 },
        { label: '已删除', value: 'deleted', count: 0 }
      ]
    }
  },
  computed: {
    visibleTabs() {
      return this.tabs.filter(tab => tab.value !== 'deleted' || this.showDeletedTab)
    }
  },
  watch: {
    tabCounts: {
      handler(newVal) {
        this.tabs = this.tabs.map(tab => ({
          ...tab,
          count: newVal[tab.value === 'deleted' ? '5' : tab.value] || 0
        }))
      },
      immediate: true,
      deep: true
    },
    showDeletedTab(newVal) {
      if (!newVal && this.activeTab === 'deleted') {
        this.activeTab = 'all'
        this.emitQuery()
      }
    }
  },
  methods: {
    /** 搜索 */
    handleSearch() {
      this.emitQuery()
    },
    /** 标签页切换 */
    handleTabChange(tabValue) {
      this.activeTab = tabValue
      this.emitQuery()
    },
    /** 发送查询 */
    emitQuery() {
      const includeDeleted = this.activeTab === 'deleted'
      this.$emit('query', {
        projectName: this.searchKeyword || null,
        status: includeDeleted || this.activeTab === 'all' ? null : this.activeTab,
        includeDeleted
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-filter-bar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 16px 20px;
  background: var(--ccdi-surface);
  border: 1px solid var(--ccdi-border);
  border-radius: 14px;
  box-shadow: var(--ccdi-shadow);
}

.search-input-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
}

.search-input {
  width: 240px;
  // Let Element UI's size="small" control the height naturally
}

.tab-filters {
  display: flex;
  align-items: center;
  gap: 24px;
}

.tab-item {
  font-size: 14px;
  color: var(--ccdi-text-secondary);
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 10px;
  border: 1px solid transparent;
  transition: all 0.2s ease;
  user-select: none;

  &:hover {
    color: var(--ccdi-primary);
  }

  &.active {
    color: var(--ccdi-primary);
    background: var(--ccdi-primary-soft);
    border-color: #d6e3f0;
    font-weight: 600;
  }
}
</style>
