<template>
  <ui-popover
    v-model="showPopover"
    class="bi-sort-select"
    trigger="hover"
    popper-class="bi-sort-select__popover"
  >
    <span slot="reference" class="bi-sort-select__handle" @click="handleHandleClick">
      <span class="bi-sort-select__handle-text">{{ currentItem ? currentItem.label : '排序' }}</span>
      <span class="bi-sort-select__handle-arrow" :class="['is-' + (currentItem && currentItem.order === 'asc' ? 'asc' : 'desc')]" />
    </span>
    <div class="bi-sort-select__menu">
      <div
        class="bi-sort-select__item"
        @click="handleItemClick(item)"
        v-for="(item, index) in items"
        :class="{'is-active': innerValue && (innerValue.prop === item.prop && innerValue.order === item.order)}"
        :key="index"
      >{{ item.label }}{{ item.unit }}</div>
      <div
        class="bi-sort-select__item is-divided"
        @click="handleResetClick"
      >恢复默认排序</div>
    </div>
  </ui-popover>
</template>
<script>
import Popover from '@huntianning/components/packages/popover'

export default {
  name: 'BiGridSort',
  props: {
    value: {
      type: Object
    },
    /**
     * {
     *    prop: 'aaa',
     *    order: 'asc
     * }
    */
    defaultSort: {
      type: Object
    },
    /**
     * [{
     *    label: 'xxxxx',
     *    prop: 'aaa',
     *    order: 'asc',
     *    unit: '最早'
     * }]
    */
    items: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      innerValue: this.defaultSort,
      showPopover: false
    }
  },
  computed: {
    currentItem() {
      if (!this.innerValue) {
        return
      }
      return this.items.find(v => v.prop === this.innerValue.prop && v.order === this.innerValue.order)
    }
  },
  watch: {
    value() {
      this.innerValue = this.value
    }
  },
  components: {
    UiPopover: Popover
  },
  methods: {
    handleHandleClick() {
      if (!this.innerValue) {
        return
      }
      const orderMap = {
        desc: 'asc',
        asc: 'desc'
      }
      this.$emit('change', {
        value: {
          prop: this.innerValue.prop,
          order: orderMap[this.innerValue.order]
        }
      })
    },
    handleItemClick(item) {
      this.showPopover = false
      this.$emit('change', {
        value: {
          prop: item.prop,
          order: item.order
        }
      })
    },
    handleResetClick() {
      this.showPopover = false
      this.$emit('change', {
        value: this.defaultSort || null
      })
    }
  }
}
</script>
<style lang="less">
.bi-sort-select__popover{
  padding: 6px 0!important;
}
.bi-sort-select__handle-text{
  font-size: 14px;
}
.bi-sort-select__handle{
  display: flex;
  flex-direction: row;
  align-items: center;
  height: 28px;
  line-height: 28px;
  cursor: pointer;
}
.bi-sort-select__handle-arrow{
  display: inline-block;
  width: 18px;
  height: 18px;
  margin-left: 6px;
  background: url('./icons/desc.png') no-repeat 50% 50%;
  background-size: 90%;
  margin-top: 1px;
  &.is-asc{
    margin-top: 0;
    background-image: url('./icons/asc.png');
  }
}
.bi-sort-select__item{
  line-height: 24px;
  padding: 6px 12px;
  cursor: pointer;
  transition: all .25s;
  &:hover {
    background-color: #f3f3f3;
  }
  &.is-active {
    color: #409EFF;
    background-color: rgba(95, 158, 250, .1);
  }
  &.is-divided{
    margin-top: 12px;
    position: relative;
    &:after {
      content: '';
      position: absolute;
      left: 0;
      right: 0;
      top: -6px;
      height: 1px;
      background-color: #eee;
    }
  }
}
</style>
