<template>
  <div class="ui-grid-toolbar-batch">
    <ui-toolbar :bind-close="false">
      <ui-toolbar-item>
        <span class="ui-grid-toolbar-batch__checkbox">
          <el-checkbox
            @click.native="handleToggleCheckbox"
            :indeterminate="data.length > 0 && !isAllSelected"
            :value="isAllSelected"
          ></el-checkbox>
        </span>
      </ui-toolbar-item>
      <ui-toolbar-item>
        <span class="ui-grid-toolbar-batch__count">已选择<b>{{selecteds.length}}</b>项</span>
      </ui-toolbar-item>
    </ui-toolbar>
    <ui-toolbar :bind-close="false" align="right">
      <ui-toolbar-item
        v-for="(item, index) in innerItems"
        :key="index"
      >
        <component
          v-if="item.render"
          :is="{render: item.render}"
        ></component>
        <ui-button
          v-else
          v-bind="item"
          :plain="true"
          :icon="item.icon"
          size="small"
          :click="handleRenderBtnClick(item)"
        >
          {{item.label}}
        </ui-button>
      </ui-toolbar-item>
    </ui-toolbar>
  </div>
</template>
<script>
import ElCheckbox from 'element-ui/lib/checkbox'

export default {
  name: 'UiGridBatchToolbar',
  components: {
    ElCheckbox
  },
  props: {
    title: {
      type: String
    },
    data: {
      type: Array,
      default: () => []
    },
    selecteds: {
      type: Array,
      default: () => []
    },
    items: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    isAllSelected() {
      return this.data.length <= this.selecteds.length
    },
    renderData() {
      return {
        selecteds: this.selecteds
      }
    },
    innerItems() {
      const me = this
      return (this.items || []).filter(v => {
        if (typeof v.visible === 'boolean') {
          return v.visible
        }
        if (typeof v.visible === 'function') {
          return v.visible()
        }
        return true
      }).map(v => {
        const vv = Object.assign({}, v)
        if (v.render) {
          vv.render = (h) => {
            return v.render(h, me.renderData)
          }
        }
        return vv
      })
    }
  },
  methods: {
    handleToggleCheckbox(evt) {
      evt.stopPropagation()
      this.$emit('toggle-selection')
    },
    handleRenderBtnClick(item) {
      if (item && item.click) {
        return () => {
          const result = item.click({
            item,
            selecteds: this.selecteds
          })
          if (result && result.then) {
            item.loading = true
            result.then(() => {
              item.loading = false
              this.$emit('batch-success')
            }, () => {
              item.loading = false
            })
          }
          return result
        }
      }
    }
  }
}
</script>
<style lang="less">
.ui-grid-toolbar-batch{
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  height: 32px;
}
.ui-grid-toolbar-batch__count{
  display: inline-block;
  color: #888;
}

.ui-grid-toolbar-batch__count b{
  display: inline-block;
  min-width: 20px;
  text-align: center;
  color: #00A0EB;
  font-weight: normal;
}
.ui-grid-toolbar-batch__checkbox{
  display: inline-block;
  margin-left: 10px;
}
</style>
