const ReturnReason = {
  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: 'small'
    },
    goodFlag: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      defaultVal: this.value,
      list: []
    };
  },
  created() {
    // 不良品
    if(this.goodFlag == 'DEFECTIVE_PRODUCT_RETURN'){
      this.list = this.$store.state.app.defectiveReturnReason
    }
    // 良品
    if(this.goodFlag == 'GOOD_PRODUCT_RETURN'){
      this.list = this.$store.state.app.goodReturnReason
    }
    // 全部
    if(!this.goodFlag){
      this.list = [...this.$store.state.app.defectiveReturnReason, ...this.$store.state.app.goodReturnReason]
    }
  },
  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);
    }
  },
};

export default ReturnReason