<template>
  <transition :name="transitionName" @before-enter="beforeEnter">
    <div v-show="visible" :class="wrapperClasses" :style="wrapperStyles">
      <div v-if="hasArrow" ref="arrow" :class="arrowClass" x-arrow></div>
      <x-cuctom-render v-if="customHeader" :render="customHeader"></x-cuctom-render>
      <slot name="search"></slot>
      <div ref="wrapper" class="inner-wrapper" :style="innerStyles">
        <x-scroller
          ref="scroller"
          :scrollbar-class="scrollbarClass"
          :style="innerStyles"
          width="100%"
          @start-drag="handleStartDrag"
          @dragging="handleDragging"
        >
          <slot></slot>
        </x-scroller>
      </div>
      <x-cuctom-render v-if="customFooter" :render="customFooter"></x-cuctom-render>
    </div>
  </transition>
</template>
<script>
import { LIB_NAME, ANIMATION_PREFIX, Popper, formatSize, xCuctomRender } from '../../../../src/util'
import { xScroller } from '../../../vue-scroller/src'

export default {
  name: 'xSelectDropdown',
  components: { xCuctomRender, xScroller },
  mixins: [Popper],
  props: {
    width: [String, Number], // eslint-disable-line
    height: [String, Number], // eslint-disable-line
    maxHeight: [String, Number], // eslint-disable-line
    hasArrow: Boolean,
    customHeader: Function, // eslint-disable-line
    customFooter: Function, // eslint-disable-line
    customClass: String, // eslint-disable-line
    scrollbarClass: String // eslint-disable-line
  },
  data() {
    return {
      arrowClass: `${LIB_NAME}-popper-arrow large`,
      wrapperStyles: {},
      innerStyles: {}
    }
  },
  computed: {
    transitionName() {
      return this.$parent.dropAnimation || `${ANIMATION_PREFIX}drop`
    },
    wrapperClasses() {
      return [
        `${LIB_NAME}-select-dropdown`,
        { light: this.hasArrow },
        { 'no-box-shadow': this.noBoxShadow },
        this.customClass
      ]
    }
  },
  mounted() {
    this.$refs.reference = this.$parent.$el
  },
  methods: {
    handleStartDrag() {
      this.$parent.dragging = false
    },
    handleDragging() {
      this.$parent.dragging = true
    },
    toggle() {
      if (this.visible) {
        this.hide()
      } else {
        this.show()
      }
    },
    show() {
      const width = formatSize(this.width) || this.$parent.$el.clientWidth + 'px'
      this.$emit('on-update-width', width)
      this.wrapperStyles = { width }
      this.innerStyles = {
        height: formatSize(this.height),
        maxHeight: formatSize(this.maxHeight)
      }
      this.visible = true
    },
    hide() {
      this.visible = false
    },
    beforeEnter() {
      this.checkScrollable()
    },
    checkScrollable() {
      this.$nextTick(() => {
        this.$refs.scroller.checkScrollable()
      })
    },
    updateLayout() {
      this.$refs.scroller.stickToBoundary(true, true, false)
      this.updateElementHandler()
    },
    scrollToTarget(option) {
      if (option && option.$el) {
        this.$refs.scroller.scrollToTarget(option.$el)
      }
    },
    setReference(reference) {
      this.destroyPopper()
      this.referenceEl = reference
    }
  }
}
</script>
