<template>
  <div class="s-table">
    <div class="table-headers">
      <div
        v-for="(header, index) in headers"
        :key="index"
        :style="{
          minWidth: header.width ? header.width : '',
          ...(header.style ? header.style : {}),
        }"
      >
        {{ header.name }}
      </div>
    </div>
    <div class="table-data" ref="table">
      <div
        class="table-data-columns"
        v-for="(row, index) in tableData"
        :key="index"
      >
        <div
          v-for="(header, hIndex) in headers"
          :key="hIndex"
          :style="{
            minWidth: header.width ? header.width : '',
            ...(header.style ? 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>
                {{ row[header.props] }}
              </div>
            </el-tooltip>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "UTable",
  props: {
    headers: {
      type: Array,
      default: () => {
        return [];
      },
    },
    tableData: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {};
  },
  mounted() {
    this.$refs.table.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;
  border: 1px solid #107de5;
  background: #0a3163;
  color: #02fffc;
  font-size: 14px;
  text-align: center;
  display: flex;
  justify-content: space-between;
  flex-wrap: nowrap;
  div {
    flex: 1;
    white-space: nowrap;
  }
}
.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;
    .table-data-column {
      flex: 1;
    }
    .table-data-cell {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    &:nth-child(2n) {
      background: rgba(4, 36, 66, 0.6);
    }
    &:hover {
      background: rgba(19, 56, 132, 0.6);
    }
  }
}
</style>