sub-department-one.component.ts 4.54 KB
Newer Older
1
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit } from '@angular/core';
2
import { ToastrService } from 'ngx-toastr';
3 4 5 6
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';
7 8 9 10 11
interface table {
  currentPage: number,
  page: number[],
  search: string
}
12 13 14 15 16
@Component({
  selector: 'app-sub-department-one',
  templateUrl: './sub-department-one.component.html',
  styleUrls: ['./sub-department-one.component.scss']
})
17
export class SubDepartmentOneComponent implements OnInit {
18 19
  bu4List: Bu4Model[] = []
  bu4: Bu4Model = new MyBu4Model({})
20 21 22 23 24 25 26 27 28 29 30
  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[] = []
31
  bu3: Bu3Model = new MyBu3Model({})
32 33 34 35 36 37
  bu3Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  currentModal = ""
38
  constructor(private bu4Service: Bu4Service,
39
    private bu3Service: Bu3Service,
40 41
    private toastr: ToastrService,
    private cdr: ChangeDetectorRef
42 43 44
  ) { }
  ngOnInit(): void {
    this.getBu4List()
45
    this.getBu3List()
46
  }
47 48 49
  getBu4List() {
    this.bu4Service.getList().subscribe(response => {
      this.bu4List = response
50
      this.onBu4TableSearchChange()
51
    })
52
  }
53 54 55
  onBu4TableSearchChange() {
    this.bu4Table.currentPage = 1
    this.bu4Table.page = Array.from({ length: Math.ceil(this.filterBu4Table().length / 10) }, (_, i) => i + 1);
56
  }
57 58 59 60
  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))
61
  }
62 63 64 65 66 67
  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)
68
        this.cdr.detectChanges()
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
      })
    }
  }
  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))
84 85 86 87 88 89 90 91 92 93 94 95 96 97
  }
  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()
    //   }
    // })
98 99
  }

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  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() {
124 125 126 127 128 129 130 131 132 133 134 135 136
    this.toastr.success('บันทึกข้อมูลสำเร็จ', 'เเจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
  showSuccessEdit() {
    this.toastr.success('เเก้ไขข้อมูลสำเร็จ', 'เเจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
  showSuccessDelete() {
    this.toastr.success('ลบข้อมูลสำเร็จ', 'เเจ้งเตือน', {
137 138 139 140
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
141
}
142 143