<template>
  <div class="map-table-container" ref="mapTableContainer">
    <div><slot name="header" /></div>
    <div class="map-table-container-box">
      <ul class="map-table-list" ref="mapTableList">
        <li
          class="map-table-list-item"
          v-for="(item, index) in tableList"
          :key="index"
        >
          <div class="serial">{{ index + 1 }}</div>
          <div
            v-for="(key, value) of item"
            :key="value + index"
            class="map-table-list-item-value"
            :style="[{ '--text-color': key.color, '--text-w': key.width }]"
          >
            {{ key.name }}
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    tableList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      mapTableListHeight: 0,
      mapTableContainerHeight: 0,
      h: 0,
    };
  },
  mounted() {
    setTimeout(() => {
      this.getHeight();
      this.compareH();
    }, 700);
  },
  methods: {
    // 获取map-table-list的高度
    getHeight() {
      // 获取map-table-list的高度
      let mapTableContainer = this.$refs.mapTableContainer;
      this.mapTableContainerHeight = mapTableContainer.offsetHeight - 50;
      let mapTableList = this.$refs.mapTableList;
      this.mapTableListHeight = mapTableList.offsetHeight;
    },
    // 比较map-table-list高度
    compareH() {
      console.log(this.mapTableContainerHeight, this.mapTableListHeight);
      if (this.mapTableContainerHeight < this.mapTableListHeight) {
        // 添加定时器滚动map-table-list-item
        setInterval(() => {
          this.h -= 25;
          if (
            Math.abs(this.h) + this.mapTableContainerHeight >
            this.mapTableListHeight
          ) {
            this.h = 0;
          }
          this.$refs.mapTableList.style.transform = `translateY(${this.h}px) `;
        }, 1500);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.map-table-container {
  --text-w: 20%;
  height: 100%;
  overflow: hidden;
  &-box {
    height: calc(100% - 20px);
    overflow: auto;
  }
}
.map-table-list {
  transition: all 1s;
  margin: 0;
  padding: 0;
  &-item {
    display: flex;
    height: 30px;
    margin-bottom: 5px;
    line-height: 30px;
    color: #fff;
    text-align: center;
    background: linear-gradient(
      180deg,
      rgba(0, 102, 153, 1) 0%,
      rgba(0, 102, 153, 1) 17%,
      rgba(9, 60, 106, 1) 100%,
      rgba(9, 60, 106, 1) 100%
    );
    font-size: 14px;
    .serial {
      width: 40px;
      height: 100%;
      position: relative;
      text-align: center;
      background: #2a719d;
      color: #fff;
      font-weight: bold;
      font-size: 16px;
      &::after {
        position: absolute;
        right: -14px;
        top: 0;
        content: "";
        display: block;
        border-top: 16px solid transparent;
        border-bottom: 16px solid transparent;
        border-left: 16px solid #2a719d;
      }
    }
    .name {
      width: 30%;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      margin-left: 18px;
      font-weight: bold;
    }
    .province,
    .city,
    .num {
      width: 20%;
    }
    .province {
      color: #02a7f0;
    }
    // #FFEA00
    .city {
      color: #ffea00;
    }
    .num {
      color: rgba(217, 0, 27, 0.9);
      font-size: 12px;
    }
    &-value {
      width: var(--text-w);
      color: var(--text-color);
    }
  }
  &-item:hover {
    background: rgba(255, 166, 0, 0.802);
    color: #000;
    .province,
    .city {
      color: #000;
    }
    .serial {
      background: linear-gradient(#ff6048 0%, #ff7346 100%);
      &::after {
        border-left-color: #ff7346;
      }
    }
  }
}
::-webkit-scrollbar {
  display: none;
}
</style>