<template>
  <el-select
    v-model="_value"
    :size="size" 
    :remote-method="onFetch"
    placeholder="项目需求"
    :loading="loading"
    reserve-keyword
    filterable
    remote
    clearable
  >
    <el-option 
      v-for="item in list"
      :label="item.name" 
      :value="item.id" 
      :key="item.id" 
    />
  </el-select>
</template>


<script>
import asyncRequest from "@/apis/components/search"
export default {
  name:'SearchDemand',
  props:['size', 'value'],
  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 onFetch(keyword){
      const {code, data} = await asyncRequest.demand({name: keyword})
      if(code !== 1) return
      this.list = data.list
    }
  }
}
</script>

