<template>
    <div class="control" @click="onInputerClick">
        <div class="component select" v-click-outside="onOutside">
            <div class="place" :class="{ placeholder: !modelValue.length }">
                <ul v-if="modelValue.length">
                    <li v-for="item in modelValue" @click.stop>
                        <span>{{ item.text || $t('tag.unkownUser') }}</span
                        ><i class="iconfont icon-close" @click="onselecterChange(item)"></i>
                    </li>
                </ul>
                <div v-else>{{ placeholder }}</div>
            </div>
            <div class="icon" :class="{ up: selecterShow }">
                <i class="iconfont icon-arrows_down"></i>
            </div>
            <div class="panel" v-show="selecterShow">
                <ul>
                    <li v-for="item in data" @click.stop="onselecterChange(item)">
                        <div>
                            <span class="checkbox" :class="{ checked: modelValue.includes(item) }"></span><span class="name">{{ item.text || $t('tag.unkownUser') }}</span>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref } from 'vue'
const props = defineProps({
    data: {
        type: Array,
        default: [],
    },
    modelValue: {
        type: Array,
        require: true,
    },
    placeholder: {
        type: String,
        require: false,
        default: '请输入',
    },
})

const emits = defineEmits(['change'])

const selecterShow = ref(false)

const onInputerClick = () => {
    selecterShow.value = !selecterShow.value
}

const onselecterChange = data => {
    let index = props.modelValue.findIndex(item => item.value == data.value)
    if (index == -1) {
        props.modelValue.push(data)
    } else {
        props.modelValue.splice(index, 1)
    }
    emits('change', props.modelValue)
}
const onOutside = () => {
    selecterShow.value = false
}
</script>
<style lang="scss" scoped>
ul,
li {
    list-style: none;
    margin: 0;
    padding: 0;
}
.control {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px 4px 4px 4px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    .component {
        position: relative;
        width: 100%;

        display: flex;
        align-items: center;
    }
    .select {
        .place {
            display: flex;
            width: 100%;
            min-height: 66px;
            color: #fff;
            align-items: flex-start;
            &.placeholder {
                color: #757575;
                align-items: center;
                justify-content: center;
            }
            ul {
                margin-bottom: 6px;
            }
            li {
                display: inline-block;
                background-color: #767473;
                border-radius: 4px;
                padding: 4px 6px;
                margin-top: 6px;
                margin-left: 6px;
                i {
                    font-size: 12px;
                    margin-left: 7px;
                    cursor: pointer;
                }
            }
        }
        .panel {
            position: absolute;
            left: -1px;
            right: -1px;
            top: calc(100% + 4px);
            background: rgba(27, 27, 28, 0.9);
            // background: rgb(57, 57, 60, 0.9);
            box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.25);
            border-radius: 4px 4px 4px 4px;
            border: 1px solid #000000;
            max-height: 250px;
            overflow: hidden;
            overflow-y: auto;
            z-index: 1000;
            li {
                cursor: pointer;
                height: 34px;
                display: flex;
                align-items: center;
                &:hover {
                    background: rgba(255, 255, 255, 0.1);
                }
                > div {
                    display: flex;
                    align-items: center;
                    color: rgba(255, 255, 255, 0.6);
                    width: 100%;
                    .name {
                        width: 80%;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                    }
                }
            }
        }

        .icon {
            cursor: pointer;
            margin-right: 10px;
            &.up {
                transform: rotate(180deg);
            }
            i {
                font-size: 12px;
                color: #999;
            }
        }
    }
}
.checkbox {
    position: relative;
    width: 16px;
    height: 16px;
    margin-right: 5px;
    margin-left: 10px;
    &::before {
        content: '';
        border: 1px solid #666;
        border-radius: 2px;
        width: 14px;
        height: 14px;
        position: absolute;
        left: 0px;
        top: 0;
        display: inline-block;
    }
    &.checked {
        &::before {
            border: 1px solid #0076f6;
            background-color: #0076f6;
        }
        &::after {
            left: 3px;
            top: 7px;
            position: absolute;
            display: table;
            border: 2px solid #fff;
            border-top: 0;
            border-left: 0;
            transform: rotate(45deg) translate(-50%, -50%);
            opacity: 1;
            transition: all 0.2s cubic-bezier(0.12, 0.4, 0.29, 1.46) 0.1s;
            width: 6px;
            height: 8px;
            content: ' ';
        }
    }
}

.checkbox-label {
    display: inline-block;
    vertical-align: 3px;
}
</style>
