<template>
  <a-select
    show-search
    label-in-value
    :value="dealerName"
    placeholder="请输入名称搜索"
    style="width: 100%"
    :filter-option="false"
    :not-found-content="fetching ? undefined : null"
    mode="multiple"
    :disabled="!disabled?true:false"
    @search="fetchUser"
    @change="handleChange"
    id="orglist-input"
    allowClear
  >
    <a-spin v-if="fetching" slot="notFoundContent" size="small" />
    <a-select-option v-for="item in data" :id="'orglist-'+item.sn" :key="item.sn" :value="item.sn" :disabled="disabled==item.sn || item.status=='0'">
      {{ item.name }}
    </a-select-option>
  </a-select>
</template>
<script>
import debounce from 'lodash/debounce'
import { jsxorgList } from '@/api/jxsorg'
export default {
  props: {
    disabled: {
      type: [String, Number],
      default: ''
    },
    params: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    this.lastFetchId = 0
    this.fetchUser = debounce(this.fetchUser, 800)
    return {
      data: [],
      dealerName: [],
      fetching: false
    }
  },
  methods: {
    fetchUser (dealerName) {
      console.log('fetching user', dealerName)
      if (dealerName == '') return
      this.lastFetchId += 1
      const fetchId = this.lastFetchId
      this.data = []
      this.fetching = true
      jsxorgList(Object.assign({ 'name': dealerName, pageNo: 1, pageSize: 20 }, this.params)).then(res => {
        if (fetchId !== this.lastFetchId) {
          return
        }
        this.data = res.data && res.data.list ? res.data.list : []
        this.fetching = false
      })
    },
    handleChange (value) {
      Object.assign(this, {
        dealerName: value,
        data: [],
        fetching: false
      })
      this.$emit('change', value || { key: undefined })
    },
    resetForm () {
      this.dealerName = []
      this.data = []
    },
    setData (val, list) {
      this.data = list
      this.dealerName = val
    }
  }
}
</script>
