<template>
  <div class="tableMneu">
    <div
      class="mask"
      v-if="isShowHeaderBox || menuOpen"
      @click="maskClick"
    ></div>
    <el-table
      ref="tableMenu"
      :data="tableData"
      border
      fit
      style="width: 100%"
      row-key="id"
      height="100%"
      default-expand-all
      highlight-current-row
      @row-click="handlerRowClick"
      @expand-change="handlerExpand"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="进度">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  props: {
    list: Array,
    BGScrollTop: Number
  },
  computed: {
    tableData() {
      return this.list;
    }
  },
  watch: {
    BGScrollTop: {
      handler: function(newValue) {
        let table = this.$refs.tableMenu.bodyWrapper;
        // console.log(newValue, table);
        table.scrollTo(0, newValue);
        // table.scrollTo({
        //   top: newValue,
        //   left: 0,
        //   behavior: "smooth"
        // });
      }
    }
  },
  data() {
    return {
      // tableData: [],
      checkList: [],
      isShowHeaderBox: false,
      menuOpen: false,
      //当前点击的row
      currentRow: {}
    };
  },
  methods: {
    handlerWatchScroll() {
      let table = this.$refs.tableMenu.bodyWrapper;
      table.addEventListener("scroll", e => {
        // console.log(e.srcElement.scrollTop);
        this.$emit("TableScrollTop", e.srcElement.scrollTop);
      });
    },
    handlerSelect(row) {
      this.$refs.tableMenu.setCurrentRow(row);
    },
    handlerExpand(row, expand) {
      // console.log(row, expanded);
      this.$emit("handlerExpand", row, expand);
    },
    maskClick() {
      this.isShowHeaderBox = false;
      this.menuOpen = false;
      this.currentRow = {};
    },
    handlerRowClick(row) {
      this.$emit("handlerRowClick", row);
    },
  },
  mounted() {
    this.handlerWatchScroll();
  }
};
</script>

<style lang="scss" scoped>
.tableMneu {
  width: 100%;
  position: relative;
  height: 100%;
  .mask {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background-color: transparent;
    z-index: 9999;
  }
  .icons {
    cursor: pointer;
    &:hover {
      color: #409eff;
    }
  }
  .menulist {
    width: 100px;
    position: absolute;
    background-color: rgb(255, 255, 255);
    border-radius: 5px;
    border: 1px solid #ebeef5;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    transition: 0.3s;
    width: 70px;
    z-index: 9999;

    .item {
      background-color: rgb(255, 255, 255);
      line-height: 30px;
      text-align: center;
      font-size: 12px;
      color: rgb(102, 102, 102);
      cursor: pointer;
      height: 30px;
    }

    .item:hover {
      color: #409eff;
    }
  }
  .headerBox {
    z-index: 9999;
    position: absolute;
    right: -128px;
    top: 50px;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);
    width: 170px;
    background-color: #fff;
    &::after {
      content: " ";
      position: absolute;
      top: -5px;
      left: 16px;
      width: 0;
      height: 0;
      border-right: 7px solid transparent;
      border-left: 7px solid transparent;
      border-bottom: 7px solid #fff;
    }
    .title {
      margin: 0;
      padding: 8px 16px;
      min-height: 32px;
      border-bottom: 1px solid #e9e9e9;
      color: #303030;
      font-weight: 500;
    }
    .main {
      .line {
        padding: 8px 10px;
        display: flex;
        flex-direction: row;
        align-items: center;
        width: 150px;
        .pre {
          flex: 1 1 auto;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
}
</style>
<style lang="scss" >
.tableMneu {
  .el-table th.gutter {
    display: table-cell !important;
  }
  .el-table td,
  .el-table th {
    padding: 8px 0;
  }
  .el-table--border td {
    border-right: none;
  }
}
</style>
