<!--
 * @Date: 2020-12-14 09:07:03
 * @LastEditors: zhp
 * @LastEditTime: 2024-09-05 09:50:14
 * @FilePath: \mt-bus-fe\src\views\OperationalOverview\components\baseTable.vue
 * @Description:
-->
<template>
  <div class="visual-base-table-container">
    <el-table :max-height="maxHeight" ref="scroll_Table" @mouseenter.native="autoScroll(true)"
      @mouseleave.native="autoScroll(false)" v-loading="isLoading"
      :header-cell-style="{ background: 'rgba(218, 226, 237, 1)', color: 'rgba(0, 0, 0, .6)', padding: '3px 2px' }"
      :row-style="setRowStyle" :data="renderData" border style="width: 100%; background: transparent">
      <el-table-column v-if="page && limit && showIndex" prop="_pageIndex" label="序号" :width="70" align="center" />
      <el-table-column v-for="item in renderTableHeadList" :key="item.prop" :show-overflow-tooltip="showOverflow"
        v-bind="item">
        <template slot-scope="scope">

          <component :is="item.subcomponent" v-if="item.subcomponent" :inject-data="{ ...scope.row, ...item }"
            @emitData="emitData" />
          <span v-else>{{ scope.row[item.prop] | commonFilter(item.filter) }}</span>

        </template>
      </el-table-column>
      <slot name="content" />
    </el-table>
  </div>
</template>
<script>
import { isObject, isString } from 'lodash'
export default {
  name: 'BaseTable',
  filters: {
    commonFilter: (source, filterType = a => a) => {
      return filterType(source)
    }
  },
  props: {
    maxHeight: {
      type: [Number, String],  // 支持数字（如300）或字符串（如'300px'）
      required: false,
      default: 200  // 原固定值，作为默认 fallback
    },
    tableData: {
      type: Array,
      required: true,
      validator: val => val.filter(item => !isObject(item)).length === 0
    },
    tableConfig: {
      type: Array,
      required: true,
      validator: val => val.filter(item => !isString(item.prop) || !isString(item.label)).length === 0
    },
    isLoading: {
      type: Boolean,
      required: false
    },
    page: {
      type: Number,
      required: false,
      default: 1
    },
    limit: {
      type: Number,
      required: false,
      default: 5
    },
    beilv: {
      type: Number,
      default: 1
    },
    showOverflow: {
      type: Boolean,
      default: true
    },
    showIndex: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      tableConfigBak: [],
      selectedBox: new Array(100).fill(true)
    }
  },
  computed: {
    renderData() {
      if (this.tableData.length && !this.tableData[0]._pageIndex) {
        this.tableData.forEach((item, index) => {
          item._pageIndex = (this.page - 1) * this.limit + index + 1
        })
      }
      return this.tableData.slice((this.page - 1) * this.limit, this.page * this.limit)
    },
    renderTableHeadList() {
      return this.tableConfig.filter((item, index) => {
        return this.selectedBox[index]
      })
    }
  },
  beforeMount() {
    this.selectedBox = new Array(100).fill(true)
  },
  mounted() {
    this.autoScroll()
  },
  beforeDestroy() {
    this.autoScroll(true)
  },
  methods: {
    autoScroll(stop) {
      const table = this.$refs.scroll_Table
      if (!table) return; // 防止table未加载时出错

      const divData = table.$refs.bodyWrapper
      if (stop) {
        window.clearInterval(this.scrolltimer)
      } else {
        // 先清除已有计时器，避免重复创建
        if (this.scrolltimer) {
          window.clearInterval(this.scrolltimer)
        }

        this.scrolltimer = window.setInterval(() => {
          // 每次滚动1像素
          divData.scrollTop += 1

          // 关键修改：使用>=判断，允许微小像素偏差
          if (divData.scrollTop + divData.clientHeight >= divData.scrollHeight - 1) {
            // 滚动到底部后，重置到顶部（延迟一点更自然）
            // setTimeout(() => {
            divData.scrollTop = 0
            // }, 2000); // 停顿500ms后再从头滚动
          }
        }, 200) // 滚动速度（数值越小越快）
      }
    },
    emitData(val) {
      this.$emit('emitFun', val)
    },
    setRowStyle(v) {
      if (v.rowIndex % 2 === 0) {
        return {
          background: '#F9FCFF',
          color: 'rgba(87, 87, 87, 1)',
          height: 35 + 'px',
          lineHeight: 26 + 'px',
          padding: 0,
          fontSize: 12 + 'px'
        }
      } else {
        return {
          background: 'rgba(239, 243, 248, 1)',
          color: 'rgba(87, 87, 87, 1)',
          height: 35 + 'px',
          lineHeight: 26 + 'px',
          padding: 0,
          fontSize: 12 + 'px'
        }
      }
    },
    setCellStyle() {
      return {
        // lineHeight: 23  + 'px'
      }
    }
  }
}
</script>
<style lang="scss" scoped>
// @import "./styles/index.scss";
.visual-base-table-container {
  .el-table {
    border: 0;

    //  .el-table__body-wrapper::-webkit-scrollbar-thumb {
    //       background-color: blue;
    //       border-radius: 3px;
    //     }
    // 关键修改：隐藏滚动条但保留滚动功能
    &::-webkit-scrollbar {
      width: 0;
      height: 0;
      background: transparent;
    }

    // 隐藏表头的gutter
    .el-table__header .el-table__cell.gutter {
      display: none !important;
    }

    // 表格主体内容区滚动条处理
    .el-table__body-wrapper {
      &::-webkit-scrollbar {
        width: 0;
        height: 0;
      }

      &::-webkit-scrollbar-thumb {
        background: transparent;
      }

      &::-webkit-scrollbar-track {
        background: transparent;
      }

      overflow-y: auto !important; // 确保垂直滚动可用
      overflow-x: auto !important; // 确保水平滚动可用
    }

    // 固定列滚动条处理
    .el-table__fixed,
    .el-table__fixed-right {
      .el-table__fixed-body-wrapper {
        &::-webkit-scrollbar {
          width: 0;
          height: 0;
        }

        overflow-y: auto !important;
        overflow-x: auto !important;
      }
    }
  }

  .el-table::before,
  .el-table--border::after {
    background-color: transparent;
  }

  .el-table th,
  td {
    border-color: rgba(221, 221, 221, 1) !important;
    padding: 0;
  }

  .el-table tr {
    background: transparent;
  }

  .el-table__row:hover>td {
    background-color: rgba(79, 114, 136, 0.29) !important;
  }

  .el-table__row--striped:hover>td {
    background-color: rgba(79, 114, 136, 0.29) !important;
  }
}

// .setting {
//   text-align: right;
//   padding: 15px;
//   .setting-box {
//     width: 100px;
//   }
//   i {
//     color: #aaa;
//     @extend .pointer;
//   }
// }
</style>
