<template>
    <el-select
        v-model="index"
        filterable
        :clearable="clearable"
        remote
        reserve-keyword
        placeholder="请选择"
        :loading="loading">
        <el-option
        v-for="(item, index) in list"
        :key="item.id"
        :label="`${item.nickname}`"
        :value="index">
        </el-option>
    </el-select>
</template>
<script>
import {getCustomerContactsSelect, getCustomerContacts} from '@/api/ecw/customerContacts'
import {listServiceUser, listSimpleUsers} from '@/api/system/user'
export default {
    props:{
        value: [String, Number],
        clearable: Boolean,
        manage:{
          type:Boolean,
          default:false
        }
    },
    data(){
        return {
            index: null,
            list:[],
            loading: false,
            size: 20
        }
    },
    watch:{
        index(val){
            this.$emit('input', val !== null && val !== undefined && val != '' ? this.list[val].id : null)
            this.$emit('change', val !== null && val !== undefined && val != '' ? this.list[val]: null)
        },
        value(val){
            this.resetIndex()
        },
        list(){
            this.resetIndex()
        }
    },
    created(){
      if(this.manage){
        // 客户经理
        listServiceUser().then(r=>{
          this.list = r.data;
        })
      }else {
        // 全部客户
        listSimpleUsers().then(res => {
          this.list = res.data
        })
      }

        // this.init()
    },
    methods:{
        resetIndex(){
            let index = this.list.findIndex(item => item.id == this.value)
            if(index < 0) this.index = null
            else this.index = index
        }
        /* init(){
            console.log('初始化联系人选择', this.value)
            if(!this.value) return
            let index = this.list.findIndex(item => item.customerContactsId == this.value)
            if(index < 0){
                getCustomerContactsSelect({ids: this.value}).then(res => {
                    if(!res.data || !res.data.length){
                        return this.$message.error('联系人信息获取失败')
                    }
                    this.list.unshift(res.data[0])
                    this.index = 0
                })
            }
        }, */
        /* remoteMethod(keyword){
            let params = {
                size: this.size
            }
            params.searchKey = keyword
            this.loading = true
            getCustomerContactsSelect(params)
                .then(res => this.list = res.data)
                .finally(() => this.loading = false)
        } */
    }
}
</script>
