import { shelfList } from '@/api/shelf'
const shelfSList = {
  template: `
      <a-select
        placeholder="请选择货架"
        :maxTagCount="3"
        :maxTagTextLength="mode=='default'?20:8"
        :mode="mode"
        :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 shelfList" :key="item.shelfSn" :value="item.shelfSn">{{ item.shelfName }}</a-select-option>
      </a-select>
    `,
  props: {
    value: {
      type: [String, Array, Object],
    },
    id: {
      type: String,
      default: ''
    },
    mode: {
      type: String,
      default: 'default'
    }
  },
  data () {
    return {
      defaultVal: this.value,
      shelfList: []
    }
  },
  mounted () {
    this.getShelf()
  },
  watch: {
    value (newValue, oldValue) {
      this.defaultVal = newValue
    }
  },
  methods: {
    filterOption (input, option) {
      console.log(input, option)
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleChange (value) {
      console.log(value)
      const row = this.shelfList.find(item => item.shelfSn == value)
      this.defaultVal = value
      this.$emit('input', value, row)
      this.$emit('change', value, row)
    },
    //  货架列表
    getShelf () {
      shelfList({ pageNo: 1, pageSize: 1000 }).then(res => {
        if (res.status == 200) {
          this.shelfList = res.data && res.data.list ? res.data.list : []
        } else {
          this.shelfList = []
        }
      })
    }
  }
}

export default shelfSList
