/**
 * 计算表格高度
 * @param cardRef 标题容器
 */
export function useTableHeight(cardRef: Ref<HTMLElement | null>) {
  const computeTableHeight = ref({
    titleHeight: 0,
    dividerHeight: 32,
    toolbarHeight: 51,
    padding: 50,
    cardPadding: 40,
  });

  const totalOtherHeight = ref(0);
  const tableHeight = ref(0);

  const computeHeight = () => {
    const titleElement = unref(cardRef);

    computeTableHeight.value.titleHeight = titleElement ? titleElement.scrollHeight : 0;

    totalOtherHeight.value =
        computeTableHeight.value.titleHeight +
        computeTableHeight.value.dividerHeight +
        computeTableHeight.value.toolbarHeight +
        computeTableHeight.value.padding +
        computeTableHeight.value.cardPadding * 2;

    tableHeight.value = window.innerHeight - totalOtherHeight.value;
  };

  onMounted(() => {
    computeHeight(); // 组件挂载时计算一次高度
    window.addEventListener('resize', computeHeight); // 监听窗口大小变化
  });

  onUnmounted(() => {
    window.removeEventListener('resize', computeHeight); // 移除事件监听器
  });

  return { tableHeight: tableHeight as Ref<number> }; // 返回类型注释
}
