<!--  -->
<template>
    <div class="style-icon">
        <div class="style-list">
            <div class="upload-btn" v-if="icons && icons.length < 19">
                <ui-button class="add">
                    <ui-input v-vip:vip="auth.isVip" v-vip:expired="auth.isExpired" class="input" preview accept=".jpg, .jpeg, .png" @update:modelValue="iconUpload" type="file">
                        <template v-slot:replace>
                            <ui-icon type="add" class="icon" />
                        </template>
                    </ui-input>
                </ui-button>
                <div class="vip-tag"></div>
            </div>
            <div class="style-item default" @click="changeLogo('')" :style="`background-image:url(${defaultLogo});`" :class="{ active: icon == '' }"></div>
            <template v-for="(i, index) in showList">
                <!-- <div class="style-item" @click="changeLogo(i.id, index)" v-if="index < 3" :style="`background:${i.name};`" :class="{ active: icon == i.id }"></div> -->
                <div class="style-item" @click="changeLogo(i.name, i.sid)" v-if="index < 4" :style="`background-image:url(${common.changeUrl(i.name)});`" :class="{ active: iconId == i.sid }">
                    <div class="close-btn" @click.stop="delIcon(i)">
                        <ui-icon type="close"></ui-icon>
                    </div>
                </div>
            </template>
            <div class="more" v-if="icons && icons.length > 4">
                <ui-button @click.stop="showMore = !showMore">
                    <ui-icon :class="{ active: showMore }" type="pull-down"></ui-icon>
                </ui-button>
                <div class="tagLogo-box" :class="{ active: showMore }">
                    <template v-for="(i, index) in hideList">
                        <div @click="insertLogo(i)" class="tagLogo-item" :style="`background-image:url(${common.changeUrl(i.name)});`">
                            <div class="close-btn">
                                <ui-icon type="close" @click.stop="delIcon(i)"></ui-icon>
                            </div>
                        </div>
                    </template>
                </div>
            </div>
        </div>
        <!-- <span class="more">更多></span> -->
    </div>
</template>

<script setup>
import { reactive, toRefs, onBeforeMount, onMounted, onUnmounted, ref, computed } from 'vue'
import { Cropper, Loading, Dialog } from '@/global_components/'
import common from '@/utils/common'
import { useStore } from 'vuex'
import { getApp } from '@/app'
const store = useStore()
const showMore = ref(false)
const defaultLogo = ref('')
defaultLogo.value = getApp().resource.getAppURL('images/tag_icon_default.svg')
const chooseItem = ref(null)
const hotData = computed(() => store.getters['tag/hotData'])
const icons = computed(() => {
    console.log(store.getters['tag/icons'])
    return store.getters['tag/icons'] || []
})
const auth = computed(() => store.getters['scene/auth'])
const iconId = computed(() => store.getters['tag/iconId'])
const icon = computed(() => {
    return hotData.value.icon
})

const showList = computed(() => {
    for (let i = 0; i < icons.value.length; i++) {
        if (icons.value[i].sid == iconId.value) {
            chooseItem.value = icons.value[i]
        }
    }
    const styles = icons.value.slice(0, icons.value.length > 4 ? (icons.value.length < 19 ? 3 : 4) : 4)
    if (chooseItem.value && !styles.includes(chooseItem.value)) {
        let index = icons.value.length < 19 ? 2 : 3
        styles[index] = chooseItem.value
    }
    return styles
})
const hideList = computed(() => {
    let res = icons.value.filter(style => {
        return !showList.value.includes(style)
    })
    return res
})

