<!-- Used as Code, HTML Editor, Markdown Editor & JSON Control -->
<script setup>
import { computed, onMounted, ref, useSlots } from "vue";

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

let codeValue = ref(props.modelValue || '');

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

<template>
  <div class="control" :class="{ editable: slots.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>
    
    <div class="code-editor">
      <textarea
        class="code-textarea form-control"
        :style="{ maxHeight: df.max_height ?? '300px' }"
        :value="modelValue"
        :disabled="read_only || df.read_only || !!slots.label"
        :readonly="!!slots.label"
        @input="handleInput"
        spellcheck="false"
      />
    </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;
  }
  
  .code-editor {
    .code-textarea {
      font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', monospace;
      font-size: 13px;
      line-height: 1.5;
      min-height: 150px;
      resize: vertical;
      white-space: pre;
      overflow-x: auto;
    }
  }
  
  .form-control {
    display: block;
    width: 100%;
    padding: 8px 12px;
    font-size: 14px;
    line-height: 1.5;
    color: #374151;
    background-color: #f8fafc;
    background-clip: padding-box;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    
    &:focus {
      outline: none;
      border-color: #3b82f6;
      box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
    }
    
    &:disabled, &:read-only {
      background-color: #f1f5f9;
      color: #64748b;
      cursor: not-allowed;
    }
  }
  
  .description {
    font-size: 12px;
    color: #6b7280;
  }
}

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