<template>
    <el-select v-model="collectionId" filterable :filter-method="filterCollection">
        <el-option v-for="item in filterOptions" :label="item.name" :value="item.id" :key="item.id">
            <span style="float: left">{{ item.name }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">#{{ item.id }}</span>
        </el-option>
    </el-select>
</template>
<script>
export default {
    props: ['value'],
    created() {
        this.collectionId = this.value;
        this.$http
            .post(
                '/collection/all',
                {
                    size: 10000,
                    sort: 'sort,desc;createdAt,desc',
                    query: { del: false, source: 'OFFICIAL', type: 'all' }
                },
                { body: 'json' }
            )
            .then(res => {
                this.filterOptions = this.options = res.content;
            });
    },
    data() {
        return {
            options: [],
            filterOptions: [],
            collectionId: null,
            selected: null
        };
    },
    methods: {
        filterCollection(val) {
            this.filterOptions = this.options.filter(i => i.name.includes(val) || i.id == val);
        }
    },
    watch: {
        collectionId(val) {
            this.$emit('input', val);
            if (val) {
                this.selected = this.options.find(i => i.id === val);
            }
            setTimeout(() => {
                this.filterOptions = [...this.options];
            }, 200);
        },
        value(val) {
            this.collectionId = val;
        }
    }
};
</script>