<template>
  <el-select
    v-model="_value"
    :size="size" 
    remote
    :remote-method="onFetch"
    :placeholder="placeholder || '请选择客户公司'"
    :loading="loading"
    :disabled="disabled"
    reserve-keyword
    filterable
    clearable
  >
    <el-option 
      v-for="item in list" 
      :label="item.companyName" 
      :key="item[prop || 'id']" 
      :value="item[prop || 'id']" 
    />
  </el-select>
</template>


<script>
import asyncRequest from "@/apis/components/search"
export default {
  name:'SearchCustomer',
  props:['size', 'value' , 'placeholder', 'prop', 'disabled'],
  data(){
    return {
      loading: false,
      list: []
    }
  },
  computed:{
    _value:{
        get(){
          return this.value
        },
        set(newVal){
          const current = this.list.find(({id}) => id === newVal)
          this.$emit('update:value', newVal)
          this.$emit('change', current)
        }
    }
  },
  methods:{
    async init(param){
      const {code, data} = await asyncRequest.customer(param)
      if(code !== 1) return
      this.list = data.list
      this._value = data.list[0][this.prop]
    },
    async onFetch(keyword){
      const {code, data} = await asyncRequest.customer({companyName: keyword})
      if(code !== 1) return
      this.list = data.list
    }
  }
}
</script>
