import {Component, AfterViewInit, ElementRef, ViewChild, HostListener} from '@angular/core';
import {Router} from "@angular/router";
import * as Parse from "parse";


(Parse as any).serverURL = "https://web2023.fmode.cn/parse";

// (Parse as any).serverURL = "https://web2023.fmode.cn/parse";
// https://web2023.fmode.cn/s0210490/api/user/login
Parse.initialize("dev")


@Component({
  selector: 'app-allcompany',
  templateUrl: './allcompany.component.html',
  styleUrls: ['./allcompany.component.scss']
})


export class AllcompanyComponent implements AfterViewInit {

  @ViewChild('inputSearch') inputSearch!: ElementRef;

  companyList: any[] = []

  constructor(private router: Router, private element: ElementRef) {

  }


  ngAfterViewInit(): void {
    this.initPage();
  }


  async initPage() {
    console.log(this?.inputSearch.nativeElement.value)
    this.companyList = await this.searchCompany(this?.inputSearch.nativeElement.value)
  }

  searchInput: string = ``
  searchType: string = `name`

  async onSearch(searchText: string) {
    this.skip = 0; // 每次搜索条件变化，从第一条重新加载
    console.log("搜索")
    this.companyList = await this.searchCompany(searchText);
  }

  isAttach = false


  onContainerScroll(event: Event) {
    const container = event.target as HTMLElement;
    const scrollHeight = container.scrollHeight;
    const scrollTop = container.scrollTop;
    const clientHeight = container.clientHeight;
    // console.log(Math.round(scrollHeight - scrollTop));
    // console.log(clientHeight);
    if (Math.round(scrollHeight - scrollTop) - 1 <= clientHeight && !this.isAttach) {
      // 容器已触底

      this.isAttach = true
      setTimeout(() => {
        this.isAttach = false
      }, 1000);
      this.handleScrollToBottom()
      // 在这里执行触底后的操作
    }
  }


  handleScrollToBottom() {
    // 执行触底加载逻辑函数
    this.onBottomLoad();
    console.log("触底")
  }


  //触底加载逻辑
  pageSize = 12
  skip: number = 0 // 跳过多少条进行加载
  async onBottomLoad() {
    console.log("onBottomLoad", this.companyList.length)
    this.skip = this.companyList.length;
    let list = await this.searchCompany(this.inputSearch.nativeElement.value);
    this.companyList = this.companyList.concat(list)
  }

  //数据加载相关函数
  // async getCompanyData() {
  //   let query = new Parse.Query("LjCompany");
  //   query.limit(this.pageSize);
  //   if (searchText) {
  //     query.contains(this.searchType, this.searchInput)
  //   }
  //   if (this.skip) {
  //     query.skip(this.skip)
  //   }
  //   query.addAscending("comno")
  //   let list = await query.find();
  //   return list
  // }

  async searchCompany(searchText?: string) {
    let query = new Parse.Query("LjCompany");
    query.limit(this.pageSize);
    if (searchText) {
      query.contains(this.searchType, searchText)
    }
    if (this.skip) {
      query.skip(this.skip)
    }
    query.addAscending("comno")
    let list = await query.find();
    console.log(list)
    return list

  }

  goStudentDetail(student: Parse.Object) {
    this.router.navigate(["/home/allcompany/companydetail"], {
      queryParams: student
    })
  }


}
