<template>
  <a-select
    show-search
    label-in-value
    :size="size"
    :value="dealerName"
    :placeholder="placeholder"
    style="width: 100%"
    :filter-option="false"
    :not-found-content="fetching ? undefined : null"
    :dropdownMatchSelectWidth="false"
    @search="fetchUser"
    @change="handleChange"
    allowClear
  >
    <a-spin v-if="fetching" slot="notFoundContent" size="small" />
    <a-select-option v-for="item in data" :key="item.dealerSn" :value="item.dealerSn">
      {{ item.dealerName }}
    </a-select-option>
  </a-select>
</template>
<script>
import debounce from 'lodash/debounce'
import { dealerQueryList, dealerFindUpdateInfoBySn } from '@/api/dealer'
export default {
  props: {
    id: {
      type: String,
      default: ''
    },
    size: {
      type: String,
      default: 'default'
    },
    itemSn: {
      type: String || Number,
      default: undefined
    },
    placeholder: {
      type: String,
      default: '请输入名称搜索'
    },
    cooperateFlag: { // 合作关系
      type: String,
      default: undefined
    },
    dealerLevel: { // 商户级别
      type: String,
      default: undefined
    },
    // 过滤该类别1
    notDealerType: {
      type: Array,
      default: function () { return [] }
    },
    // 过滤该sn
    notDealerSn: {
      type: Array,
      default: function () { return [] }
    },
    // 默认有经销商范围权限
    dataAuthFlag: {
      type: [String, Number],
      default: 1
    }
  },
  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
      const params = { pageNo: 1, pageSize: 20, dataAuthFlag: this.dataAuthFlag }
      if (this.itemSn) {
        params.dealerSn = this.itemSn
      } else {
        params.nameLike = dealerName
        if (this.cooperateFlag) {
          params.cooperateFlag = this.cooperateFlag
        }
        if (this.dealerLevel) {
          params.dealerLevel = this.dealerLevel
        }
      }
      dealerQueryList(params).then(res => {
        if (fetchId !== this.lastFetchId) {
          return
        }
        const list = res.data && res.data.list ? res.data.list : []
        const notSn = []
        console.log(this.notDealerSn)
        this.notDealerSn.filter(item => !!item).map(item => { notSn.push(item.parentDealerSn) })
        this.data = list.filter(item => !notSn.includes(item.dealerSn)).filter(item => item.dealerLevel && !this.notDealerType.includes(item.dealerLevel))
        this.fetching = false
      })
    },
    handleChange (value) {
      console.log(value)
      if (value) {
        const row = this.data.filter(item => item.dealerSn == value.key)
        Object.assign(this, {
          dealerName: value,
          data: [],
          fetching: false
        })
        this.$emit('change', value || { key: undefined }, row[0], this.id)
        // this.$emit('input', value.key)
      } else {
        this.setDufaultVal('')
        this.resetForm()
        this.$emit('change', value || { key: undefined }, undefined, this.id)
        // this.$emit('input', value.key)
      }
    },
    resetForm () {
      this.dealerName = []
    },
    // 查询详细
    getDetail (sn) {
      dealerFindUpdateInfoBySn({ sn: sn }).then(res => {
        this.$emit('dealerDetail', res.data || null)
      })
    },
    setData (value) {
      Object.assign(this, {
        dealerName: value,
        data: [],
        fetching: false
      })
    },
    setDufaultVal (itemSn) {
      this.fetchUser(itemSn)
      this.setData({ key: itemSn })
    }
  },
  mounted () {
    if (this.itemSn) {
      this.fetchUser(this.itemSn)
      this.setData({ key: this.itemSn })
    }
  }
}
</script>
