<template>
  <transition name="el-zoom-in-top" @after-leave="doDestroy">
    <div
      v-show="showPopper"
      class="b-autocomplete-suggestion b-popper"
      :class="{ 'is-loading': !parent.hideLoading && parent.loading }"
      :style="{ width: dropdownWidth }"
      role="region">
      <b-scrollbar
        tag="ul"
        wrap-class="b-autocomplete-suggestion__wrap"
        view-class="b-autocomplete-suggestion__list">
        <li v-if="!parent.hideLoading && parent.loading"><i class="b-icon-loading"></i></li>
        <li v-else-if="parent.suggestions.length === 0 && parent.isShowNoData" class="b-no-data-tips">暂无数据</li>
        <slot v-else>
        </slot>
      </b-scrollbar>
    </div>
  </transition>
</template>
<script>
  import Popper from 'b-chensh-ui/src/utils/vue-popper';
  import Emitter from 'b-chensh-ui/src/mixins/emitter';
  import BScrollbar from 'b-chensh-ui/packages/scrollbar';

  export default {
    components: { BScrollbar },
    mixins: [Popper, Emitter],

    componentName: 'b-autocomplete-suggestions',

    data() {
      return {
        parent: this.$parent,
        dropdownWidth: ''
      };
    },

    props: {
      options: {
        default() {
          return {
            gpuAcceleration: false
          };
        }
      },
      id: String
    },

    methods: {
      select(item) {
        this.dispatch('BAutocomplete', 'item-click', item);
      }
    },

    updated() {
      this.$nextTick(_ => {
        this.popperJS && this.updatePopper();
      });
    },

    mounted() {
      this.$parent.popperElm = this.popperElm = this.$el;
      this.referenceElm = this.$parent.$refs.input.$refs.input || this.$parent.$refs.input.$refs.textarea;
      this.referenceList = this.$el.querySelector('.b-autocomplete-suggestion__list');
      this.referenceList.setAttribute('role', 'listbox');
      this.referenceList.setAttribute('id', this.id);
    },

    created() {
      this.$on('visible', (val, inputWidth) => {
        this.dropdownWidth = inputWidth + 'px';
        this.showPopper = val;
      });
    }
  };
</script>
