<script setup lang="ts">
// 아바타 스택 + 오버플로(+N) + 클릭 팝오버 목록 공용 컴포넌트.
// 좁은 영역에서 다수의 사람(참석자/담당자 등)을 컴팩트하게 표시하고,
// 클릭하면 전체 목록(아바타 + 이름 + 선택적 보조문구 sub)을 펼친다.
import { computed, ref } from 'vue'
import UserAvatar from '@/components/UserAvatar.vue'
import { useClickOutside } from '@/composables/useClickOutside'

interface StackPerson {
  id: string
  name: string
  avatarUrl?: string | null
  // 팝오버 목록의 이름 아래 보조문구(예: 역할). 없으면 표시 안 함
  sub?: string | null
}
const props = withDefaults(
  defineProps<{ people: StackPerson[]; max?: number }>(),
  { max: 6 },
)

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

// max 초과 시 (max-1)개만 보이고 나머지는 +N
const shown = computed(() =>
  props.people.length > props.max
    ? props.people.slice(0, props.max - 1)
    : props.people,
)
const extra = computed(() =>
  props.people.length > props.max ? props.people.length - (props.max - 1) : 0,
)
</script>

<template>
  <div
    ref="rootRef"
    class="as"
  >
    <button
      type="button"
      class="as-trigger"
      :class="{ open }"
      @click="open = !open"
    >
      <span class="as-stack">
        <span
          v-for="p in shown"
          :key="p.id"
          class="as-av"
        ><UserAvatar
          :url="p.avatarUrl"
          :name="p.name"
        /></span>
        <span
          v-if="extra > 0"
          class="as-more"
        >+{{ extra }}</span>
      </span>
      <span class="as-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="as-panel"
    >
      <div
        v-for="p in people"
        :key="p.id"
        class="as-prow"
      >
        <span class="as-av sm"><UserAvatar
          :url="p.avatarUrl"
          :name="p.name"
        /></span>
        <span class="as-pinfo">
          <span class="as-pnm">{{ p.name }}</span>
          <span
            v-if="p.sub"
            class="as-psub"
          >{{ p.sub }}</span>
        </span>
      </div>
    </div>
  </div>
</template>

<style scoped>
.as {
  position: relative;
  display: inline-block;
}
.as-trigger {
  display: inline-flex;
  align-items: center;
  gap: 0.5625rem;
  border: none;
  background: none;
  cursor: pointer;
  padding: 0.3125rem 0.5rem 0.3125rem 0.3125rem;
  margin: -0.3125rem -0.5rem -0.3125rem -0.3125rem;
  border-radius: 0.5625rem;
}
.as-trigger:hover,
.as-trigger.open {
  background: #f1f2f4;
}
.as-stack {
  display: inline-flex;
  align-items: center;
}
.as-av {
  width: 1.875rem;
  height: 1.875rem;
  border-radius: 50%;
  flex-shrink: 0;
}
.as-stack .as-av,
.as-stack .as-more {
  margin-left: -0.5625rem;
  box-shadow: 0 0 0 2px var(--panel);
}
.as-stack .as-av:first-child {
  margin-left: 0;
}
.as-more {
  width: 1.875rem;
  height: 1.875rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  font-weight: 700;
  background: #eceef1;
  color: var(--text-2);
  flex-shrink: 0;
}
.as-chev {
  color: var(--text-3);
  display: grid;
  transition: transform 0.15s ease;
}
.as-chev svg {
  width: 0.9375rem;
  height: 0.9375rem;
}
.as-trigger.open .as-chev {
  transform: rotate(180deg);
}
.as-panel {
  position: absolute;
  top: calc(100% + 0.375rem);
  left: 0;
  min-width: 12.5rem;
  max-width: 16.25rem;
  max-height: 15rem;
  overflow-y: auto;
  z-index: 20;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 0.625rem;
  box-shadow: var(--shadow-pop);
  padding: 0.3125rem;
}
.as-prow {
  display: flex;
  align-items: center;
  gap: 0.5625rem;
  padding: 0.375rem 0.5rem;
  border-radius: 0.375rem;
  white-space: nowrap;
}
.as-prow:hover {
  background: #f4f5f7;
}
.as-av.sm {
  width: 1.5rem;
  height: 1.5rem;
}
.as-pinfo {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.as-pnm {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
}
.as-psub {
  font-size: 0.719rem;
  color: var(--text-3);
}
</style>
