import { Component, OnInit } from '@angular/core';
import { Bu6Model, MyBu6Model } from 'src/app/shared/model/bu6.model';
import { Bu7Model, MyBu7Model } from 'src/app/shared/model/bu7.model';
import { Bu6Service } from 'src/app/shared/services/bu6.service';
import { Bu7Service } from 'src/app/shared/services/bu7.service';

@Component({
  selector: 'app-sub-department-four',
  templateUrl: './sub-department-four.component.html',
  styleUrls: ['./sub-department-four.component.scss']
})
export class SubDepartmentFourComponent implements OnInit {
  currentPage = 1
  page = Array.from({ length: 1 }, (_, i) => i + 1);
  bu7List: Bu7Model[] = []
  bu7: Bu7Model = new MyBu7Model({})
  bu6: Bu6Model = new MyBu6Model({})
  search = ""
  constructor(private bu7Service: Bu7Service,
    private bu6Service: Bu6Service
  ) { }
  ngOnInit(): void {
    this.getBu7List()
  }
  getBu7List() {
    this.bu7Service.getList().subscribe(response => {
      this.bu7List = response
      this.searchChange()
    })
  }
  searchChange() {
    this.currentPage = 1
    this.page = Array.from({ length: Math.ceil(this.bu7ListFilter().length / 10) }, (_, i) => i + 1);
  }
  bu7ListFilter() {
    return this.bu7List.filter(x => x.bu7id.toLowerCase().includes(this.search) ||
      x.tdesc.toLowerCase().includes(this.search) ||
      x.edesc.toLowerCase().includes(this.search))
  }
  selectBu7(bu7: Bu7Model) {
    this.bu6Service.getById(bu7.parent).subscribe(response =>{
      this.bu6 =  new MyBu6Model(response)
    })
    this.bu7 = new MyBu7Model(bu7)
  }
  addBu7() {
    // this.bu7Service.post(this.bu7).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu7List()
    //   }
    // })
  }
  deleteBu7(bu7: Bu7Model) {
    // this.bu7Service.delete(new MyBu1Model(bu7)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu7List()
    //   }
    // })
  }

}
