<template>
  <section class="filters" :class="{ compact }" aria-label="筛选条件">
    <template v-if="!compact">
      <label class="filter-field">
        <span>法人主体</span>
        <EnterpriseSelect
          v-model="filters.entity"
          :options="entityOptions"
          placeholder="选择主体"
        />
      </label>

      <label class="filter-field">
        <span>费用类型</span>
        <EnterpriseSelect
          v-model="filters.category"
          :options="categoryOptions"
          placeholder="选择费用"
        />
      </label>

      <label class="filter-field">
        <span>风险等级</span>
        <EnterpriseSelect
          v-model="filters.risk"
          :options="riskOptions"
          placeholder="选择风险"
        />
      </label>
    </template>

    <div class="segmented-wrap" :class="{ compact }">
      <span v-if="!compact">时间范围</span>
      <div class="segmented" role="tablist" aria-label="处理视图">
        <button
          v-for="range in ranges"
          :key="range"
          :class="{ active: activeRange === range }"
          type="button"
          @click="emit('update:activeRange', range)"
        >
          {{ range }}
        </button>
      </div>
    </div>
  </section>
</template>

<script setup>
import EnterpriseSelect from '../shared/EnterpriseSelect.vue'

defineProps({
  filters: { type: Object, required: true },
  ranges: { type: Array, required: true },
  activeRange: { type: String, required: true },
  compact: { type: Boolean, default: false }
})

const emit = defineEmits(['update:activeRange'])

const entityOptions = [
  '全部主体',
  'Northstar China Ltd.',
  'Northstar Singapore Pte.',
  'Northstar US Inc.'
]

const categoryOptions = [
  '全部费用',
  '机票',
  '酒店',
  '火车/用车',
  '餐补及杂费'
]

const riskOptions = [
  '全部风险',
  '高风险',
  '需解释',
  '低风险'
]
</script>

<style scoped>
.filters {
  display: grid;
  grid-template-columns: repeat(3, minmax(160px, 1fr)) auto;
  gap: 12px;
  padding: 0 16px 12px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}

.filters.compact {
  display: flex;
  justify-content: flex-end;
  padding: 8px 16px;
}

.filter-field,
.segmented-wrap {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.segmented-wrap {
  justify-self: end;
}

.segmented {
  align-self: end;
  display: inline-flex;
  min-height: 36px;
  padding: 2px;
  border: 1px solid #d8dee8;
  border-radius: 4px;
  background: #f8fafc;
}

.segmented button {
  position: relative;
  min-height: 30px;
  padding: 0 16px;
  border: none;
  border-radius: 3px;
  background: transparent;
  color: #64748b;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition:
    background 160ms var(--ease),
    color 160ms var(--ease),
    box-shadow 160ms var(--ease);
  white-space: nowrap;
}

.segmented button:hover:not(.active) {
  color: #334155;
  background: #fff;
}

.segmented button.active {
  background: #fff;
  color: var(--primary-active);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

@media (max-width: 980px) {
  .filters {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .segmented-wrap {
    justify-self: start;
  }
}

@media (max-width: 760px) {
  .filters {
    grid-template-columns: 1fr;
    padding: 0 16px 16px;
  }

  .segmented {
    width: 100%;
  }
}
</style>
