<template>
  <div class="mb_select" v-clickoutside="onClickOutSide">
    <transition name="slide">
      <ul v-if="isShow">
        <li @click="handleOption(item)" :class="{ active: item.id == value }" v-for="(item,i) in options" :key="i">
          <span>{{item.name}}</span>
        </li>
      </ul>
    </transition>

    <div :class="{noradius:noradius,noradiusAll:noradiusAll}" class="activecon" @click="isShow=!isShow">
      <span>{{active?active.name:''}}</span>
      <img :src="require(`@/assets/images/icon/${isShow?'label_open':'label_shrink'}.png`)" alt="">
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch, defineProps, defineEmits, nextTick, computed } from "vue";

const props = defineProps({
  options: {
    type: Array,
    default: () => {
      return []
    },
  },

  value: {
    type: Number || String,
    default: () => {
      return 0
    }
  },

  noradiusAll: {
    type: Boolean,
    default: () => {
      return false
    }
  },

  noradius: {
    type: Boolean,
    default: () => {
      return false
    }
  },
})

const emit = defineEmits(['handleOption'])

const isShow = ref(false)

const onClickOutSide = (e) => {
  console.log(e, 'e');
  isShow.value = false
}

const handleOption = data => {
  isShow.value = false
  emit('handleOption', data)
}

const active = computed(() => {
  return props.options.find(item => item.id == props.value) || props.options[0]
})

</script>

<style lang="scss" scoped>
.mb_select {
  position: relative;
  display: inline-block;
  width: 100%;
  font-size: 14px;

  .activecon {
    width: 100%;
    height: 40px;
    background: #B61E22;
    border-radius: 50px;
    text-align: center;
    line-height: 40px;
    color: var(--font-active);
    font-weight: bold;
    position: relative;
    z-index: 10;

    &.noradius {
      border-radius: 50px 0 0 50px;
    }

    &.noradiusAll {
      border-radius: 0;
    }


    >span {
      display: inline-block;
      width: 80%;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    >img {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      right: 10px;
      width: 12px;
      height: auto;
    }
  }

  >ul {
    position: absolute;
    left: 0;
    bottom: 20px;
    padding-bottom: 20px;
    z-index: 0;
    text-align: center;
    z-index: 9;
    width: 100%;
    background: rgba(255, 254, 246, 0.94);
    border-radius: 4px;
    backdrop-filter: blur(20px);
    transition: all 0.3s;
    overflow-y: auto;

    >li {
      color: #999;
      margin: 20px 0;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;

      &.active {
        color: var(--font-active);
      }

      >span {
        display: inline-block;
        width: 80%;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
}
</style>
