sub-department-four.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 { 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';
7

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

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