import { Component, EventEmitter, Input, OnInit } from '@angular/core'; import { ToastrService } from 'ngx-toastr'; import { Bu3Model, MyBu3Model } from 'src/app/shared/model/bu3.model'; import { Bu4Model, MyBu4Model } from 'src/app/shared/model/bu4.model'; import { Bu3Service } from 'src/app/shared/services/bu3.service'; import { Bu4Service } from 'src/app/shared/services/bu4.service'; interface table { currentPage: number, page: number[], search: string } @Component({ selector: 'app-sub-department-one', templateUrl: './sub-department-one.component.html', styleUrls: ['./sub-department-one.component.scss'] }) export class SubDepartmentOneComponent implements OnInit { bu4List: Bu4Model[] = [] bu4: Bu4Model = new MyBu4Model({}) bu4Table: table = { currentPage: 1, page: Array.from({ length: 1 }, (_, i) => i + 1), search: "" } bu4Modal: table = { currentPage: 1, page: Array.from({ length: 1 }, (_, i) => i + 1), search: "" } bu3List: Bu3Model[] = [] bu3: Bu3Model = new MyBu3Model({}) bu3Modal: table = { currentPage: 1, page: Array.from({ length: 1 }, (_, i) => i + 1), search: "" } currentModal = "" constructor(private bu4Service: Bu4Service, private bu3Service: Bu3Service, private toastr: ToastrService ) { } ngOnInit(): void { this.getBu4List() this.getBu3List() } getBu4List() { this.bu4Service.getList().subscribe(response => { this.bu4List = response this.onBu4TableSearchChange() }) } onBu4TableSearchChange() { this.bu4Table.currentPage = 1 this.bu4Table.page = Array.from({ length: Math.ceil(this.filterBu4Table().length / 10) }, (_, i) => i + 1); } filterBu4Table() { return this.bu4List.filter(x => x.bu4id.toLowerCase().includes(this.bu4Table.search) || x.tdesc.toLowerCase().includes(this.bu4Table.search) || x.edesc.toLowerCase().includes(this.bu4Table.search)) } selectBu4(bu4?: Bu4Model) { this.bu4 = new MyBu4Model(bu4 || {}) this.selectBu3() if (this.bu4.parent) { this.bu3Service.getById(this.bu4.parent).subscribe(response => { this.bu3 = new MyBu3Model(response) }) } } bu4idChange() { const bu4 = this.bu4List.find(x => x.bu4id == this.bu4.bu4id) this.selectBu4(bu4 || new MyBu4Model({ bu4id: this.bu4.bu4id })) } onBu4ModalSearchChange() { this.bu4Modal.currentPage = 1 this.bu4Modal.page = Array.from({ length: Math.ceil(this.filterBu4Modal().length / 10) }, (_, i) => i + 1); } filterBu4Modal() { return this.bu4List.filter(x => x.bu4id.toLowerCase().includes(this.bu4Modal.search) || x.tdesc.toLowerCase().includes(this.bu4Modal.search) || x.edesc.toLowerCase().includes(this.bu4Modal.search)) } addBu4() { // this.bu4Service.post(this.bu4).subscribe((response:any) => { // if (response.success) { // this.getBu4List() // } // }) } deleteBu4(bu4: Bu4Model) { // this.bu4Service.delete(new MyBu4Model(bu4)).subscribe((response:any) => { // if (response.success) { // this.getBu4List() // } // }) } getBu3List() { this.bu3Service.getList().subscribe(response => { this.bu3List = response this.onBu3ModalSearchChange() }) } onBu3ModalSearchChange() { this.bu3Modal.currentPage = 1 this.bu3Modal.page = Array.from({ length: Math.ceil(this.filterBu3Modal().length / 10) }, (_, i) => i + 1); } bu3idChange() { const bu3 = this.bu3List.find(x => x.bu3id == this.bu3.bu3id) this.selectBu3(bu3 || new MyBu3Model({ bu3id: this.bu3.bu3id })) } filterBu3Modal() { return this.bu3List.filter(x => x.bu3id.toLowerCase().includes(this.bu3Modal.search) || x.tdesc.toLowerCase().includes(this.bu3Modal.search) || x.edesc.toLowerCase().includes(this.bu3Modal.search)) } selectBu3(bu3?: Bu3Model) { this.bu3 = new MyBu3Model(bu3 || {}) } showSuccess() { 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', }); } }