<template>
  <el-input
    :model-value="modelValue"
    type="textarea"
    :placeholder="placeholder"
    :disabled="isDisabled"
    :readonly="readonly"
    :rows="rows"
    :maxlength="maxlength"
    :show-word-limit="showWordLimit"
    @update:model-value="handleChange"
    @blur="handleBlur"
    @focus="handleFocus"
  />
</template>

<script setup lang="ts">
import { computed } from 'vue'
import type { FieldConfig } from '@/types/form'

interface Props {
  modelValue: string
  field: FieldConfig
  disabled?: boolean
  readonly?: boolean
  rows?: number
  maxlength?: number
  showWordLimit?: boolean
}

interface Emits {
  (e: 'update:modelValue', value: string): void
  (e: 'blur', event: FocusEvent): void
  (e: 'focus', event: FocusEvent): void
}

const props = withDefaults(defineProps<Props>(), {
  rows: 3,
  showWordLimit: false
})

const emit = defineEmits<Emits>()

const placeholder = computed(() => props.field.placeholder || `请输入${props.field.label}`)

const isDisabled = computed(() => {
  if (props.disabled) return true
  if (typeof props.field.disabled === 'function') {
    return false
  }
  return props.field.disabled || false
})

const maxlength = computed(() => props.field.validationRules?.max)
const showWordLimit = computed(() => {
  return props.showWordLimit || !!maxlength.value
})

const handleChange = (value: string) => {
  emit('update:modelValue', value)
}

const handleBlur = (event: FocusEvent) => {
  emit('blur', event)
}

const handleFocus = (event: FocusEvent) => {
  emit('focus', event)
}
</script>

<style scoped lang="scss">
// 继承全局样式
</style>
