<template>
  <select
    :id="id"
    :value="modelValue"
    :disabled="disabled"
    :required="required"
    :class="selectClasses"
    :aria-invalid="error ? 'true' : null"
    @change="onChange"
  >
    <option v-if="placeholder" value="" disabled :selected="!modelValue">
      {{ placeholder }}
    </option>
    <option
      v-for="option in options"
      :key="option.value"
      :value="option.value"
      :disabled="option.disabled"
    >
      {{ option.label }}
    </option>
  </select>
</template>

<script lang="ts">
import { defineComponent, computed, PropType } from 'vue';

interface SelectOption {
  value: string | number; // Or any, but string/number are most common for select values
  label: string;
  disabled?: boolean;
}

export default defineComponent({
  name: 'VSelect',
  props: {
    modelValue: {
      type: [String, Number] as PropType<string | number>,
      required: true,
    },
    options: {
      type: Array as PropType<SelectOption[]>,
      required: true,
      default: () => [],
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    required: {
      type: Boolean,
      default: false,
    },
    error: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
      default: null,
    },
    placeholder: {
      type: String,
      default: null,
    },
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const selectClasses = computed(() => [
      'form-input', // Re-use .form-input styles
      'select',     // Specific class for select styling (e.g., dropdown arrow)
      { 'error': props.error },
    ]);

    const onChange = (event: Event) => {
      const target = event.target as HTMLSelectElement;
      // Attempt to convert value to number if original option value was a number
      // This helps v-model work more intuitively with numeric values
      const selectedOption = props.options.find(opt => String(opt.value) === target.value);
      let valueToEmit: string | number = target.value;
      if (selectedOption && typeof selectedOption.value === 'number') {
        valueToEmit = parseFloat(target.value);
      }
      emit('update:modelValue', valueToEmit);
    };

    return {
      selectClasses,
      onChange,
    };
  },
});
</script>

<style lang="scss" scoped>
// Assume .form-input styles are available (globally or imported)
// For brevity, these are not repeated here but were defined in VInput/VTextarea.
// If they are not globally available, they should be added or imported.
.form-input {
  display: block;
  width: 100%;
  padding: 0.5em 0.75em; // Adjust padding for select, esp. right for arrow
  font-size: 1rem;
  font-family: inherit;
  line-height: 1.5;
  color: #212529;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #ced4da;
  border-radius: 0.25rem;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;

  &:focus {
    border-color: #80bdff;
    outline: 0;
    box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
  }

  &[disabled] {
    background-color: #e9ecef;
    opacity: 1;
    cursor: not-allowed;
  }

  &.error {
    border-color: #dc3545;
    &:focus {
      border-color: #dc3545;
      box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, 0.25);
    }
  }
}

.select {
  // Select-specific styling
  appearance: none; // Remove default system appearance
  // Custom dropdown arrow (often a ::after pseudo-element or background image)
  // Example using background SVG:
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23343a40' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 16px 12px;
  padding-right: 2.5rem; // Ensure space for the arrow

  // For placeholder option (disabled, selected)
  &:invalid, option[value=""][disabled] {
     color: #6c757d; // Placeholder text color
  }
  // Ensure that when a real value is selected, the color is the normal text color
  & option {
    color: #212529; // Or your default text color for options
  }
   // Fix for Firefox showing a lower opacity on disabled options
  & option:disabled {
    color: #adb5bd; // A lighter color for disabled options, but still readable
  }
}
</style>
