<template>
  <div class="app-container--toolbar" :style="style">
    <div class="app-container--toolbar-left">
      <div class="toolbar-title">
        <span>数据列表</span>
      </div>
    </div>
    <div class="app-container--toolbar-right">
      <el-tooltip
        v-if="add"
        class="item"
        effect="dark"
        content="新增"
        placement="top"
      >
        <el-button
          size="mini"
          type="primary"
          circle
          icon="el-icon-plus"
          @click="handleToolbar('add')"
        />
      </el-tooltip>
      <el-tooltip
        v-if="edit"
        class="item"
        effect="dark"
        content="编辑当前行"
        placement="top"
      >
        <el-button
          size="mini"
          circle
          icon="el-icon-edit"
          @click="handleToolbar('edit')"
        />
      </el-tooltip>
      <el-tooltip
        v-if="edit"
        class="item"
        effect="dark"
        content="删除当前行"
        placement="top"
      >
        <el-button
          size="mini"
          circle
          icon="el-icon-delete"
          @click="handleToolbar('delete')"
        />
      </el-tooltip>
      <el-tooltip
        v-if="upload"
        class="item"
        effect="dark"
        content="导入Excel"
        placement="top"
      >
        <el-button
          size="mini"
          circle
          icon="el-icon-upload"
          @click="handleToolbar('upload')"
        />
      </el-tooltip>
      <el-tooltip
        v-if="excel"
        class="item"
        effect="dark"
        content="导出Excel"
        placement="top"
      >
        <el-button
          size="mini"
          circle
          icon="el-icon-download"
          @click="handleToolbar('excel')"
        />
      </el-tooltip>
      <el-tooltip
        v-if="search"
        class="item"
        effect="dark"
        :content="showSearch ? '隐藏搜索' : '显示搜索'"
        placement="top"
      >
        <el-button
          size="mini"
          circle
          icon="el-icon-search"
          @click="toggleSearch()"
        />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="刷新表格" placement="top">
        <el-button
          size="mini"
          circle
          icon="el-icon-refresh"
          @click="refresh()"
        />
      </el-tooltip>
      <el-tooltip
        v-if="columns.length > 0"
        class="item"
        effect="dark"
        content="显隐列"
        placement="top"
      >
        <el-button
          size="mini"
          circle
          icon="el-icon-menu"
          @click="showColumn()"
        />
      </el-tooltip>
    </div>
    <el-dialog
      width="500px"
      :title="title"
      :visible.sync="open"
      append-to-body
      custom-class="app-container--dialog"
    >
      <el-transfer
        v-model="value"
        :titles="['显示', '隐藏']"
        :data="columns"
        @change="dataChange"
      />
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'RightToolbar',
  props: {
    showSearch: {
      type: Boolean,
      default: true
    },
    columns: {
      type: Array,
      default: () => []
    },
    search: {
      type: Boolean,
      default: true
    },
    excel: {
      type: Boolean,
      default: false
    },
    upload: {
      type: Boolean,
      default: false
    },
    add: {
      type: Boolean,
      default: false
    },
    edit: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    gutter: {
      type: Number,
      default: 10
    }
  },
  data() {
    return {
      // 显隐数据
      value: [],
      // 弹出层标题
      title: '显示/隐藏',
      // 是否显示弹出层
      open: false
    }
  },
  computed: {
    style() {
      const ret = {}
      if (this.gutter) {
        ret.marginRight = `${this.gutter / 2}px`
      }
      return ret
    }
  },
  created() {
    // 显隐列初始默认隐藏列
    for (const item in this.columns) {
      if (this.columns[item].visible === false) {
        this.value.push(parseInt(item))
      }
    }
  },
  methods: {
    handleToolbar(key) {
      this.$emit(key)
    },
    // 搜索
    toggleSearch() {
      this.$emit('update:showSearch', !this.showSearch)
    },
    // 刷新
    refresh() {
      this.$emit('queryTable')
    },
    // 右侧列表元素变化
    dataChange(data) {
      for (const item in this.columns) {
        const key = this.columns[item].key
        this.columns[item].visible = !data.includes(key)
      }
    },
    // 打开显隐列dialog
    showColumn() {
      this.open = true
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-transfer__button {
  border-radius: 50%;
  padding: 12px;
  display: block;
  margin-left: 0px;
}
::v-deep .el-transfer__button:first-child {
  margin-bottom: 10px;
}
</style>
