<template>
    <div ref="dropdownRef" class="dropdown">
        <!-- Dropdown button -->
        <button @click="toggleDropdown" style="width: 100%;">
            {{ selectedOption }}
            <!-- <svg
          :class="isOpen ? 'down' : ''"
          aria-hidden="true"
          class="icon icon-caret"
          role="presentation"
          viewBox="0 0 10 6"
        >
          <path
            clip-rule="evenodd"
            d="M9.354.646a.5.5 0 00-.708 0L5 4.293 1.354.646a.5.5 0 00-.708.708l4 4a.5.5 0 00.708 0l4-4a.5.5 0 000-.708z"
            fill="currentColor"
            fill-rule="evenodd"
          ></path>
        </svg> -->
        </button>
        <!-- Dropdown menu -->
        <ul v-if="isOpen" class="dropdown-menu">
            <li v-for="(option, index) in options" :key="index" @click="selectOption(option)">
                {{ option.label }}
            </li>
        </ul>
    </div>
</template>

<script>
import { ref, onMounted, onUnmounted, nextTick } from "vue";

export default {
    name: "DropdownMenu",
    props: {
        options: {
            type: Array,
            required: true,
        },
        title:{
            type: String,
            required: true
        }
    },
    setup(props) {
        // Reactive variables
        const isOpen = ref(false); // Dropdown open/close state
        const selectedOption = ref(props.title); // Currently selected option 默认是标题
        const dropdownRef = ref(null); // Reference to dropdown element
        const { proxy } = getCurrentInstance();
        // Toggle dropdown open/close state
        const toggleDropdown = () => {
            isOpen.value = !isOpen.value;
        };

        // Select an option from the dropdown
        const selectOption = (option) => {
            selectedOption.value = option.label; // Update selected option
            if (!option.value) {
                selectedOption.value = props.title
            }
            isOpen.value = false; // Close the dropdown
            // 将选择的值传递给父组件
            proxy.$emit("getOption", option.value);
        };

        // Close dropdown when clicking outside
        const closeDropdown = (event) => {
            if (!dropdownRef.value.contains(event.target)) {
                isOpen.value = false;
            }
        };
        // 清除选择数据
        const clearOption = () => {
            selectedOption.value = props.title;
        }

        // 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
        });

        return {
            isOpen,
            selectedOption,
            toggleDropdown,
            selectOption,
            dropdownRef,
            clearOption
        };
        defineExpose({
            clearOption
        })
    },
};
</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;
}

.dropdown-menu {
    position: absolute;
    left: 50%;
    transform: translate(-50%, 10px);
    // background-color: #f9f9f9;
    background: rgba(0, 0, 0, 0.8);
    min-width: 180px;
    max-height: 300px;
    box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
    z-index: 1;
    margin: 0;
    padding: 0;
    overflow-y: auto;
}

.dropdown-menu li {
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    cursor: pointer;
    font-size: 16px;
    line-height: 1.5;
    font-weight: 500;
}

.dropdown-menu li:hover {
    background-color: rgb(35, 83, 147);
}

.icon-caret {
    color: #666;
    stroke: #666;
    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);
    }
}

/* 设置滚动条的宽度 */
::-webkit-scrollbar {
    display: none;
}
</style>