/*
 *@description:表格hover性能优化
 *@author: hudw
 *@date: 2022-09-20 11:09:41
 */


export default {
  data() {
    this.lastHoverIndex = -1;
    return {
      elementTableContent: null,
      elementTableBodyList: null,

      tableBody: null,
      isScollbarSport:false

    }
  },
  mounted() {

    if (this.$refs.a_table) {
      this.tableBody = this.$el.querySelector('.ant-table-body');
      let table = this.elementTableContent =this.$refs.a_table.$el.querySelector('.ant-table-content')
      let tableScroll = table.children[0].querySelector('.ant-table-body>table');
      let currentRowList = [tableScroll]
      const bodyList=()=> {
        if (table.children.length > 1) {
          for (let i = 1; i < table.children.length; i++) {
            let div = table.children[i]?.querySelector('.ant-table-body-outer table') ?? null
            div && currentRowList.push(div)
          }
        }
        this.elementTableBodyList = currentRowList
      }
      bodyList()
      this.$watch('columns', bodyList)
      this.elementTableContent.addEventListener('mouseover', this.handleMouseover)
      this.elementTableContent.addEventListener('mouseout', this.handleMouseout)

  }


  },
  destroyed() {
    if (this.elementTableContent) {

      this.elementTableContent.removeEventListener('mouseover', this.handleMouseover)
      this.elementTableContent.removeEventListener('mouseout', this.handleMouseout)
    }

    this.elementTableContent = null


  },
  methods: {
    handleMouseover(event) {
      if(this.isScollbarSport)return false
      this.resultsTRList(event, 'add')
    },
    handleMouseout(event) {
      this.resultsTRList(event, 'remove')
    },
    resultsTRList(event, fn) {
      event.stopPropagation()
      let tr
      let rowIndex
      if (fn === 'add') {
         tr = event.target.closest("tr");
        if (!tr) return
        rowIndex=tr.rowIndex
      } else if (fn === 'remove') {
        rowIndex= this.lastHoverIndex
      }

      this.elementTableBodyList.forEach((body) => {
        body.rows[rowIndex]?.classList[fn]('ant-table-row-hover')
      }, this)
      if (fn === 'add') {
        this.lastHoverIndex = rowIndex

      }
    },
  },
}
