<template>
  <el-select
    v-model="_value"
    :size="size" 
    remote
    :remote-method="onFetch"
    :placeholder="placeholder || '请选择城市'"
    :loading="loading"
    reserve-keyword
    filterable
    clearable
  >
    <el-option 
      v-for="item in list" 
      :key="item.value" 
      :value="item.value" 
      :label="item.label"
    />
  </el-select>
</template>


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