<template>
  <div class="admin-table-filter admin-card mb-6 p-4">
    <div class="filter-main">
      <div v-if="showKeyword" class="filter-keyword">
        <div class="relative flex-1 min-w-[200px]">
          <input
            type="text"
            :value="keyword"
            class="admin-input w-full pl-10"
            :placeholder="keywordPlaceholder"
            @input="onKeywordInput"
            @keyup.enter="emit('search')"
          />
          <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="search-icon">
            <circle cx="11" cy="11" r="8"></circle>
            <path d="m21 21-4.35-4.35"></path>
          </svg>
          <button
            v-if="keyword"
            type="button"
            class="clear-btn"
            @click="clearKeyword"
          >
            ×
          </button>
        </div>
      </div>

      <div v-if="hasPrimaryFilters" class="filter-primary">
        <slot name="primaryFilters" />
      </div>

      <button
        v-if="hasFilters"
        type="button"
        class="filter-toggle admin-btn-secondary text-xs"
        @click="expanded = !expanded"
      >
        {{ expanded ? '收起筛选 ▲' : '展开筛选 ▼' }}
      </button>

      <div class="filter-actions">
        <button type="button" class="admin-btn-search" @click="emit('search')">查询</button>
        <button type="button" class="admin-btn-secondary opacity-70" @click="emit('reset')">重置</button>
      </div>
    </div>

    <Transition name="filter-panel">
      <div v-if="hasFilters && expanded" class="filter-panel">
        <slot name="filters" />
      </div>
    </Transition>
  </div>
</template>

<script setup lang="ts">
import { computed, ref, useSlots, watch } from 'vue'

const props = withDefaults(defineProps<{
  keyword?: string
  keywordPlaceholder?: string
  showKeyword?: boolean
  defaultExpanded?: boolean
}>(), {
  keyword: '',
  keywordPlaceholder: '关键词搜索...',
  showKeyword: true,
  defaultExpanded: false,
})

const emit = defineEmits<{
  'update:keyword': [value: string]
  search: []
  reset: []
}>()

const slots = useSlots()
const hasFilters = computed(() => !!slots.filters)
const hasPrimaryFilters = computed(() => !!slots.primaryFilters)
const expanded = ref(props.defaultExpanded)

watch(() => props.defaultExpanded, (value) => {
  expanded.value = value
})

const onKeywordInput = (e: Event) => {
  emit('update:keyword', (e.target as HTMLInputElement).value)
}

const clearKeyword = () => {
  emit('update:keyword', '')
  emit('search')
}
</script>

<style scoped>
.filter-main {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.75rem 1rem;
}

.filter-keyword {
  flex: 1 1 240px;
  min-width: 200px;
  max-width: 100%;
}

.filter-primary {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.75rem 1rem;
  flex: 0 0 auto;
}

.filter-toggle {
  flex-shrink: 0;
  white-space: nowrap;
}

.filter-actions {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
  margin-left: auto;
}

.filter-panel {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.75rem 1rem;
  width: 100%;
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.filter-panel-enter-active,
.filter-panel-leave-active {
  transition: opacity 0.2s ease, max-height 0.25s ease;
  overflow: hidden;
}

.filter-panel-enter-from,
.filter-panel-leave-to {
  opacity: 0;
  max-height: 0;
}

.filter-panel-enter-to,
.filter-panel-leave-from {
  opacity: 1;
  max-height: 500px;
}

.search-icon {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: rgba(255, 255, 255, 0.4);
  pointer-events: none;
}

.clear-btn {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 9999px;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.6);
  cursor: pointer;
  line-height: 1;
  font-size: 1rem;
}

.clear-btn:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.15);
}

:deep(.filter-field) {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 0 0 auto;
  flex-shrink: 0;
  min-width: 120px;
}

:deep(.filter-field label) {
  font-size: 0.75rem;
  color: rgba(255, 255, 255, 0.5);
  white-space: nowrap;
}
</style>
