<template>
  <div class="s-table">
    <div class="table-headers">
      <div
        v-for="(header, index) in headers"
        :key="index"
        :style="{
          width: header.width ? header.width : '',
          ...header.style,
        }"
      >
        {{ header.name }}
      </div>
    </div>
    <div
      class="table-data"
      v-infinite-scroll="load"
      :infinite-scroll-distance="1"
      :infinite-scroll-delay="1000"
      ref="tableData"
    >
      <div
        class="table-data-columns"
        v-for="(row, index) in tableData"
        :key="index"
        @click="rowClick(row)"
      >
        <div
          v-for="(header, hIndex) in headers"
          :key="hIndex"
          :style="{
            width: header.width ? header.width : '',
            ...header.style,
          }"
          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 style="cursor: pointer">
                {{ row[header.props] }}
              </div>
            </el-tooltip>
          </template>
        </div>
      </div>
      <div v-if="!tableData || tableData.length == 0" class="no-data">
        <span>暂无数据</span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "STable",
  props: {
    headers: {
      type: Array,
      default: () => {
        return [];
      },
    },
    tableData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    pagination: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    load() {
      if (this.pagination) {
        this.$emit("load");
      }
    },
    rowClick(row) {
      this.$emit("row-click", row);
    },
  },
  data() {
    return {};
  },
  mounted() {
    this.$refs.tableData.onscroll = () => {
      let list = document.getElementsByClassName("el-tooltip__popper");
      if (list.length > 0) {
        list[list.length - 1].style.display = "none";
      }
    };
  },
};
</script>
<style lang="less" scoped>
.s-table {
  height: 100%;
  width: calc(100% - 20px);
  margin: 0 10px;
}
.table-headers {
  line-height: 32px;

  color: #afcfed;
  font-size: 13px;
  text-align: center;
  display: flex;
  justify-content: space-between;
  flex-wrap: nowrap;
  background: #19396e;
  div {
    padding: 0 4px;
  }
}
.table-data {
  height: calc(100% - 34px);
  overflow-y: auto;
  background: rgba(25, 57, 110, 0.3);
  .table-data-columns {
    line-height: 34px;
    display: flex;
    justify-content: space-between;
    flex-wrap: nowrap;
    text-align: center;
    color: #fff;
    font-size: 14px;

    &:hover {
      background: rgba(25, 57, 110, 1) !important;
    }
    &:nth-child(2n) {
      background: rgba(25, 57, 110, 0.5);
    }

    .table-data-cell {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      padding: 0 4px;
    }
  }
}
.no-data {
  text-align: center;
  color: #fff;

  height: 100%;
  font-size: 20px;
  position: relative;
  span {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
  }
}
</style>