<!-- 身份证input -->
<template>
  <el-input v-model="inputVal" :maxlength="maxLen" type="text" :disabled="disabled" :placeholder="placeholder" @input="handleInput" />
</template>

<script>
/**
 * usage: <cy-card-input v-model="ruleForm.testVal" />
 */
export default {
  props: {
    maxLen: {
      type: Number,
      default: 18
    },
    placeholder: {
      type: String,
      default: '请输入文字'
    },
    value: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      inputVal: ''
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val && val !== '') {
          this.handleInput(String(val))
        }
      },
      immediate: true
    },
    inputVal(val) {
      this.handleInput(val)
    }
  },
  methods: {
    handleInput(val) {
      // 过滤非法字符，只保留数字和最后一位可能的 X 或 x
      this.inputVal = val.replace(/[^\dxX]/g, '')
      this.$emit('input', this.inputVal)
    }
  }
}
</script>

<style lang="scss" scoped></style>
