<template>
  <div class="s-table">
    <div class="table-headers">
      <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
      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"
      >
        <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 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,
      default: () => {
        return [];
      },
    },
    pagination: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    load() {
      if (this.pagination) {
        this.$emit("load");
      }
    },
  },
  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%;
}
.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: 34px;
    display: flex;
    justify-content: space-between;
    flex-wrap: nowrap;
    text-align: center;
    color: #fff;
    font-size: 14px;
    margin-bottom: 4px;

    background: url("@/assets/images/index/icon_moren@2x.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    &:hover {
      background: url("@/assets/images/index/icon_xuanz@2x.png");
      background-repeat: no-repeat;
      background-size: 100% 100%;
    }

    .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;
  img {
    width: 100px;
    vertical-align: top;
    opacity: 0.7;
  }
  .inner {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
  }
}
</style>