<script setup lang="ts">
/**
 * KBX Select Component — v60
 */

export interface KbxSelectOption {
  value: string | number
  label: string
  disabled?: boolean
}

withDefaults(
  defineProps<{
    modelValue: string | number
    options: KbxSelectOption[]
    placeholder?: string
    label?: string
    error?: string
    required?: boolean
    disabled?: boolean
  }>(),
  {
    modelValue: '',
  }
)

defineEmits<{
  'update:modelValue': [value: string | number]
  change: []
}>()
</script>

<template>
  <div class="kbx-select-wrapper">
    <label v-if="label" class="kbx-select__label">
      {{ label }}
      <span v-if="required" class="kbx-select__required">*</span>
    </label>
    <select
      :value="modelValue"
      :disabled="disabled"
      :class="['kbx-select', { 'is-error': error }]"
      @change="$emit('update:modelValue', ($event.target as HTMLSelectElement).value); $emit('change')"
    >
      <option v-if="placeholder" value="">{{ placeholder }}</option>
      <option
        v-for="opt in options"
        :key="opt.value"
        :value="opt.value"
        :disabled="opt.disabled"
      >
        {{ opt.label }}
      </option>
    </select>
    <div v-if="error" class="kbx-select__error">{{ error }}</div>
  </div>
</template>

<style scoped>
.kbx-select-wrapper {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.kbx-select__label {
  font-size: 13px;
  font-weight: 500;
  color: var(--kbx-color-text, #000);
}

.kbx-select__required {
  color: var(--kbx-color-danger, #ef4444);
  margin-left: 2px;
}

.kbx-select {
  padding: 8px 12px;
  border: 1px solid var(--kbx-color-border, #e5e7eb);
  border-radius: 4px;
  font-size: 14px;
  background: var(--kbx-color-surface, #fff);
  color: var(--kbx-color-text, #000);
  cursor: pointer;
  min-height: 34px;
}

.kbx-select:focus {
  outline: none;
  border-color: var(--kbx-color-primary, #3b82f6);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

.kbx-select:disabled {
  background: var(--kbx-color-border, #e5e7eb);
  cursor: not-allowed;
  opacity: 0.6;
}

.kbx-select.is-error {
  border-color: var(--kbx-color-danger, #ef4444);
}

.kbx-select__error {
  font-size: 12px;
  color: var(--kbx-color-danger, #ef4444);
}

@media (prefers-color-scheme: dark) {
  .kbx-select__label {
    color: #f9fafb;
  }

  .kbx-select {
    background: #1f2937;
    border-color: #374151;
    color: #f9fafb;
  }
}
</style>
