import { Component, EventEmitter, Input, OnInit } from '@angular/core';
import { Bu4Model, MyBu4Model } from 'src/app/shared/model/bu4.model';
import { Bu5Model, MyBu5Model } from 'src/app/shared/model/bu5.model';
import { Bu4Service } from 'src/app/shared/services/bu4.service';
import { Bu5Service } from 'src/app/shared/services/bu5.service';


@Component({
  selector: 'app-sub-department-two',
  templateUrl: './sub-department-two.component.html',
  styleUrls: ['./sub-department-two.component.scss']
})
export class SubDepartmentTwoComponent implements OnInit {
  currentPage = 1
  page = Array.from({ length: 1 }, (_, i) => i + 1);
  bu5List: Bu5Model[] = []
  bu5: Bu5Model = new MyBu5Model({})
  bu4: Bu4Model = new MyBu4Model({})
  search = ""
  constructor(private bu5Service: Bu5Service,
    private bu4Service: Bu4Service
  ) { }
  ngOnInit(): void {
    this.getBu5List()
  }
  getBu5List() {
    this.bu5Service.getList().subscribe(response => {
      this.bu5List = response
      this.searchChange()
    })
  }
  searchChange() {
    this.currentPage = 1
    this.page = Array.from({ length: Math.ceil(this.bu5ListFilter().length / 10) }, (_, i) => i + 1);
  }
  bu5ListFilter() {
    return this.bu5List.filter(x => x.bu5id.toLowerCase().includes(this.search) ||
      x.tdesc.toLowerCase().includes(this.search) ||
      x.edesc.toLowerCase().includes(this.search))
  }
  selectBu5(bu5: Bu5Model) {
    this.bu4Service.getById(bu5.parent).subscribe(response => {
      this.bu4 = response
    })
    this.bu5 = new MyBu5Model(bu5)
  }
  addBu5() {
    // this.bu5Service.post(this.bu5).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu5List()
    //   }
    // })
  }
  deleteBu5(bu5: Bu5Model) {
    // this.bu5Service.delete(new MyBu5Model(bu5)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu5List()
    //   }
    // })
  }

}
