<script>
export default {
  props: {
    tableList: {
      type: Array,
      default: () => [],
    },
    columns: {
      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;
      if (mapTableContainer) {
        this.mapTableContainerHeight = mapTableContainer.offsetHeight - 50;
      }
      let mapTableList = this.$refs.mapTableList;
      this.mapTableListHeight = mapTableList?.offsetHeight;
    },
    // 比较map-table-list高度
    compareH() {
      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;
          }
          if (this.$refs.mapTableList) {
            this.$refs.mapTableList.style.transform = `translateY(${this.h}px) `;
          }
        }, 1500);
      }
    },
  },
  render() {
    return <div class="map-table-container" ref="mapTableContainer">
      <div>
        {this.$slots.header}
      </div>
      <div class="map-table-container-box">
        <ul class="map-table-list" ref="mapTableList">
          {
            this.tableList.map((item, index) => {
              return <li class="map-table-list-item">
                {
                  this.columns.map((key, keyIndex) => {
                    return <div class="map-table-list-item-value" style={{
                      '--text-color': key.color, '--text-w': key.width
                    }}>
                      {key.props == 'index' && <div class="serial">{index + 1}</div>}
                      {key.render && key.render()}
                      {<span>{item[key.props]}</span>}
                    </div>
                  })
                }
              </li>
            })
          }
        </ul>
      </div>
    </div>
  }
};
</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;
      }
    }

    .map-table-list-item-value {
      width: 30%;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      margin-right: 18px;
      font-weight: bold;
    }

    .map-table-list-item-value {
      width: var(--text-w);
      color: var(--text-color);
    }

    .map-table-list-item-value:nth-child(3) {
      color: #02a7f0;
    }

    .map-table-list-item-value:nth-child(4) {
      color: #ffea00;
    }

    .map-table-list-item-value:nth-child(5) {
      color: rgba(217, 0, 27, 0.9);
      font-size: 12px;
    }

    .province {}

    // #FFEA00
    .city {}

    .num {}


  }

  &-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>