import { Component, EventEmitter, Input, OnInit, } from '@angular/core';
import { Bu2Model, MyBu2Model } from 'src/app/shared/model/bu2.model';
import { Bu3Model, MyBu3Model } from 'src/app/shared/model/bu3.model';
import { Bu2Service } from 'src/app/shared/services/bu2.service';
import { Bu3Service } from 'src/app/shared/services/bu3.service';


@Component({
  selector: 'app-section-registration',
  templateUrl: './section-registration.component.html',
  styleUrls: ['./section-registration.component.scss']
})
export class SectionRegistrationComponent implements OnInit {
  currentPage = 1
  page = Array.from({ length: 1 }, (_, i) => i + 1);
  bu3List: Bu3Model[] = []
  bu3: Bu3Model = new MyBu3Model({})
  bu2: Bu2Model = new MyBu2Model({})
  search = ""
  constructor(private bu3Service: Bu3Service,
    private bu2Service: Bu2Service
  ) { }
  ngOnInit(): void {
    this.getBu3List()
  }
  getBu3List() {
    this.bu3Service.getList().subscribe(response => {
      this.bu3List = response
      this.searchChange()
    })
  }
  searchChange() {
    this.currentPage = 1
    this.page = Array.from({ length: Math.ceil(this.bu3ListFilter().length / 10) }, (_, i) => i + 1);
  }
  bu3ListFilter() {
    return this.bu3List.filter(x => x.bu3id.toLowerCase().includes(this.search) ||
      x.tdesc.toLowerCase().includes(this.search) ||
      x.edesc.toLowerCase().includes(this.search))
  }
  selectBu3(bu3: Bu3Model) {
    this.bu2Service.getById(bu3.parent).subscribe(response =>{
      this.bu2 = response
    })
    this.bu3 = new MyBu3Model(bu3)
  }
  addBu3() {
    // this.bu3Service.post(this.bu3).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu3List()
    //   }
    // })
  }
  deleteBu3(bu3: Bu3Model) {
    // this.bu3Service.delete(new MyBu1Model(bu3)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu3List()
    //   }
    // })
  }

}
