<template>
  <div class="top-right-btn">
    <el-row class="right-tools">
      <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top" v-if="search">
        <svg-icon icon-class="search-svg" class-name="svg-icon-img" @click="toggleSearch()" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="刷新" placement="top">
        <svg-icon icon-class="refresh-svg" class-name="svg-icon-img" @click="refresh()" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="筛选" placement="top" v-if="checkboxArr">
        <svg-icon icon-class="select-svg" class-name="svg-icon-img" @click="showColumn()" v-if="showColumnsType == 'transfer'" />
        <el-dropdown trigger="click" :hide-on-click="false" v-if="showColumnsType == 'checkbox'">
          <section>
            <svg-icon icon-class="select-svg" class-name="svg-icon-img"> </svg-icon>
          </section>
          <el-dropdown-menu slot="dropdown">
            <!-- <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox> -->
            <template v-for="(item, idx) in checkboxArr">
              <el-dropdown-item :key="item.key">
                <el-checkbox v-model="item.visible" @change="checkboxChange($event, item.label, idx)" :label="item.label" />
              </el-dropdown-item>
            </template>
          </el-dropdown-menu>
        </el-dropdown>
      </el-tooltip>
    </el-row>
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-transfer :titles="['显示', '隐藏']" v-model="value" :data="checkboxArr" @change="dataChange"></el-transfer>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'RightToolbar',
  data() {
    return {
      // 显隐数据
      value: [],
      // 弹出层标题
      title: '显示/隐藏',
      // 是否显示弹出层
      open: false,
      checkAll: true,
      isIndeterminate: false,
      checkboxArr: []
    }
  },
  props: {
    /* 是否显示检索条件 */
    showSearch: {
      type: Boolean,
      default: true
    },
    /* 显隐列信息 */
    columns: {
      type: Array
    },
    /* 是否显示检索图标 */
    search: {
      type: Boolean,
      default: true
    },
    /* 显隐列类型（transfer穿梭框、checkbox复选框） */
    showColumnsType: {
      type: String,
      default: 'checkbox'
    },
    /* 右外边距 */
    gutter: {
      type: Number,
      default: 10
    }
  },
  watch: {
    columns: {
      handler(val) {
        this.checkboxArr = val
      },
      immediate: true
    }
  },
  computed: {
    style() {
      const ret = {}
      if (this.gutter) {
        ret.marginRight = `${this.gutter / 2}px`
      }
      return ret
    }
  },
  created() {
    if (this.showColumnsType == 'transfer') {
      // 显隐列初始默认隐藏列
      for (const item in this.checkboxArr) {
        if (this.checkboxArr[item].visible === false) {
          this.value.push(parseInt(item))
        }
      }
    }
  },
  methods: {
    // 搜索
    toggleSearch() {
      this.$emit('update:showSearch', !this.showSearch)
    },
    // 刷新
    refresh() {
      this.$emit('queryTable')
    },
    // 右侧列表元素变化
    dataChange(data) {
      for (const item in this.checkboxArr) {
        const key = this.checkboxArr[item].key
        this.checkboxArr[item].visible = !data.includes(key)
      }
    },
    // 打开显隐列dialog
    showColumn() {
      this.open = true
    },
    // 勾选
    checkboxChange(event, label, idx) {
      this.$nextTick(() => {
        this.$set(this.checkboxArr[idx], 'visible', event)
        this.$emit('getConfigColumns', {
          label: label,
          checked: event
        })
      })
    },
    handleCheckAllChange(val) {
      this.checkboxArr = this.checkboxArr?.map((ot) => ({
        ...ot,
        visible: val
      }))
    }
  }
}
</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;
}
.el-icon-menu,
.el-icon-refresh-right,
.svg-icon-img {
  cursor: pointer;
  // font-size: 14px;
  width: 14px;
  height: 14px;
  line-height: 14px;
  // vertical-align: middle;
}
.el-icon-search {
}
.right-tools {
  .el-tooltip {
    margin-right: 20px;
    &:last-child {
      margin-right: 0;
    }
  }
}
</style>
