<template>
<el-select v-bind="$attrs" remote-show-suffix clearable remote :remote-method="querySearchAsync" :loading="loading">
	<el-option v-for="item in options" :key="item[props.value]" :label="item[props.label]" :value="item[props.value]"/>
</el-select>
</template>

<script>
export default{
	props: {
		apiObj: { type: Object, default: () => {} },
		props: { type: Object, default: () => {return {label: 'name', value: 'id'}} }
	},
	data(){
		return {
			loading: false,
			options: []
		}
	},
	created() {
		//如果有默认值就去请求接口获取options
		if(this.hasValue()){
			this.initloading = true
		}
	},
	methods: {
		async querySearchAsync(queryString){
			let params = {}
			params[this.props.value] = queryString
			let res = await this.apiObj.get(params)
			if(res.code == 1){
				this.options = res.data
				this.loading = false
			}
		},
		//判断是否有回显默认值
		hasValue(){
			if(Array.isArray(this.$attrs.modelValue) && this.$attrs.modelValue.length <= 0){
				return false
			}else if(this.$attrs.modelValue){
				this.querySearchAsync(this.$attrs.modelValue)
				return true
			}else{
				return false
			}
		}
	}
}
</script>