// let fristIndex = null
const iconUpload = item => {
    let sid = common.getRandomSid()
    let src = URL.createObjectURL(item.file)
    let pramas = { img: src, fixedNumber: [1, 1] }
    let suffix = item.file.type.split('/')[1]
    let fileName = sid + '.' + suffix
    Cropper.open(pramas).then(res => {
        if (res) {
            let data = {
                sid: sid,
                name: res[1],
                fileName: fileName,
            }
            store.commit('tag/pushIcon', { ...data })
            store.commit('tag/changeIcon', { ...data })
        }
    })
}
const changeLogo = (name, sid) => {
    store.commit('tag/changeIcon', { name: name, sid: sid })
    showMore.value = false
}
const insertLogo = item => {
    chooseItem.value = item
    store.commit('tag/changeIcon', item)
    showMore.value = false
}
const delIcon = item => {
    chooseItem.value = null
    store.commit('tag/deleteIcon', item)
    if (item.sid == iconId.value) {
        store.commit('tag/changeIcon', { name: '', sid: '' })
    }
}
const closeMore = () => {
    showMore.value = false
}
onMounted(() => {
    window.addEventListener('click', closeMore, false)
})
onUnmounted(() => {
    window.removeEventListener('click', closeMore, false)
})
</script>
<style lang="scss" scoped>
.style-icon {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 0 24px;
    // .more {
    //     color: var(--editor-main-color);
    //     cursor: pointer;
    // }
    .style-list {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        > div {
            margin-right: 20px;
            &:last-of-type {
                margin-right: 0;
            }
        }
        .upload-btn {
            position: relative;
            cursor: pointer;
            .ui-input {
                position: relative;
                z-index: 11;
            }
        }

        .add {
            width: 40px;
            height: 40px;
            border-radius: 0;
            cursor: pointer;
            // margin-right: 20px;
            border: 1px solid #fde1b0;
            color: #fde1b0;
            position: relative;
            border-radius: 4px;
            transition: none;
        }
        .style-item {
            width: 40px;
            height: 40px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            // background: #f2f2f2;
            background-repeat: no-repeat;
            background-size: cover;
            position: relative;
            border-radius: 4px;
            border: 1px solid transparent;
            &.default {
            }
            &.active {
                border: 1px solid var(--editor-main-color);
            }
            .close-btn {
                width: 16px;
                height: 16px;
                border-radius: 50%;
                position: absolute;
                display: flex;
                align-items: center;
                justify-content: center;
                background: rgba(250, 63, 72, 1);
                opacity: 0.6;
                right: -6px;
                top: -6px;
                z-index: 101;
                display: none;
                &:hover {
                    opacity: 1;
                }
                > i {
                    font-size: 12px;
                    color: #fff;
                    transform: scale(0.8);
                }
            }
            &:hover {
                .close-btn {
                    display: flex;
                }
            }
        }
        .more {
            width: 40px;
            height: 40px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            // border-radius: 4px;
            // border: 1px solid rgba(255, 255, 255, 0.2);
            box-sizing: border-box;
            position: relative;
            z-index: 100;
            button {
                width: 100%;
                height: 100%;
                position: relative;
                .iconfont {
                    transition: all 0.3s;
                    position: absolute;
                    margin-top: -7px;
                    margin-left: -7px;

                    &.active {
                        transform: rotate(180deg);
                    }
                }
            }
            .tagLogo-box {
                position: absolute;
                width: 380px;
                // height: 200px;
                background: rgba(27, 27, 28, 0.9);
                box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
                border: 1px solid #000000;
                backdrop-filter: blur(4px);
                z-index: 10;
                right: -20px;
                top: 100%;
                margin-top: 12px;
                display: flex;
                flex-wrap: wrap;
                align-items: center;
                justify-content: flex-start;
                padding: 0 18px 20px 18px;
                transform: scale(0);
                transform-origin: 90% top;
                transition: all 0.3s;
                &.active {
                    transform: scale(1);
                    transform-origin: 90% top;
                }
                &::after {
                    position: absolute;
                    content: '';
                    right: 32px;
                    top: -10px;
                    width: 0;
                    height: 0;
                    border-left: 6px solid transparent;
                    border-right: 6px solid transparent;
                    border-bottom: 10px solid rgba(27, 27, 28, 0.8);
                }
                .tagLogo-item {
                    width: 40px;
                    height: 40px;
                    border-radius: 4px;
                    background: #fff;
                    margin-right: 20px;
                    margin-top: 20px;
                    position: relative;
                    border: 1px solid transparent;
                    background-size: cover;

                    &.active {
                        border: 1px solid var(--editor-main-color);
                    }
                    &:nth-of-type(6n) {
                        margin-right: 0;
                    }
                    .close-btn {
                        width: 16px;
                        height: 16px;
                        border-radius: 50%;
                        position: absolute;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        background: rgba(250, 63, 72, 1);
                        opacity: 0.6;
                        right: -6px;
                        top: -6px;
                        z-index: 101;
                        display: none;
                        &:hover {
                            opacity: 1;
                        }
                        > i {
                            font-size: 12px;
                            color: #fff;
                            transform: scale(0.8);
                        }
                    }
                    &:hover {
                        .close-btn {
                            display: flex;
                        }
                    }
                }
            }
        }
    }
}
</style>
