sub-department-three.component.ts 4.54 KB
Newer Older
1
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit } from '@angular/core';
2
import { ToastrService } from 'ngx-toastr';
Natthaphat Pankiang committed
3 4 5 6
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';
7

8 9 10 11 12
interface table {
  currentPage: number,
  page: number[],
  search: string
}
13 14 15 16 17
@Component({
  selector: 'app-sub-department-three',
  templateUrl: './sub-department-three.component.html',
  styleUrls: ['./sub-department-three.component.scss']
})
Natthaphat Pankiang committed
18 19 20
export class SubDepartmentThreeComponent implements OnInit {
  bu6List: Bu6Model[] = []
  bu6: Bu6Model = new MyBu6Model({})
21 22 23 24 25 26 27 28 29 30 31
  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[] = []
Natthaphat Pankiang committed
32
  bu5: Bu5Model = new MyBu5Model({})
33 34 35 36 37 38
  bu5Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  currentModal = ""
Natthaphat Pankiang committed
39
  constructor(private bu6Service: Bu6Service,
40
    private bu5Service: Bu5Service,
41 42
    private toastr: ToastrService,
    private cdr: ChangeDetectorRef
Natthaphat Pankiang committed
43 44 45
  ) { }
  ngOnInit(): void {
    this.getBu6List()
46
    this.getBu5List()
Natthaphat Pankiang committed
47
  }
Natthaphat Pankiang committed
48 49 50
  getBu6List() {
    this.bu6Service.getList().subscribe(response => {
      this.bu6List = response
51
      this.onBu6TableSearchChange()
Natthaphat Pankiang committed
52
    })
Natthaphat Pankiang committed
53
  }
54 55 56
  onBu6TableSearchChange() {
    this.bu6Table.currentPage = 1
    this.bu6Table.page = Array.from({ length: Math.ceil(this.filterBu6Table().length / 10) }, (_, i) => i + 1);
Natthaphat Pankiang committed
57
  }
58 59 60 61
  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))
Natthaphat Pankiang committed
62
  }
63 64 65 66 67 68
  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)
69
        this.cdr.detectChanges()
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      })
    }
  }
  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))
Natthaphat Pankiang committed
85 86 87 88 89 90 91 92 93
  }
  addBu6() {
    // this.bu6Service.post(this.bu6).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu6List()
    //   }
    // })
  }
  deleteBu6(bu6: Bu6Model) {
94
    // this.bu6Service.delete(new MyBu6Model(bu6)).subscribe((response:any) => {
Natthaphat Pankiang committed
95 96 97 98
    //   if (response.success) {
    //     this.getBu6List()
    //   }
    // })
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
  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 || {})
  }

124 125 126 127 128 129 130 131 132 133 134 135 136 137
  showSuccessAdd() {
    this.toastr.success('บันทึกข้อมูลสำเร็จ', 'แจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
  showSuccessEdit() {
    this.toastr.success('แก้ไขข้อมูลสำเร็จ', 'แจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
  showSuccessDelete() {
    this.toastr.success('ลบข้อมูลสำเร็จ', 'แจ้งเตือน', {
138 139 140 141
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
142
}
143 144