<template>
    <div style="width: 100%;">
        <el-input v-model="selectName" :readonly="readonly" :placeholder="placeholder">
            <el-button slot="append" icon="el-icon-search" @click="openUserSelect"></el-button>
        </el-input>

        <UserSelect ref="userSelectRef" :multiple="multiple" :data="selectData" @confirmCallBack="userSelectCallBack">
        </UserSelect>
    </div>
</template>

<script>
import { pointsDetail } from '@/api/pointsManage';
import UserSelect from './index.vue'
export default {
    data() {
        return {
            selectName: '',
            selectData: ''
        }
    },
    components: {
        UserSelect
    },
    props: {
        multiple: {
            type: Boolean,
            default: false
        },
        placeholder: {
            value: String,
            default: '请选择'
        },
        value: {
            value: [String, Number],
            default: ''
        },
        readonly: {
            type: Boolean,
            default: true
        },
    },
    watch: {
        value: {
            handler: function (val, oVal) {
                if (val) {
                    this.selectData = val;
                    this.handleUserChange(val)
                }
            }
        },
        deep: true,
        immediate: true
    },

    methods: {
        openUserSelect() {
            this.$refs.userSelectRef.openModal(this.selectData)
        },
        userSelectCallBack(data) {
            debugger
            if (data.length === 0) {
                this.selectName = undefined
                this.selectData = ''
            } else {
                this.selectName = data.map((item) => item.nickName).join(',');
                this.selectData = data.map((item) => item.userName).join(',');
            }
            this.$emit('update:value', this.selectData);
            this.$emit('getValue', this.selectData, this.selectName);
        },
        handleUserChange(val) {
            pointsDetail(val).then(res => {
                if (val && res.data) {
                    this.selectName = res.data.nickName;
                }
            })
        },
    }
}
</script>