import { AfterViewInit, Component, OnDestroy, OnInit } from '@angular/core';
import { SFComponent, SFSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { fromEvent, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

@Component({
  template: ''
})
export class BasicTableComponent implements AfterViewInit, OnDestroy {
  scrollY = '400px';

  sf!: SFComponent;
  sfValue: Record<string, any> = {};
  drawer: Subscription[] = [];
  schema: SFSchema = {};

  deviationHeight = 0;

  constructor(public searchDrawerService: SearchDrawerService) {}

  ngAfterViewInit(): void {
    setTimeout(() => {
      this.getScrollY();
    }, 100);
    fromEvent(window, 'resize')
      .pipe(debounceTime(100))
      .subscribe(event => {
        this.getScrollY();
      });
  }

  ngOnDestroy(): void {
    this.drawer.forEach(sub => sub.unsubscribe());
  }

  openDrawer() {
    if (this.drawer?.length > 0) {
      this.searchDrawerService.create(this.sfValue, this.schema);
    } else {
      const drawer = this.searchDrawerService.create(this.sfValue, this.schema);
      this.drawer.push(
        drawer.initEvent.subscribe((sf: SFComponent) => {
          if (sf) {
            this.sf = sf;
          }
        })
      );
      this.drawer.push(
        drawer.closeEvent.subscribe((res: Record<string, any>) => {
          this.sfValue = res;
          if (res) {
            this.search();
          }
        })
      );
    }
  }

  getScrollY() {
    const windowHeight = window.innerHeight || Math.max(document.documentElement.clientHeight, document.body.clientHeight);
    const header = document.getElementsByTagName('layout-pro-header')?.[0];
    if (windowHeight && header) {
      let scrollY = windowHeight - header.clientHeight - 35 - 49;
      // 剔除页头高度
      const headerWrapper = document.getElementsByTagName('page-header-wrapper')?.[0];
      if (headerWrapper) {
        scrollY -= headerWrapper.clientHeight;
      }
      // 计算所有tabs高度
      const tabset = document.getElementsByTagName('nz-tabs-nav');
      let tabsetHeight = 0;
      for (let index = 0; index < tabset.length; index++) {
        tabsetHeight += tabset[index].clientHeight;
      }
      if (tabset) {
        scrollY -= tabsetHeight;
      }
      // 剔除高度容器
      const headerBox = document.getElementsByClassName('header_box');
      let headerBoxHeight = 0;
      for (let index = 0; index < headerBox.length; index++) {
        headerBoxHeight += headerBox[index].clientHeight;
      }
      if (headerBox) {
        scrollY -= headerBoxHeight;
      }

      // 剔除高度容器
      const heightBox = document.getElementsByClassName('height_box');
      let heightBoxHeight = 0;
      for (let index = 0; index < heightBox.length; index++) {
        heightBoxHeight += heightBox[index].clientHeight;
      }
      if (heightBox) {
        scrollY -= heightBoxHeight;
      }

      if (typeof this.deviationHeight === 'number') {
        scrollY -= this.deviationHeight;
      }
      this.scrollY = scrollY + 'px';
    }
  }

  search() {}
}
