<template>
    <div>
        <el-select
            v-model="index"
            filterable
            clearable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
            v-for="(item, index) in list"
            :key="item.id"
            :label="`${item.contactsName}`"
            :value="index">
            </el-option>
        </el-select>
        <el-button v-if="quickable" type="text" @click="showQuickCreate=true" class="ml-10">快速新建</el-button>

        <quick-create v-if="showQuickCreate" @success="onQuickCreateSuccess" @close="showQuickCreate=false"></quick-create>
    </div>
</template>
<script>
import {getCustomerContactsSelect, getCustomerContactsListByCustomer} from '@/api/ecw/customerContacts'
import QuickCreate from '@/views/ecw/customer/components/quickCreate'

export default {
    components:{QuickCreate},
    props:{
        value: [String, Number],
        quickable: {
            type: Boolean,
            default: true
        }
    },
    data(){
        return {
            index: null,
            list:[],
            loading: false,
            size: 20,
            showQuickCreate: false
        }
    },
    watch:{
        index(val){
            this.$emit('input', val !== null ? this.list[val].customerContactsId: null)
            this.$emit('change', val !== null ? this.list[val]: null)
        },
        value(val){
            this.init()
        }
    },
    created(){
        this.init()
    },
    methods:{
        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)
        },
        onQuickCreateSuccess(id){
            this.showQuickCreate = false
            getCustomerContactsListByCustomer({customerId: id}).then(res => {
                if(!res.data || !res.data.length){
                    return this.$message.error('联系人信息获取失败')
                }
                let data = res.data[0]
                data.contactsName = data.name // 字段名跟getCustomerContactsSelect对齐
                this.list.unshift(data)
                this.index = 0
            })
        }
    }
}
</script>