<template>
  <!-- 卫星仓客户 -->
  <a-select
    show-search
    label-in-value
    :value="dealerName"
    placeholder="请输入名称搜索"
    style="width: 100%"
    :filter-option="false"
    :dropdownMatchSelectWidth="false"
    :not-found-content="fetching ? undefined : null"
    @search="fetchUser"
    @change="handleChange"
    allowClear
  >
    <a-spin v-if="fetching" slot="notFoundContent" size="small" />
    <a-select-option v-for="item in data" :key="item.customerSn" :value="item.customerSn">{{ item.customerName }}</a-select-option>
  </a-select>
</template>
<script>
import debounce from 'lodash/debounce'
import { custList } from '@/api/customer'
export default {
  props: {
    verify: { //  当前输入客户名称是否是卫星仓客户 提示信息
      type: Boolean,
      default: false
    }
  },
  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
      custList({ customerName: dealerName, satelliteFlag: 1, pageNo: 1, pageSize: 20 }).then(res => {
        if (fetchId !== this.lastFetchId) {
          return
        }
        this.data = res.data && res.data.list ? res.data.list : []
        if (this.verify && this.data.length == 0) {
          this.$message.info('客户不存在')
        }
        this.fetching = false
      })
    },
    handleChange (value) {
      Object.assign(this, {
        dealerName: value,
        data: [],
        fetching: false
      })
      this.$emit('change', value || { key: undefined, label: '' })
    },
    resetForm () {
      this.dealerName = []
    }
  }
}
</script>
