<template>
  <header class="topbar"> 
    <div class="topbar-search">
      <input
        type="text"
        :value="keyword"
        @input="$emit('update:keyword', $event.target.value)"
        placeholder="搜索法案"
        class="topbar-search-input"
      />
      <svg class="topbar-search-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <circle cx="11" cy="11" r="8" />
        <line x1="21" y1="21" x2="16.65" y2="16.65" />
      </svg>
    </div>
 
    <div class="topbar-spacer"></div>
 
    <div class="topbar-sort" ref="sortDropdownRef">
      <button class="topbar-sort-btn" @click="showSortDropdown = !showSortDropdown"> 
        <svg class="topbar-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="topbar-sort-label">{{ currentSortLabel }}</span> 
        <svg class="topbar-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="topbar-sort-dropdown">
        <button
          v-for="opt in sortOptions"
          :key="opt.value"
          @click="selectSort(opt.value)"
          class="topbar-sort-option"
          :class="{ active: sortBy === opt.value }"
        >
          {{ opt.label }}
        </button>
      </div>
    </div>
 
    <label class="topbar-checkbox-label">
      <input
        type="checkbox"
        :checked="chinaRelatedOnly"
        @change="$emit('toggle-china-related')"
        class="topbar-checkbox"
      />
      <span>只看涉华法案</span>
    </label>
  </header>
</template>

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

const props = defineProps({
  keyword: { type: String, default: '' },
  chinaRelatedOnly: { type: Boolean, default: true },
  sortBy: { type: String, default: 'publishTimeDesc' },
  sortOptions: { type: Array, default: () => [] },
})

const emit = defineEmits(['update:keyword', 'toggle-china-related', '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>
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.topbar-search {
  position: relative;
  flex: 1;
  min-width: 180px;
  max-width: 400px;
}

.topbar-search-input {
  width: 100%;
  height: 32px;
  padding: 0 32px 0 12px;
  font-size: 14px;
  border: 1px solid rgba(230, 231, 232, 1);
  border-radius: 4px;
  outline: none;
  color: rgba(59, 65, 75, 1);
  background: #fff;
  box-sizing: border-box;
  line-height: 22px;
}

.topbar-search-input::placeholder {
  color: rgba(132, 136, 142, 1);
}

.topbar-search-input:focus {
  border-color: rgba(5, 95, 194, 1);
  box-shadow: 0 0 0 2px rgba(5, 95, 194, 0.1);
}

.topbar-search-icon {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: rgba(132, 136, 142, 1);
  pointer-events: none;
}

.topbar-spacer {
  flex: 1;
}

.topbar-sort {
  position: relative;
  flex-shrink: 0;
}

.topbar-sort-btn {
  width: 170px;
  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;
}
.topbar-sort-btn:hover {
  border-color: rgba(5, 95, 194, 0.4);
}
.topbar-sort-icon {
  flex-shrink: 0;
  margin-right: 6px;
}
.topbar-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;
}
.topbar-sort-arrow {
  flex-shrink: 0;
  transition: transform 0.2s;
}
.topbar-sort-arrow.open {
  transform: rotate(180deg);
}

.topbar-sort-dropdown {
  position: absolute;
  right: 0;
  top: 100%;
  margin-top: 4px;
  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;
  min-width: 140px;
}

.topbar-sort-option {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 12px;
  font-size: 14px;
  background: none;
  border: none;
  cursor: pointer;
  color: rgba(95, 101, 108, 1);
}

.topbar-sort-option:hover {
  background: rgba(247, 248, 249, 1);
}

.topbar-sort-option.active {
  color: rgba(5, 95, 194, 1);
  font-weight: 500;
}

.topbar-checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 400;
  color: rgba(132, 136, 142, 1);
  white-space: nowrap;
  line-height: 24px;
}

.topbar-checkbox {
  width: 16px;
  height: 16px;
  accent-color: rgba(5, 95, 194, 1);
  cursor: pointer;
}
</style>
