import { Component, EventEmitter, Input, OnInit, } from '@angular/core';
import { Bu1Model, MyBu1Model } from 'src/app/shared/model/bu1.model';
import { Bu2Model, MyBu2Model } from 'src/app/shared/model/bu2.model';
import { Bu1Service } from 'src/app/shared/services/bu1.service';
import { Bu2Service } from 'src/app/shared/services/bu2.service';


@Component({
  selector: 'app-department-list',
  templateUrl: './department-list.component.html',
  styleUrls: ['./department-list.component.scss']
})
export class DepartmentListComponent implements OnInit {
  currentPage = 1
  page = Array.from({ length: 1 }, (_, i) => i + 1);
  bu2List: Bu2Model[] = []
  bu2: Bu2Model = new MyBu2Model({})
  bu1: Bu1Model = new MyBu1Model({})
  search = ""
  constructor(private bu2Service: Bu2Service,
    private bu1Service: Bu1Service
  ) { }
  ngOnInit(): void {
    this.getBu2List()
  }
  getBu2List() {
    this.bu2Service.getList().subscribe(response => {
      this.bu2List = response
      this.searchChange()
    })
  }
  searchChange() {
    this.currentPage = 1
    this.page = Array.from({ length: Math.ceil(this.bu2ListFilter().length / 10) }, (_, i) => i + 1);
  }
  bu2ListFilter() {
    return this.bu2List.filter(x => x.bu2id.toLowerCase().includes(this.search) ||
      x.tdesc.toLowerCase().includes(this.search) ||
      x.edesc.toLowerCase().includes(this.search))
  }
  selectBu2(bu2: Bu2Model) {
    this.bu1Service.getById(bu2.parent).subscribe(response => {
      this.bu1 = response
    })
    this.bu2 = new MyBu2Model(bu2)
  }
  addBu2() {
    // this.bu2Service.post(this.bu2).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu2List()
    //   }
    // })
  }
  deleteBu2(bu2: Bu2Model) {
    // this.bu2Service.delete(new MyBu2Model(bu2)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu2List()
    //   }
    // })
  }

}

