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

interface table {
  currentPage: number,
  page: number[],
  search: string
}
@Component({
  selector: 'app-sub-department-four',
  templateUrl: './sub-department-four.component.html',
  styleUrls: ['./sub-department-four.component.scss']
})
export class SubDepartmentFourComponent implements OnInit {
  bu7List: Bu7Model[] = []
  bu7: Bu7Model = new MyBu7Model({})
  bu7Table: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  bu7Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  bu6List: Bu6Model[] = []
  bu6: Bu6Model = new MyBu6Model({})
  bu6Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  currentModal = ""
  constructor(private bu7Service: Bu7Service,
    private bu6Service: Bu6Service,
    private toastr: ToastrService,
    private cdr: ChangeDetectorRef
  ) { }
  ngOnInit(): void {
    this.getBu7List()
    this.getBu6List()
  }
  getBu7List() {
    this.bu7Service.getList().subscribe(response => {
      this.bu7List = response
      this.onBu7TableSearchChange()
    })
  }
  onBu7TableSearchChange() {
    this.bu7Table.currentPage = 1
    this.bu7Table.page = Array.from({ length: Math.ceil(this.filterBu7Table().length / 10) }, (_, i) => i + 1);
  }
  filterBu7Table() {
    return this.bu7List.filter(x => x.bu7id.toLowerCase().includes(this.bu7Table.search) ||
      x.tdesc.toLowerCase().includes(this.bu7Table.search) ||
      x.edesc.toLowerCase().includes(this.bu7Table.search))
  }
  selectBu7(bu7?: Bu7Model) {
    this.bu7 = new MyBu7Model(bu7 || {})
    this.selectBu6()
    if (this.bu7.parent) {
      this.bu6Service.getById(this.bu7.parent).subscribe(response => {
        this.bu6 = new MyBu6Model(response)
        this.cdr.detectChanges()
      })
    }
  }
  bu7idChange() {
    const bu7 = this.bu7List.find(x => x.bu7id == this.bu7.bu7id)
    this.selectBu7(bu7 || new MyBu7Model({ bu7id: this.bu7.bu7id }))
  }
  onBu7ModalSearchChange() {
    this.bu7Modal.currentPage = 1
    this.bu7Modal.page = Array.from({ length: Math.ceil(this.filterBu7Modal().length / 10) }, (_, i) => i + 1);
  }
  filterBu7Modal() {
    return this.bu7List.filter(x => x.bu7id.toLowerCase().includes(this.bu7Modal.search) ||
      x.tdesc.toLowerCase().includes(this.bu7Modal.search) ||
      x.edesc.toLowerCase().includes(this.bu7Modal.search))
  }
  addBu7() {
    // this.bu7Service.post(this.bu7).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu7List()
    //   }
    // })
  }
  deleteBu7(bu7: Bu7Model) {
    // this.bu7Service.delete(new MyBu7Model(bu7)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu7List()
    //   }
    // })
  }

  getBu6List() {
    this.bu6Service.getList().subscribe(response => {
      this.bu6List = response
      this.onBu6ModalSearchChange()
    })
  }
  onBu6ModalSearchChange() {
    this.bu6Modal.currentPage = 1
    this.bu6Modal.page = Array.from({ length: Math.ceil(this.filterBu6Modal().length / 10) }, (_, i) => i + 1);
  }
  bu6idChange() {
    const bu6 = this.bu6List.find(x => x.bu6id == this.bu6.bu6id)
    this.selectBu6(bu6 || new MyBu6Model({ bu6id: this.bu6.bu6id }))
  }
  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))
  }
  selectBu6(bu6?: Bu6Model) {
    this.bu6 = new MyBu6Model(bu6 || {})
  }

  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',
    });
  }
}