<template>
  <BaseInputField
    v-model="value"
    :label="label"
    :placeholder="placeholder || '8080'"
    :error="validationError"
    :icon="icon || Network"
    type="number"
    v-bind="$attrs"
    @blur="validateOnBlur"
  />
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { z } from 'zod'
import { Network } from 'lucide-vue-next'
import BaseInputField from './BaseInputField.vue'

interface Props {
  modelValue: number
  label?: string
  placeholder?: string
  icon?: any
  required?: boolean
  validateOnBlur?: boolean
  min?: number
  max?: number
}

const props = withDefaults(defineProps<Props>(), {
  label: '端口',
  validateOnBlur: true,
  min: 1,
  max: 65535
})

const emit = defineEmits<{
  'update:modelValue': [value: number]
}>()

defineOptions({
  inheritAttrs: false
})

const hasBlurred = ref(false)

const value = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', Number(val))
})

// 端口验证模式
const portSchema = computed(() => 
  z.number()
    .int('端口必须是整数')
    .min(props.min, `端口必须大于等于${props.min}`)
    .max(props.max, `端口必须小于等于${props.max}`)
)

const validationError = computed(() => {
  // 如果是必填且为空
  if (props.required && (!props.modelValue && props.modelValue !== 0)) {
    return '请输入端口号'
  }
  
  // 如果有值但格式不正确，并且设置了在失焦时验证且已经失焦过
  if ((props.modelValue || props.modelValue === 0) && (!props.validateOnBlur || hasBlurred.value)) {
    const result = portSchema.value.safeParse(props.modelValue)
    return result.success ? '' : result.error.errors[0]?.message || '无效的端口号'
  }
  
  return ''
})

const validateOnBlur = () => {
  if (props.validateOnBlur) {
    hasBlurred.value = true
  }
}
</script>