import { Component, EventEmitter, Input, OnInit } from '@angular/core';
import { Bu5Model, MyBu5Model } from 'src/app/shared/model/bu5.model';
import { Bu6Model, MyBu6Model } from 'src/app/shared/model/bu6.model';
import { Bu5Service } from 'src/app/shared/services/bu5.service';
import { Bu6Service } from 'src/app/shared/services/bu6.service';

@Component({
  selector: 'app-sub-department-three',
  templateUrl: './sub-department-three.component.html',
  styleUrls: ['./sub-department-three.component.scss']
})
export class SubDepartmentThreeComponent implements OnInit {
  currentPage = 1
  page = Array.from({ length: 1 }, (_, i) => i + 1);
  bu6List: Bu6Model[] = []
  bu6: Bu6Model = new MyBu6Model({})
  bu5: Bu5Model = new MyBu5Model({})
  search = ""
  constructor(private bu6Service: Bu6Service,
    private bu5Service: Bu5Service
  ) { }
  ngOnInit(): void {
    this.getBu6List()
  }
  getBu6List() {
    this.bu6Service.getList().subscribe(response => {
      this.bu6List = response
      this.searchChange()
    })
  }
  searchChange() {
    this.currentPage = 1
    this.page = Array.from({ length: Math.ceil(this.bu6ListFilter().length / 10) }, (_, i) => i + 1);
  }
  bu6ListFilter() {
    return this.bu6List.filter(x => x.bu6id.toLowerCase().includes(this.search) ||
      x.tdesc.toLowerCase().includes(this.search) ||
      x.edesc.toLowerCase().includes(this.search))
  }
  selectBu6(bu6: Bu6Model) {
    this.bu5Service.getById(bu6.parent).subscribe(response =>{
      this.bu5 =  new MyBu5Model(response)
    })
    this.bu6 = new MyBu6Model(bu6)
  
  }
  addBu6() {
    // this.bu6Service.post(this.bu6).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu6List()
    //   }
    // })
  }
  deleteBu6(bu6: Bu6Model) {
    // this.bu6Service.delete(new MyBu1Model(bu6)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu6List()
    //   }
    // })
  }

}
