<script setup>
// 필드형 트리거를 누르면 바텀시트로 옵션을 보여주고 단일 선택. (계좌·카드 선택 등)
import { computed, ref } from 'vue'
import BottomSheet from './BottomSheet.vue'

const props = defineProps({
  modelValue: { type: [String, Number], default: '' },
  options: { type: Array, default: () => [] }, // [{ value, label }]
  placeholder: { type: String, default: '선택하세요' },
  title: { type: String, default: '선택' },
  disabled: { type: Boolean, default: false },
  emptyText: { type: String, default: '선택할 항목이 없습니다' },
})
const emit = defineEmits(['update:modelValue'])

const open = ref(false)
const selected = computed(() => props.options.find((o) => o.value === props.modelValue) || null)
function pick(o) {
  emit('update:modelValue', o.value)
  open.value = false
}
</script>

<template>
  <button
    type="button" class="ss-trigger" :class="{ empty: !selected }"
    :disabled="disabled" @click="open = true"
  >
    <span class="ss-label">{{ selected ? selected.label : placeholder }}</span>
    <span class="ss-caret">▾</span>
  </button>

  <BottomSheet v-model="open" :title="title">
    <div v-if="options.length" class="ss-chips">
      <button
        v-for="o in options" :key="o.value"
        type="button" class="ss-chip" :class="{ active: o.value === modelValue }"
        @click="pick(o)"
      >{{ o.label }}</button>
    </div>
    <p v-else class="ss-empty">{{ emptyText }}</p>
  </BottomSheet>
</template>

<style scoped>
.ss-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: var(--color-background-soft);
  color: var(--color-text);
  font-size: 0.9rem;
  cursor: pointer;
  text-align: left;
}
.ss-trigger:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.ss-trigger.empty .ss-label {
  opacity: 0.5;
}
.ss-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ss-caret {
  opacity: 0.5;
  font-size: 0.8rem;
  flex-shrink: 0;
}
/* CategoryPicker(.acc-chip)와 동일한 4열 균일 박스 그리드 */
.ss-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0.2rem 0 0.4rem;
}
.ss-chip {
  flex: 1 1 calc(25% - 0.35rem);
  min-width: 0;
  padding: 0.5rem 0.4rem;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: transparent;
  color: var(--color-text);
  font-size: 0.82rem;
  cursor: pointer;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ss-chip.active {
  border-color: hsla(160, 100%, 37%, 1);
  background: hsla(160, 100%, 37%, 0.1);
  color: hsla(160, 100%, 37%, 1);
  font-weight: 600;
}
.ss-empty {
  padding: 1.2rem 0.4rem;
  opacity: 0.6;
  text-align: center;
}
</style>
