const BookReason = {
  template: `
      <a-select
        :placeholder="placeholder"
        style="width:100%"
        :size="size"
        :id="id"
        allowClear
        :value="defaultVal"
        :showSearch="true"
        @change="handleChange"
        option-filter-prop="children"
        :dropdownMatchSelectWidth="false"
        :filter-option="filterOption">
        <a-select-option v-for="item in list" :key="item.code" :value="item.code">
          {{item.dispName}}
        </a-select-option>
      </a-select>
    `,
  props: {
    value: {
      type: String,
      default: undefined
    },
    id: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择收款事由'
    },
    size: {
      type: String,
      default: 'default'
    }
  },
  data() {
    return {
      defaultVal: this.value,
      list: []
    };
  },
  created() {
    this.list = this.$store.state.app.bookReason
  },
  watch: {
    value(newValue, oldValue) {
      this.defaultVal = newValue || undefined
    }
  },
  methods: {
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      );
    },
    handleChange(value) {
      this.defaultVal = value;
      const row = this.list.find(item => item.code == value)
      this.$emit('change', value, row);
      this.$emit('input', value);
    },
    getCodeByName(value){
      const row = this.list.find(item => item.dispName == value)
      return row? row.code : ''
    }
  },
};

export default BookReason