import { Component, EventEmitter, Input, OnInit, } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
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';
interface table {
  currentPage: number,
  page: number[],
  search: string
}
@Component({
  selector: 'app-department-list',
  templateUrl: './department-list.component.html',
  styleUrls: ['./department-list.component.scss']
})
export class DepartmentListComponent implements OnInit {
  bu2List: Bu2Model[] = []
  bu2: Bu2Model = new MyBu2Model({})
  bu2Table: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  bu2Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  bu1List: Bu1Model[] = []
  bu1: Bu1Model = new MyBu1Model({})
  bu1Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  currentModal = ""
  constructor(private bu2Service: Bu2Service,
    private bu1Service: Bu1Service,
    private toastr: ToastrService
  ) { }
  ngOnInit(): void {
    this.getBu2List()
    this.getBu1List()
  }
  getBu2List() {
    this.bu2Service.getList().subscribe(response => {
      this.bu2List = response
      this.onBu2TableSearchChange()
    })
  }
  onBu2TableSearchChange() {
    this.bu2Table.currentPage = 1
    this.bu2Table.page = Array.from({ length: Math.ceil(this.filterBu2Table().length / 10) }, (_, i) => i + 1);
  }
  filterBu2Table() {
    return this.bu2List.filter(x => x.bu2id.toLowerCase().includes(this.bu2Table.search) ||
      x.tdesc.toLowerCase().includes(this.bu2Table.search) ||
      x.edesc.toLowerCase().includes(this.bu2Table.search))
  }
  selectBu2(bu2?: Bu2Model) {
    this.bu2 = new MyBu2Model(bu2 || {})
    this.selectBu1()
    if (this.bu2.parent) {
      this.bu1Service.getById(this.bu2.parent).subscribe(response => {
        this.bu1 = new MyBu1Model(response)
      })
    }
  }
  bu2idChange() {
    const bu2 = this.bu2List.find(x => x.bu2id == this.bu2.bu2id)
    this.selectBu2(bu2 || new MyBu2Model({ bu2id: this.bu2.bu2id }))
  }
  onBu2ModalSearchChange() {
    this.bu2Modal.currentPage = 1
    this.bu2Modal.page = Array.from({ length: Math.ceil(this.filterBu2Modal().length / 10) }, (_, i) => i + 1);
  }
  filterBu2Modal() {
    return this.bu2List.filter(x => x.bu2id.toLowerCase().includes(this.bu2Modal.search) ||
      x.tdesc.toLowerCase().includes(this.bu2Modal.search) ||
      x.edesc.toLowerCase().includes(this.bu2Modal.search))
  }
  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()
    //   }
    // })
  }

  getBu1List() {
    this.bu1Service.getList().subscribe(response => {
      this.bu1List = response
      this.onBu1ModalSearchChange()
    })
  }
  onBu1ModalSearchChange() {
    this.bu1Modal.currentPage = 1
    this.bu1Modal.page = Array.from({ length: Math.ceil(this.filterBu1Modal().length / 10) }, (_, i) => i + 1);
  }
  bu1idChange() {
    const bu1 = this.bu1List.find(x => x.bu1id == this.bu1.bu1id)
    this.selectBu1(bu1 || new MyBu1Model({ bu1id: this.bu1.bu1id }))
  }
  filterBu1Modal() {
    return this.bu1List.filter(x => x.bu1id.toLowerCase().includes(this.bu1Modal.search) ||
      x.tdesc.toLowerCase().includes(this.bu1Modal.search) ||
      x.edesc.toLowerCase().includes(this.bu1Modal.search))
  }
  selectBu1(bu1?: Bu1Model) {
    this.bu1 = new MyBu1Model(bu1 || {})
  }

  showSuccess() {
    this.toastr.success('This is an example of tip', 'TIP', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
}