import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Bu3Model, MyBu3Model } from 'src/app/shared/model/bu3.model';
import { Bu4Model, MyBu4Model } from 'src/app/shared/model/bu4.model';
import { Bu3Service } from 'src/app/shared/services/bu3.service';
import { Bu4Service } from 'src/app/shared/services/bu4.service';

@Component({
  selector: 'app-sub-department-one',
  templateUrl: './sub-department-one.component.html',
  styleUrls: ['./sub-department-one.component.scss']
})
export class SubDepartmentOneComponent {
  currentPage = 1
  page = Array.from({ length: 1 }, (_, i) => i + 1);
  bu4List: Bu4Model[] = []
  bu4: Bu4Model = new MyBu4Model({})
  bu3: Bu3Model = new MyBu3Model({})
  search = ""
  constructor(private bu4Service: Bu4Service,
    private bu3Service: Bu3Service
  ) { }
  ngOnInit(): void {
    this.getBu4List()
  }
  getBu4List() {
    this.bu4Service.getList().subscribe(response => {
      this.bu4List = response
      this.searchChange()
    })
  }
  searchChange() {
    this.currentPage = 1
    this.page = Array.from({ length: Math.ceil(this.bu4ListFilter().length / 10) }, (_, i) => i + 1);
  }
  bu4ListFilter() {
    return this.bu4List.filter(x => x.bu4id.toLowerCase().includes(this.search) ||
      x.tdesc.toLowerCase().includes(this.search) ||
      x.edesc.toLowerCase().includes(this.search))
  }
  selectBu4(bu4: Bu4Model) {
    this.bu3Service.getById(bu4.parent).subscribe(response => {
      this.bu3 = response
    })
    this.bu4 = new MyBu4Model(bu4)
  }
  addBu4() {
    // this.bu4Service.post(this.bu4).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu4List()
    //   }
    // })
  }
  deleteBu4(bu4: Bu4Model) {
    // this.bu4Service.delete(new MyBu4Model(bu4)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu4List()
    //   }
    // })
  }

}


