<template>
  <view :class="['input_content', props.className]">
    <input
      class="uni-input"
      :class="[props.classInputName]"
      v-model="valueModel"
      @input="onChange"
      :placeholder-style="props.placeholderStyle"
      :style="{
        width: props?.width || '300rpx',
        textAlign: props?.textAlign
      }"
      @change="onChange"
      :placeholder="props?.placeholder || '请输入'"
      :maxlength="props?.maxLength || 255"
      :type="props?.type || 'text'"
      @blur="onBlur"
      @confirm="onConfirm"
      :disabled="props?.disabled"
      :confirm-type="props?.confirmType"
      :password="props?.password || false"
    />
    <view v-if="valueModel?.length && !props.disabled" class="close" @click="emptyInput">
      <uni-icons type="closeempty" color="#999999" size="15" />
    </view>
    <view v-if="suffix" class="suffix">{{ suffix }}</view>
  </view>
</template>

<script setup lang="ts">
import { watch, ref, onMounted } from 'vue'
import { wlInputProps } from './reducer'
const valueModel = ref('')

// ======================= props & emit =======================
const props = defineProps(wlInputProps)
const emit = defineEmits(['update:valueModel', 'input', 'blur', 'bindconfirm', 'onClose'])

watch(
  () => props.valueModel,
  val => {
    valueModel.value = val
    handleChange(val)
  }
)

onMounted(() => {
  valueModel.value = props.valueModel
  handleChange(props.valueModel)
})
const onChange = (e: any) => {
  const target = e.target as HTMLInputElement
  let value = target.value
  handleChange(value)
}

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

const onBlur = () => {
  emit('blur', props.valueModel)
}

const onConfirm = (e: any) => {
  const target = e.target as HTMLInputElement
  let value = target.value
  emit('bindconfirm', value)
}

const emptyInput = () => {
  valueModel.value = ''
  onChange({ target: { value: '' } })
}
</script>
<style lang="scss">
.input_content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .uni-input {
    flex: 1;
    height: 70rpx;
  }
}
.suffix {
  color: #333333;
  font-size: 28rpx;
  margin-left: 20rpx;
}

.close {
  height: 70rpx;
  line-height: 70rpx;
  padding: 0 10rpx;
}
</style>
