<!-- Used as Text, Small Text & Long Text Control -->
<script setup>
import { useSlots, computed } from "vue";

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

let height = computed(() => {
  if (props.df.fieldtype === "Small Text") {
    return "150px";
  }
  return "300px";
});
</script>

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

    <!-- textarea input -->
    <textarea
      v-if="slots.label"
      :style="{ height: height, maxHeight: df.max_height ?? '' }"
      class="form-control"
      type="text"
      readonly
    />
    <textarea
      v-else
      :style="{ height: height, maxHeight: df.max_height ?? '' }"
      class="form-control"
      type="text"
      :value="value"
      :disabled="read_only || df.read_only"
      @input="(event) => $emit('update:modelValue', event.target.value)"
    />

    <!-- description -->
    <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;
    font-size: 14px;
    line-height: 1.5;
    color: #374151;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    resize: vertical;
    
    &: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;
  }
}

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