<template>
  <div class="com-combox" :class="{show:showPopup}" @click="show" v-clickoutside="hide">
    <div class="input">
      <p v-if="current">{{current.name}}</p>
    </div>
    <a class="iconfont icon-material_preview_upload_collect"></a>
    <ul>
      <li
        v-for="(item,key) in data"
        :key="key"
        @click="onClick(item)"
        :class="{active:selectedId == item.id}"
      >
        <slot :item="item">
          <template>
            <span>{{item.name}}</span>
          </template>
        </slot>
      </li>
    </ul>
  </div>
</template>
<script>
export default {
  props: {
    data: Array,
    type: {
      type: String,
      default: "object"
    },
    selectedId: {
      type: String,
      default:''
    }
  },
  data() {
    return {
      showPopup: false,
    };
  },
  computed: {
    current() {
      let tmp = this.data.find(item=>{
         return this.selectedId == item.id
      })
      return tmp
    }
  },
  methods: {
    show() {
      if (this.showPopup === true) {
        this.showPopup = false;
        return;
      }
      this.showPopup = true;
    },
    hide() {
      this.showPopup = false;
    },
    onClick(item) {
      if (this.selectedId == item.id) {
        return this.$emit("click", item);
      }
      this.$emit("change", item);
    }
  }
};
</script>
<style lang="less" scoped>
.com-combox {
  position: relative;
  border: 1px solid rgba(151, 151, 151, 0.2);
  padding: 0 16px;
  background: #1A1B1D;;
  border-radius: 2px;
  height: 36px;
  cursor: pointer;
  z-index: 100;
  &.show {
    ul {
      display: block;
    }
    a {
      transform: translateY(-50%) rotate(180deg);
    }
  }
  > .input {
    height: 100%;
    display: flex;
    align-items: center;
    .p {
      width: 100%;
      padding-right: 50px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      text-align: left;
    }
  }
  
  ul {
    display: none;
    padding: 0;
    position: absolute;
    left: -1px;
    right: -1px;
    top: 100%;
    background: #1A1B1D;
    border-radius: 0px 0px 2px 2px;
    border: 1px solid #404040;
    overflow-x: hidden;
    overflow-y: auto;
    max-height: 130px;
    li {
      height: 36px;
      padding: 0 16px;
      display: flex;
      align-items: center;
      &.active,
      &:hover {
        background: #252526;
        color: @color;
      }
    }
  }
  a {
    position: absolute;
    right: 16px;
    top: 50%;
    font-size: 12px;
    transform: translateY(-50%);
  }
}
</style>