<!-- Used as Autocomplete, Barcode, Color, Currency, Data, Date, Duration, Link, Dynamic Link, Float, Int, Password, Percent, Time, Read Only, HTML Control -->
<script setup>
import { ref, useSlots } from "vue";
import { t } from '@/shared/i18n';

const props = defineProps(["df", "value", "read_only"]);
let slots = useSlots();
let timeZone = ref("");
let placeholder = ref("");

if (props.df.fieldtype === "Datetime") {
  timeZone.value = Intl.DateTimeFormat().resolvedOptions().timeZone;
}

if (props.df.fieldtype === "Color") {
  placeholder.value = t("Choose a color");
}
if (props.df.fieldtype === "Icon") {
  placeholder.value = t("Choose an icon");
}
</script>

<template>
  <div class="control form-builder-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" :class="{ reqd: df.reqd }">{{ df.label }}</div>

    <!-- data input -->
    <input
      v-if="slots.label"
      class="form-control"
      type="text"
      :style="{ height: df.fieldtype == 'Table MultiSelect' ? '42px' : '' }"
      :placeholder="placeholder"
      readonly
    />
    <input
      v-else
      class="form-control"
      type="text"
      :value="value"
      :disabled="read_only || df.read_only"
      @input="(event) => $emit('update:modelValue', event.target.value)"
    />
    <input
      v-if="slots.label && df.fieldtype === 'Barcode'"
      class="mt-2 form-control"
      type="text"
      :style="{ height: '110px' }"
      readonly
    />

    <!-- description -->
    <div v-if="df.description" class="mt-2 description" v-html="df.description" />

    <!-- timezone for datetime field -->
    <div
      v-if="timeZone"
      :class="['time-zone', !df.description ? 'mt-2' : '']"
      v-html="timeZone"
    />

    <!-- color selector icon -->
    <div class="selected-color no-value" />
  </div>
</template>

<style lang="scss" scoped>
.control {
  &.form-builder-control {
    .control-label {
      font-size: 12px;
      font-weight: 500;
      color: #374151;
      margin-bottom: 4px;
      
      &.reqd::after {
        content: " *";
        color: #ef4444;
      }
    }
    
    .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;
      transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
      
      &: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;
    }
    
    .time-zone {
      font-size: 12px;
      color: #6b7280;
    }
  }
}

.selected-color {
  background-color: transparent;
  top: 30px !important;
}

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