<template>
  <div class="news-topbar"> 
    <div class="news-search-wrapper">
      <div class="news-search-box">
        <input
          type="text"
          class="news-search-input"
          :placeholder="'搜索新闻动态'"
          :value="searchText"
          @input="$emit('update:searchText', $event.target.value)"
        />
        <svg class="news-search-icon" width="16" height="16" viewBox="0 0 16 16" fill="none">
          <circle cx="7" cy="7" r="5.5" stroke="rgba(132,136,142,1)" stroke-width="1.2" />
          <line x1="11" y1="11" x2="14" y2="14" stroke="rgba(132,136,142,1)" stroke-width="1.2" stroke-linecap="round" />
        </svg>
      </div>
    </div>
 
    <div class="news-tabs">
      <button
        :class="['news-tab', { active: activeTab === 'local' }]"
        @click="$emit('update:activeTab', 'local')"
      > 智库报告</button>
      <button
        :class="['news-tab', { active: activeTab === 'capital' }]"
        @click="$emit('update:activeTab', 'capital')"
      > 调查项目</button>
      <button
        :class="['news-tab', { active: activeTab === 'hearing' }]"
        @click="$emit('update:activeTab', 'hearing')"
      > 听证会</button>
    </div>
 
    <div class="news-sort" ref="sortDropdownRef">
      <button class="news-sort-btn" @click="showSortDropdown = !showSortDropdown"> 
        <svg class="news-sort-icon" width="16" height="16" viewBox="0 0 16 16" fill="none">
          <rect x="4" y="3" width="2" height="10" rx="1" fill="rgba(95,101,108,1)" />
          <rect x="10" y="6" width="2" height="7" rx="1" fill="rgba(95,101,108,1)" />
        </svg>
        <span class="news-sort-label">{{ currentSortLabel }}</span> 
        <svg class="news-sort-arrow" :class="{ open: showSortDropdown }" width="11" height="6" viewBox="0 0 11 6" fill="none">
          <path d="M1 1l4.5 4L10 1" stroke="rgba(95,101,108,1)" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round" />
        </svg>
      </button>
      <div v-if="showSortDropdown" class="news-sort-dropdown">
        <button
          v-for="opt in sortOptions"
          :key="opt.value"
          @click="selectSort(opt.value)"
          class="news-sort-option"
          :class="{ active: sortBy === opt.value }"
        >{{ opt.label }}</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'

const props = defineProps({
  searchText: { type: String, default: '' },
  activeTab: { type: String, default: 'local' },
  sortBy: { type: String, default: 'publishTimeDesc' },
  sortOptions: {
    type: Array,
    default: () => [
      { value: 'publishTimeDesc', label: '发布时间倒序' },
      { value: 'publishTimeAsc', label: '发布时间正序' },
    ],
  },
})
const emit = defineEmits(['update:searchText', 'update:activeTab', 'update:sort'])

const showSortDropdown = ref(false)
const sortDropdownRef = ref(null)

const currentSortLabel = computed(() => {
  const opt = props.sortOptions.find((o) => o.value === props.sortBy)
  return opt ? opt.label : '发布时间倒序'
})

function selectSort(value) {
  emit('update:sort', value)
  showSortDropdown.value = false
}

function handleClickOutside(e) {
  if (sortDropdownRef.value && !sortDropdownRef.value.contains(e.target)) {
    showSortDropdown.value = false
  }
}

onMounted(() => document.addEventListener('click', handleClickOutside))
onUnmounted(() => document.removeEventListener('click', handleClickOutside))
</script>

<style scoped>
.news-topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  height: 32px;
  margin-bottom: 16px;
}
 
.news-search-wrapper {
  width: 360px;
  height: 32px;
  flex-shrink: 0;
}
.news-search-box {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 12px;
  padding: 5px 8px 5px 12px;
  border-radius: 4px;
  border: 1px solid rgba(230, 231, 232, 1);
  background: rgba(255, 255, 255, 1);
}
.news-search-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-size: 14px;
  font-family: 'Microsoft YaHei', sans-serif;
  font-weight: 400;
  line-height: 22px;
  color: rgba(59, 65, 75, 1);
}
.news-search-input::placeholder {
  color: rgba(132, 136, 142, 1);
}
.news-search-icon {
  flex-shrink: 0;
}
 
.news-tabs {
  display: flex;
  flex-direction: row;
  gap: 8px;
  justify-content: center;
  align-items: flex-start;
}
.news-tab {
  width: 120px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 24px;
  border-radius: 21px;
  font-size: 16px;
  font-family: 'Source Han Sans CN', sans-serif;
  font-weight: 400;
  line-height: 24px;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid rgba(234, 236, 238, 1);
  background: rgba(255, 255, 255, 1);
  color: rgba(95, 101, 108, 1);
}
.news-tab.active {
  background: rgba(5, 95, 194, 1);
  border-color: rgba(5, 95, 194, 1);
  color: rgba(255, 255, 255, 1);
  font-weight: 700;
}
 
.news-sort {
  margin-left: auto;
  position: relative;
  flex-shrink: 0;
  margin-right: 10px;
}
.news-sort-btn {
  width: 150px;
  height: 32px;
  display: flex;
  align-items: center;
  padding: 0 12px 0 7px;
  gap: 0;
  border-radius: 4px;
  border: 1px solid rgba(230, 231, 232, 1);
  background: rgba(255, 255, 255, 1);
  cursor: pointer;
  box-sizing: border-box;
  margin-top: 15px;
}
.news-sort-btn:hover {
  border-color: rgba(5, 95, 194, 0.4);
}
.news-sort-icon {
  flex-shrink: 0;
  margin-right: 6px;
}
.news-sort-label {
  flex: 1;
  font-size: 14px;
  font-family: 'Microsoft YaHei', sans-serif;
  font-weight: 400;
  color: rgba(95, 101, 108, 1);
  white-space: nowrap;
}
.news-sort-arrow {
  flex-shrink: 0;
  transition: transform 0.2s;
}
.news-sort-arrow.open {
  transform: rotate(180deg);
}
.news-sort-dropdown {
  position: absolute;
  right: 0;
  top: 100%;
  margin-top: 4px;
  min-width: 120px;
  background: #fff;
  border: 1px solid rgba(234, 236, 238, 1);
  border-radius: 8px;
  box-shadow: 0px 0px 20px 0px rgba(25, 69, 130, 0.1);
  padding: 4px 0;
  z-index: 20;
}
.news-sort-option {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 12px;
  font-size: 14px;
  font-family: 'Microsoft YaHei', sans-serif;
  background: none;
  border: none;
  cursor: pointer;
  color: rgba(95, 101, 108, 1);
}
.news-sort-option:hover {
  background: rgba(247, 248, 249, 1);
}
.news-sort-option.active {
  color: rgba(5, 95, 194, 1);
  font-weight: 500;
}
</style>
