<script setup lang="ts">
// 유형 필터 + 카테고리 관리 드롭다운
import { computed, ref } from 'vue'
import { useClickOutside } from '@/composables/useClickOutside'
import type { ApiScheduleCategory } from '@/types/schedule'

const props = defineProps<{
  categories: ApiScheduleCategory[]
  counts: Record<string, number>
  activeTypeIds: Set<string>
}>()
const emit = defineEmits<{
  (e: 'toggle', id: string): void
  (e: 'set-all', on: boolean): void
  (e: 'add'): void
  (e: 'edit', id: string): void
  (e: 'delete', id: string): void
}>()

const open = ref(false)
const rootRef = ref<HTMLElement | null>(null)
useClickOutside(open, () => (open.value = false), [rootRef], { esc: false })

const ordered = computed(() =>
  [...props.categories].sort((a, b) => a.sortOrder - b.sortOrder),
)
const allOn = computed(
  () =>
    ordered.value.length > 0 &&
    ordered.value.every((c) => props.activeTypeIds.has(c.id)),
)
</script>

<template>
  <div
    ref="rootRef"
    class="filter"
  >
    <button
      type="button"
      class="filter-btn"
      :class="{ open }"
      @click="open = !open"
    >
      <span class="fic">
        <svg
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
        ><path d="M22 3H2l8 9.46V19l4 2v-8.54L22 3z" /></svg>
      </span>유형
      <span class="chev">
        <svg
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
        ><path d="m6 9 6 6 6-6" /></svg>
      </span>
    </button>
    <div
      v-if="open"
      class="filter-menu"
    >
      <div class="fm-head">
        <span>유형 · 카테고리</span>
        <button
          class="fm-all"
          @click="emit('set-all', !allOn)"
        >
          {{ allOn ? '전체 해제' : '전체 선택' }}
        </button>
      </div>
      <div class="fm-list">
        <div
          v-for="c in ordered"
          :key="c.id"
          class="fm-item"
          :class="{ on: activeTypeIds.has(c.id) }"
        >
          <button
            class="fm-tog"
            @click="emit('toggle', c.id)"
          >
            <span class="fm-check">
              <svg
                v-if="activeTypeIds.has(c.id)"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2.4"
                stroke-linecap="round"
                stroke-linejoin="round"
              ><path d="M20 6 9 17l-5-5" /></svg>
            </span>
            <span
              class="dot"
              :style="{ background: c.color }"
            />
            <span class="fm-label">{{ c.label }}</span>
            <span class="fm-ct">{{ counts[c.id] || 0 }}</span>
          </button>
          <span class="fm-acts">
            <button
              class="fm-act"
              title="편집"
              @click="open = false; emit('edit', c.id)"
            >
              <svg
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
              ><path d="M12 20h9M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4Z" /></svg>
            </button>
            <button
              class="fm-act"
              title="삭제"
              @click="open = false; emit('delete', c.id)"
            >
              <svg
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
              ><path d="M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2m2 0v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6" /><path d="M10 11v6M14 11v6" /></svg>
            </button>
          </span>
        </div>
      </div>
      <button
        class="fm-add"
        @click="open = false; emit('add')"
      >
        <svg
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2.2"
          stroke-linecap="round"
          stroke-linejoin="round"
        ><path d="M5 12h14M12 5v14" /></svg>새 카테고리
      </button>
    </div>
  </div>
</template>

<style scoped>
.filter {
  position: relative;
}
/* 트리거 — 기존 정렬/필터 드롭다운(DropdownSelect)과 동일한 pill 외형 */
.filter-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4375rem;
  height: 2.25rem;
  padding: 0 0.625rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: #fff;
  font-family: inherit;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-2);
  cursor: pointer;
  white-space: nowrap;
  line-height: 1;
}
.filter-btn:hover {
  background: #f9fafb;
}
.filter-btn.open {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-weak);
}
.filter-btn .fic {
  display: inline-flex;
  flex-shrink: 0;
  color: var(--text-3);
}
.filter-btn .fic svg {
  width: 0.9375rem;
  height: 0.9375rem;
}
.filter-btn .chev {
  display: inline-flex;
  flex-shrink: 0;
  color: var(--text-3);
  transition: transform 0.15s ease;
}
.filter-btn .chev svg {
  width: 0.875rem;
  height: 0.875rem;
}
.filter-btn.open .chev {
  transform: rotate(180deg);
}
.filter-menu {
  position: absolute;
  top: calc(100% + 0.4375rem);
  right: 0;
  z-index: 60;
  width: 16.5rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 0.625rem;
  box-shadow: var(--shadow-pop);
  padding: 0.375rem;
}
.fm-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.4375rem 0.5rem 0.5625rem;
}
.fm-head > span {
  font-size: 0.6875rem;
  font-weight: 700;
  color: var(--text-3);
  letter-spacing: 0.0187rem;
  white-space: nowrap;
}
.fm-all {
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
  cursor: pointer;
  padding: 0.125rem 0.3125rem;
  border-radius: 0.25rem;
  white-space: nowrap;
}
.fm-all:hover {
  background: var(--accent-weak);
}
.fm-list {
  display: flex;
  flex-direction: column;
  max-height: 20rem;
  overflow-y: auto;
}
.fm-item {
  display: flex;
  align-items: center;
  border-radius: 0.375rem;
}
.fm-item:hover {
  background: #f4f5f7;
}
.fm-tog {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.5625rem;
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 0.5rem;
  cursor: pointer;
  text-align: left;
}
.fm-tog .dot {
  width: 0.625rem;
  height: 0.625rem;
  border-radius: 0.1875rem;
  flex-shrink: 0;
}
.fm-tog .fm-label {
  flex: 1;
  min-width: 0;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.fm-tog .fm-ct {
  font-size: 0.6875rem;
  font-weight: 700;
  color: var(--text-3);
}
.fm-tog .fm-check {
  width: 1rem;
  height: 1rem;
  flex-shrink: 0;
  color: var(--accent);
  display: grid;
  place-items: center;
}
.fm-tog .fm-check svg {
  width: 0.9375rem;
  height: 0.9375rem;
}
.fm-item:not(.on) .fm-label {
  color: var(--text-3);
}
.fm-item:not(.on) .dot {
  background: #d7dae0 !important;
}
.fm-acts {
  display: none;
  gap: 0.0625rem;
  padding-right: 0.3125rem;
}
.fm-item:hover .fm-acts {
  display: flex;
}
.fm-act {
  width: 1.5625rem;
  height: 1.5625rem;
  border: none;
  background: transparent;
  color: var(--text-3);
  border-radius: 0.3125rem;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.fm-act:hover {
  background: #e3e5e9;
  color: var(--text);
}
.fm-act svg {
  width: 0.8125rem;
  height: 0.8125rem;
}
.fm-add {
  display: flex;
  align-items: center;
  gap: 0.4375rem;
  width: 100%;
  border: none;
  border-top: 1px solid var(--border);
  background: none;
  font-family: inherit;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--accent);
  padding: 0.625rem 0.5rem 0.375rem;
  margin-top: 0.25rem;
  cursor: pointer;
}
.fm-add:hover {
  color: var(--accent-hover);
}
.fm-add svg {
  width: 0.9375rem;
  height: 0.9375rem;
}
</style>
