<template>
  <div>
    <el-drawer
      :title="title"
      :visible.sync="tabTitdrawer"
      :direction="direction"
      :size="size +'px'"
      :before-close="handleClose"
      :with-header="false"
    >
      <div class="customColumnTable">
        <div class="columnHeader">
          <div class="columnName columnNameMargin">字段</div>
          <div class="columnNameMargin">显示</div>
          <div class="columnNameMargin">排序</div>
        </div>
        <div class="columnList">
            <vuedraggable
              v-model="headers"
              v-bind="dragOptions"
              @start="onStart"
              @end="onEnd"
            >
              <transition-group
                type="transition"
                name="flip-list"
                class="transition"
              >
                <div class="li" v-for="(item, index) in headers" :key="index">
                  <span class="span">{{ item.label }}</span>
                  <el-checkbox v-model="item.checked"></el-checkbox>
                  <span class="fold">
                    <i class="el-icon-s-fold"></i>
                  </span>
                </div>
              </transition-group>
            </vuedraggable>
        </div>
        <div class="columnButtons">
          <div>
            <el-button size="small" type="info" plain @click="handleClose">取 消</el-button>
            <el-button size="small" type="primary" @click="sureTab"
              >确 定</el-button
            >
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import vuedraggable from "vuedraggable"; //拖拽组件
export default {
  components: {
    vuedraggable,
  },
  name: "ZrTableColumns",
  props: {
    columnData: {
      type: Array,
      default: () => [],
    },
    tabTitdrawer: {
      type: Boolean,
      default: false,
    },
    size: {
      type: Number | String,
      default: '320',
    },
    direction:{
      type: String,
      default: 'ltr',
    },
    title:{
      type: String,
      default: '自定义表头',
    }
  },
  data() {
    return {
      dragOptions: {
        //拖拽配置
        animation: 200,
      },
      headers: [],
    };
  },
  watch: {
    columnData: {
      handler(val) {
        this.headers = val;
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    sureTab() {
      this.handleClose();
      let arrList = this.headers
        // .filter((el) => el.checked)
        // .map((ele) => {
        //   return ele.type;
        // });
      this.$emit("sureTab", arrList);
    },
    handleClose() {
      this.$emit("update:tabTitdrawer", false);
    },
    //开始拖拽事件
    onStart() {
      // this.drag=true;
    },
    //拖拽结束事件
    async onEnd() {
      console.log(this.columnData,"columnData");
    },
  },
};
</script>

<style lang="scss" scoped>
.customColumnTable {
  position: relative;
  .columnHeader {
    display: flex;
    line-height: 59px;
    height: 59px;
    border-bottom: 1px solid #eee;
    font-weight: bold;
    font-size: 16px;
    .columnName {
      width: 200px;
      text-align: right;
      margin-right: 20px;
    }
    .columnNameMargin{
      margin-right: 10px;
    }
  }
  .columnList {         
    padding: 0 20px;
    height: calc(100vh - 160px);
    overflow: auto;
    padding-top: 20px;
    .li {
      margin: 10px 0;
      display: flex;
      align-items: center;
      .span {
        display: inline-block;
        width: 180px;
        margin-right: 20px;
        text-align: right;
      }
      .fold {
        margin-left: 20px;
        font-size: 20px;
        cursor: pointer;
      }
    }
  }
  .columnButtons {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 80px;
  }
}
</style>