<template>
  <div class="s-table">
    <div class="table-headers">
      <div v-if="hasIndex" style="width: 0.4rem">序号</div>
      <div style="width: calc(100% - 0.4rem); display: flex">
        <div
          v-for="(header, index) in headers"
          :key="index"
          :style="{
            ...header.headerStyle,
            width: header.width ? header.width : '',
            textAlign: header.align,
          }"
        >
          {{ header.name }}
        </div>
      </div>
    </div>
    <div
      class="table-data"
      v-infinite-scroll="load"
      :infinite-scroll-distance="1"
      :infinite-scroll-delay="1000"
      ref="scrollContainer"
    >
      <div ref="scrollList">
        <div
          class="table-data-columns"
          v-for="(row, index) in tableData"
          :key="index"
          @click="rowClick(row)"
        >
          <div class="table-data-column" v-if="hasIndex" style="width: 0.4rem">
            {{ index + 1 }}
          </div>
          <div style="width: calc(100% - 0.4rem); display: flex">
            <div
              v-for="(header, hIndex) in headers"
              :key="hIndex"
              :style="{
                ...header.style,
                width: header.width ? header.width : '',
                textAlign: header.align,
              }"
              class="table-data-column"
            >
              <template v-if="header.slot">
                <slot :row="row" :header="header" />
              </template>
              <template v-else>
                <el-tooltip
                  class="table-data-cell"
                  effect="dark"
                  :content="String(row[header.props])"
                >
                  <div>
                    {{ row[header.props] }}
                  </div>
                </el-tooltip>
              </template>
            </div>
          </div>
        </div>
      </div>

      <div v-if="tableData.length == 0" class="no-data">
        <div class="inner">
          <img src="@/assets/images/index/no-data.png" alt="" />
          <div>暂无数据</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "STable",
  props: {
    headers: {
      type: Array,
      default: () => {
        return [];
      },
    },
    tableData: {
      type: [Array, Object],
      default: () => {
        return [];
      },
    },
    pagination: {
      type: Boolean,
      default: false,
    },
    hasIndex: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      scrollInterval: null,
    };
  },
  methods: {
    load() {
      if (this.pagination) {
        this.$emit("load");
      }
    },
    startScrolling() {
      const scrollContainer = this.$refs.scrollContainer;
      const scrollList = this.$refs.scrollList;

      let scrollTop = 0; // 当前滚动位置

      this.scrollInterval = setInterval(() => {
        scrollTop++;
        scrollContainer.scrollTop = scrollTop;

        // 如果滚动到底部，直接返回顶部
        if (
          scrollTop >=
          scrollList.scrollHeight - scrollContainer.clientHeight
        ) {
          scrollTop = 0; // 重置滚动位置为顶部
        }
      }, 80);
    },
    stopScrolling() {
      if (this.scrollInterval) {
        clearInterval(this.scrollInterval);
      }
    },
    rowClick(row) {
      this.$emit("row-click", row);
    },
  },
  mounted() {
    // this.$refs.scrollContainer.onscroll = () => {
    //   let list = document.getElementsByClassName("el-tooltip__popper");
    //   if (list.length > 0) {
    //     list[list.length - 1].style.display = "none";
    //   }
    // };
    this.$refs.scrollContainer.onmouseover = () => {
      this.stopScrolling();
    };
    this.$refs.scrollContainer.onmouseout = () => {
      this.startScrolling();
    };
    this.startScrolling();
  },
};
</script>
<style lang="less" scoped>
.s-table {
  height: 100%;
}
.table-headers {
  line-height: 32px;

  color: #b2bfcf;
  font-size: 14px;
  text-align: center;
  display: flex;
  justify-content: space-between;
  flex-wrap: nowrap;

  div {
    padding: 0 4px;
  }
}
.table-data {
  height: calc(100% - 34px);
  overflow-y: auto;
  .table-data-columns {
    line-height: 40px;
    display: flex;
    justify-content: space-between;
    flex-wrap: nowrap;
    text-align: center;
    color: #fff;
    font-size: 14px;
    padding: 0 1px;
    .table-data-column {
    }
    .table-data-cell {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      padding: 0 4px;
    }
    &:nth-child(2n) {
      background: rgba(4, 36, 66, 0.6);
    }
    &:hover {
      background: #1c3f67;
      border: 1px solid #6286ba;
    }
  }
}
.no-data {
  text-align: center;
  color: #fff;
  background: rgba(4, 36, 66, 0.6);
  height: 100%;
  font-size: 20px;
  position: relative;
  img {
    width: 100px;
    vertical-align: top;
    opacity: 0.7;
  }
  .inner {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
  }
}
</style>