<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">
    <ul v-if="options.length" class="ss-list">
      <li v-for="o in options" :key="o.value">
        <button
          type="button" class="ss-opt" :class="{ active: o.value === modelValue }"
          @click="pick(o)"
        >
          <span>{{ o.label }}</span>
          <span v-if="o.value === modelValue" class="ss-check">✓</span>
        </button>
      </li>
    </ul>
    <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;
}
.ss-list {
  list-style: none;
  margin: 0;
  padding: 0;
}
.ss-opt {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0.85rem 0.4rem;
  border: 0;
  border-bottom: 1px solid var(--color-border);
  background: transparent;
  color: var(--color-text);
  font-size: 0.95rem;
  cursor: pointer;
  text-align: left;
}
.ss-opt.active {
  color: hsla(160, 100%, 37%, 1);
  font-weight: 700;
}
.ss-check {
  color: hsla(160, 100%, 37%, 1);
}
.ss-empty {
  padding: 1.2rem 0.4rem;
  opacity: 0.6;
  text-align: center;
}
</style>
