<template>
    <div ref="dropdownRef" class="dropdown">
        <!-- Dropdown button -->
        <div class="box_drop" @click="toggleDropdown">
            <!-- {{ selectedOption }} -->
            {{ name }}
            <el-icon class="icon icon-caret" :class="isOpen ? 'down' : ''" color="#ffffff">
                <CaretBottom />
            </el-icon>
        </div>
        <!-- Dropdown menu -->
        <transition name="fade">
            <ul v-if="isOpen" class="dropdown-menu">
                <li v-for="(option, index) in options" :key="index" @click="selectOption(option)">
                    {{ option }}
                </li>
            </ul>
        </transition>
    </div>
</template>

<script setup>
let props = defineProps(['options', 'name'])
// Reactive variables
const isOpen = ref(false); // Dropdown open/close state
const selectedOption = ref(props.options[0]); // Currently selected option
const dropdownRef = ref(null); // Reference to dropdown element

// Toggle dropdown open/close state
const toggleDropdown = () => {
    isOpen.value = !isOpen.value;
};
let emit = defineEmits(['changeData'])
// Select an option from the dropdown
const selectOption = (option) => {
    selectedOption.value = option; // Update selected option
    isOpen.value = false; // Close the dropdown
    emit('changeData', option)
};

// Close dropdown when clicking outside
const closeDropdown = (event) => {
    if (!dropdownRef.value.contains(event.target)) {
        isOpen.value = false;
    }
};

// Lifecycle hook: mounted
onMounted(() => {
    nextTick(() => {
        document.addEventListener("click", closeDropdown); // Listen for click events outside dropdown
    });
});

// Lifecycle hook: unmounted
onUnmounted(() => {
    document.removeEventListener("click", closeDropdown); // Remove event listener when component is unmounted
});

</script>

<style lang="scss" scoped>
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown button {
    background: none;
    color: inherit;
    padding: 0;
    border: none;
    cursor: pointer;
    font-size: inherit;
    font-weight: inherit;
}

.box_drop {
    user-select: none;
    display: flex;
    align-items: center;
    color: #ffffff;
    background: none;
    padding: 0;
    border: none;
    cursor: pointer;
    font-size: inherit;
    font-weight: inherit;
}

.dropdown-menu {
    width: fit-content;
    width: -webkit-fit-content;
    width: -moz-fit-content;
    white-space: nowrap;
    position: absolute;
    left: 50%;
    transform: translate(-50%, 10px);
    z-index: 1;
    margin: 0;
    padding: 0;
    border-radius: 6px;
    user-select: none;
    border: 1px solid rgb(3, 94, 138);
    background-color: rgb(11, 39, 90);
    padding: 5px 0;
}

.dropdown-menu::before {
    content: '';
    box-sizing: border-box;
    content: " ";
    height: 10px;
    position: absolute;
    transform: rotate(45deg);
    width: 10px;
    top: -5px;
    right: calc(50% - 5px);
    border: 1px solid rgb(3, 94, 138);
    background-color: rgb(11, 39, 90);
    border-bottom-color: transparent !important;
    border-right-color: transparent !important;
}

/* 定义过渡效果 */
.fade-enter-active,
.fade-leave-active {
    transition: opacity 0.5s;
}

.fade-enter-from,
.fade-leave-to {
    opacity: 0;
}

.dropdown-menu li {
    padding: 5px 16px;
    text-decoration: none;
    display: block;
    cursor: pointer;
    font-size: 14px;
    color: #ffffff;
    line-height: 22px;
    list-style: none;
    margin: 0;
    outline: none;
    padding: 5px 16px;
    white-space: nowrap;
}

.dropdown-menu li:hover {
    background-color: rgb(17, 60, 141);
}

.icon-caret {
    height: 1.3rem;
    width: 1.3rem;
    margin-left: 0.3rem;
    stroke-width: 0.02rem;
    pointer-events: auto;
    transition: transform 0.2s ease-in-out;

    &.down {
        transform: rotate(180deg);
    }
}
</style>