<template>
  <div class="top-right-btn" :style="style">
    <el-row>
      <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top" v-if="search">
        <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 class="item" effect="dark" content="显隐列" placement="top" v-if="columns">
        <el-button size="mini" circle icon="el-icon-menu" @click="showColumn()" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="排序列" placement="top" v-if="columns">
        <el-button size="mini" circle icon="el-icon-sort" @click="showSort()" />
      </el-tooltip>
    </el-row>
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-transfer
        :titles="['显示', '隐藏']"
        v-model="value"
        :data="columns"
        @change="dataChange"
      ></el-transfer>
    </el-dialog>
    <el-dialog title="排序列" top="50px" class="draggable-dialog" :visible.sync="open2" append-to-body>
      <draggable v-model="columnsCopy" @sort="onDraggableUpdate" animation="100" :filter="'.undraggable'">
        <!-- <transition-group type="transition"> -->
          <div 
          class="sort-col" 
          :class="[d.fixc ? 'undraggable':'']" 
          v-for="(d,i) in columnsCopy" :key="d.label"> 
           <span>{{d.label}}</span>
           <el-checkbox  v-model="d.fixc" @change="fixChange(d,i)" :label="d.label">锁定本列</el-checkbox>
        </div>
        <!-- </transition-group> -->
      </draggable>
    </el-dialog>
  </div>
</template>
<script>
import draggable from 'vuedraggable';
export default {
  components: {
    draggable, //***.第二步 
  },
  name: "RightToolbar",
  data() {
    return {
      // 显隐数据
      value: [],
      // 弹出层标题
      title: "显示/隐藏",
      // 是否显示弹出层
      open: false,
      open2: false,
      columnsCopy: []
    };
  },
  props: {
    showSearch: {
      type: Boolean,
      default: true,
    },
    columns: {
      type: Array,
    },
    search: {
      type: Boolean,
      default: true,
    },
    gutter: {
      type: Number,
      default: 10,
    },
  },
  computed: {
    style() {
      const ret = {};
      if (this.gutter) {
        ret.marginRight = `${this.gutter / 2}px`;
      }
      return ret;
    },
  },
  watch:{
    columns(){
      this.columnsCopy = JSON.parse(JSON.stringify(this.columns));
    }
  },
  created() {
    // 显隐列初始默认隐藏列
    for (let item in this.columns) {
      if (this.columns[item].visible === false) {
        this.value.push(parseInt(item));
      }
      if(this.columns[item].fixed=='left'){
        this.columns[item].fixc = true;
      }else{
        this.columns[item].fixc = false;
      }
    }
    if(this.columns){
      this.columnsCopy = JSON.parse(JSON.stringify(this.columns));
    }
    
  },
  methods: {
    fixChange(d,i){
      // d.fixc = !d.fixc;
      this.$emit('sort',this.columnsCopy)
    },
    onDraggableUpdate(e) {
        const newSort = this.columnsCopy[e.newIndex].key
        this.columnsCopy[e.newIndex].key = this.columnsCopy[e.oldIndex].key
        this.columnsCopy[e.oldIndex].key = newSort;
        this.$emit('sort',this.columnsCopy)
    },
    showSort(){
      this.open2 = true;
    },
    // 搜索
    toggleSearch() {
      this.$emit("update:showSearch", !this.showSearch);
    },
    // 刷新
    refresh() {
      this.$emit("queryTable");
    },
    // 右侧列表元素变化
    dataChange(data) {
      for (let item in this.columns) {
        const key = this.columns[item].key;
        this.columns[item].visible = !data.includes(key);
      }
      this.$emit('columnsChange', true)
    },
    // 打开显隐列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;
}
.draggable-dialog{
  ::v-deep .el-dialog__body{
    height: 75vh;
    overflow-x: hidden;
    overflow-y: auto;
  }
}
.sort-col{
  background: #ebeef5;
  margin: 8px 0;
  padding: 4px 12px;
  display: flex;
  justify-content: space-between;
}
</style>
