import { Component, EventEmitter, Input, OnInit } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
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';

interface table {
  currentPage: number,
  page: number[],
  search: string
}
@Component({
  selector: 'app-sub-department-three',
  templateUrl: './sub-department-three.component.html',
  styleUrls: ['./sub-department-three.component.scss']
})
export class SubDepartmentThreeComponent implements OnInit {
  bu6List: Bu6Model[] = []
  bu6: Bu6Model = new MyBu6Model({})
  bu6Table: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  bu6Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  bu5List: Bu5Model[] = []
  bu5: Bu5Model = new MyBu5Model({})
  bu5Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  currentModal = ""
  constructor(private bu6Service: Bu6Service,
    private bu5Service: Bu5Service,
    private toastr: ToastrService
  ) { }
  ngOnInit(): void {
    this.getBu6List()
    this.getBu5List()
  }
  getBu6List() {
    this.bu6Service.getList().subscribe(response => {
      this.bu6List = response
      this.onBu6TableSearchChange()
    })
  }
  onBu6TableSearchChange() {
    this.bu6Table.currentPage = 1
    this.bu6Table.page = Array.from({ length: Math.ceil(this.filterBu6Table().length / 10) }, (_, i) => i + 1);
  }
  filterBu6Table() {
    return this.bu6List.filter(x => x.bu6id.toLowerCase().includes(this.bu6Table.search) ||
      x.tdesc.toLowerCase().includes(this.bu6Table.search) ||
      x.edesc.toLowerCase().includes(this.bu6Table.search))
  }
  selectBu6(bu6?: Bu6Model) {
    this.bu6 = new MyBu6Model(bu6 || {})
    this.selectBu5()
    if (this.bu6.parent) {
      this.bu5Service.getById(this.bu6.parent).subscribe(response => {
        this.bu5 = new MyBu5Model(response)
      })
    }
  }
  bu6idChange() {
    const bu6 = this.bu6List.find(x => x.bu6id == this.bu6.bu6id)
    this.selectBu6(bu6 || new MyBu6Model({ bu6id: this.bu6.bu6id }))
  }
  onBu6ModalSearchChange() {
    this.bu6Modal.currentPage = 1
    this.bu6Modal.page = Array.from({ length: Math.ceil(this.filterBu6Modal().length / 10) }, (_, i) => i + 1);
  }
  filterBu6Modal() {
    return this.bu6List.filter(x => x.bu6id.toLowerCase().includes(this.bu6Modal.search) ||
      x.tdesc.toLowerCase().includes(this.bu6Modal.search) ||
      x.edesc.toLowerCase().includes(this.bu6Modal.search))
  }
  addBu6() {
    // this.bu6Service.post(this.bu6).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu6List()
    //   }
    // })
  }
  deleteBu6(bu6: Bu6Model) {
    // this.bu6Service.delete(new MyBu6Model(bu6)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu6List()
    //   }
    // })
  }

  getBu5List() {
    this.bu5Service.getList().subscribe(response => {
      this.bu5List = response
      this.onBu5ModalSearchChange()
    })
  }
  onBu5ModalSearchChange() {
    this.bu5Modal.currentPage = 1
    this.bu5Modal.page = Array.from({ length: Math.ceil(this.filterBu5Modal().length / 10) }, (_, i) => i + 1);
  }
  bu5idChange() {
    const bu5 = this.bu5List.find(x => x.bu5id == this.bu5.bu5id)
    this.selectBu5(bu5 || new MyBu5Model({ bu5id: this.bu5.bu5id }))
  }
  filterBu5Modal() {
    return this.bu5List.filter(x => x.bu5id.toLowerCase().includes(this.bu5Modal.search) ||
      x.tdesc.toLowerCase().includes(this.bu5Modal.search) ||
      x.edesc.toLowerCase().includes(this.bu5Modal.search))
  }
  selectBu5(bu5?: Bu5Model) {
    this.bu5 = new MyBu5Model(bu5 || {})
  }

  showSuccessAdd() {
    this.toastr.success('บันทึกข้อมูลสำเร็จ', 'แจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
  showSuccessEdit() {
    this.toastr.success('แก้ไขข้อมูลสำเร็จ', 'แจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
  showSuccessDelete() {
    this.toastr.success('ลบข้อมูลสำเร็จ', 'แจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
}