import { pickUpLogisticsList } from '@/api/pickUp'
const LogisticsCompany = {
  template: `
      <a-select
          allowClear
          :size="size"
          mode="combobox"
          :value="defaultVal"
          show-search
          :placeholder="placeholder"
          option-filter-prop="children"
          style="width: 100%"
          :filter-option="filterOption"
          :dropdownMatchSelectWidth="false"
          @change="onChange"
          @blur="onBlur"
          @focus="open=true"
          @select="open=false"
          :open="open"
        >
          <a-select-option v-for="item in list" :key="item.id" :value="item.logisticsCompany">
            {{ item.logisticsCompany }}
          </a-select-option>
        </a-select>
    `,
  props: {
    value: {
      type: String,
      defatut: ''
    },
    id: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择或输入物流公司(最多50字符)'
    },
    size: {
      type: String,
      default: 'default'
    }
  },
  data() {
    return {
      defaultVal: this.value,
      list: [],
      open: false,
    };
  },
  mounted() {
    this.getList()
  },
  watch: {
    value(newValue, oldValue) {
      this.defaultVal = newValue
    },
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    onChange(value) {
      const ret = value?value.substr(0,50):''
      this.defaultVal = ret;
      this.$emit('change', ret);
      this.$emit('input', ret);
    },
    onBlur(value){
      this.open = false;
      this.$emit('blur', value);
    },
    getList () {
      pickUpLogisticsList({}).then(res => {
        if (res.status == 200) {
          this.list = res.data
        } else {
          this.list = []
        }
      })
    },
    getPoint(v){
      const row = this.list.find(item => item.logisticsCompany == v)
      return row ? row.logisticsPointList : []
    }
  },
};

export default LogisticsCompany