<script setup>
import { useSlots, onMounted, ref, computed, watch } from "vue";
import { Icon } from '@iconify/vue';
import { t } from '@/shared/i18n';

const props = defineProps(["df", "read_only", "modelValue", "no_label"]);
let emit = defineEmits(["update:modelValue"]);
let slots = useSlots();

let select = ref(null);

function getOptions() {
  let options = props.df.options;

  if (typeof options === "string") {
    options = options.split("\n") || "";
    options = options.map((opt) => {
      return { label: opt, value: opt };
    });
  }

  if (options?.length && typeof options[0] === "string") {
    options = options.map((opt) => {
      return { label: opt, value: opt };
    });
  }

  if (props.df.sort_options) {
    options.sort((a, b) => a.label.localeCompare(b.label));
  }

  return options || [];
}

let options = computed(() => getOptions());

function handleChange(event) {
  emit("update:modelValue", event.target.value);
}
</script>

<template>
  <div v-if="slots.label" class="control form-builder-control" :class="{ editable: slots.label }">
    <!-- label -->
    <div class="field-controls">
      <slot name="label" />
      <slot name="actions" />
    </div>

    <!-- select input -->
    <div class="select-input">
      <input class="form-control" readonly />
      <Icon icon="mdi:chevron-down" class="select-icon" />
    </div>

    <!-- description -->
    <div v-if="df.description" class="mt-2 description" v-html="df.description"></div>
  </div>
  <div v-else class="control">
    <div v-if="!no_label" class="control-label label">{{ df.label }}</div>
    <div class="select-input">
      <select 
        class="form-control" 
        :value="modelValue" 
        @change="handleChange"
        :disabled="read_only || df.read_only"
      >
        <option v-for="opt in options" :key="opt.value" :value="opt.value">
          {{ opt.label }}
        </option>
      </select>
      <Icon icon="mdi:chevron-down" class="select-icon" />
    </div>
    <div v-if="df.description" class="mt-2 description" v-html="df.description"></div>
  </div>
</template>

<style lang="scss" scoped>
.control {
  .control-label {
    font-size: 12px;
    font-weight: 500;
    color: #374151;
    margin-bottom: 4px;
  }
  
  .form-control {
    display: block;
    width: 100%;
    padding: 6px 12px;
    padding-right: 30px;
    font-size: 14px;
    line-height: 1.5;
    color: #374151;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    appearance: none;
    
    &:focus {
      outline: none;
      border-color: #3b82f6;
      box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
    }
    
    &:disabled {
      background-color: #f9fafb;
      color: #9ca3af;
      cursor: not-allowed;
    }
  }
  
  .description {
    font-size: 12px;
    color: #6b7280;
  }
}

.editable {
  .select-icon {
    top: 8px !important;
  }
}

.select-input {
  position: relative;

  .select-icon {
    position: absolute;
    pointer-events: none;
    top: 50%;
    transform: translateY(-50%);
    right: 10px;
    color: #6b7280;
    width: 16px;
    height: 16px;
  }
}

.mt-2 {
  margin-top: 0.5rem;
}
</style>
