import { getViewportOffset, useWindowSizeFnHook } from './utils'

export default {
  data() {
    return {
      // 默认最大高度
      scrollH: 200,
      resizeHeightOffset: 0
    }
  },
  computed: {
    scrollY() {
      return this.scrollH
    }
  },
  watch: {
    toggleFormTag() {
      this.$nextTick(() => {
        this.computeScroll()
      })
    }
  },
  mounted() {
    const { start, stop } = useWindowSizeFnHook(this.computeScroll, undefined, { immediate: true })
    this.$on('hook:activated', start)
    this.$on('hook:deactivated', stop)
    this.$once('hook:destroyed', stop)
  },
  methods: {
    computeScroll() {
      this.scrollH = this.computeTableHeight()
    },
    computeTableHeight() {
      const table = this.$refs.dataSource
      const tableEl = table?.$el
      if (!table || !tableEl) return
      const headEl = tableEl.querySelector('.ant-table-thead')
      const { bottomIncludeBody } = getViewportOffset(headEl)
      console.log(bottomIncludeBody)
      let cardPadding = 18 // card组件内边距
      let paginationH = 60 // 表格分页高度
      let copyright = 38 // 版权高度
      let headH = 42 // 表头
      let resizeHeightOffset = this.resizeHeightOffset || 0 // 手动偏移
      const height = bottomIncludeBody - (paginationH + cardPadding + copyright + headH + resizeHeightOffset)
      return height < 80 ? 80 : height
    }
  }
}