<template>
  <el-date-picker
    :model-value="modelValue"
    type="date"
    :placeholder="placeholder"
    :disabled="isDisabled"
    :readonly="readonly"
    :clearable="clearable"
    value-format="YYYY-MM-DD"
    @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
  clearable?: boolean
}

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

const props = withDefaults(defineProps<Props>(), {
  clearable: true
})

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 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">
.el-date-picker {
  width: 100%;
}
</style>
