department-list.component.ts 4.53 KB
Newer Older
1
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, } from '@angular/core';
2
import { ToastrService } from 'ngx-toastr';
LAPTOP-CV4JFSHE\kantavee committed
3 4 5 6
import { Bu1Model, MyBu1Model } from 'src/app/shared/model/bu1.model';
import { Bu2Model, MyBu2Model } from 'src/app/shared/model/bu2.model';
import { Bu1Service } from 'src/app/shared/services/bu1.service';
import { Bu2Service } from 'src/app/shared/services/bu2.service';
7 8 9 10 11
interface table {
  currentPage: number,
  page: number[],
  search: string
}
12 13 14 15 16
@Component({
  selector: 'app-department-list',
  templateUrl: './department-list.component.html',
  styleUrls: ['./department-list.component.scss']
})
LAPTOP-CV4JFSHE\kantavee committed
17 18 19
export class DepartmentListComponent implements OnInit {
  bu2List: Bu2Model[] = []
  bu2: Bu2Model = new MyBu2Model({})
20 21 22 23 24 25 26 27 28 29 30
  bu2Table: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  bu2Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  bu1List: Bu1Model[] = []
LAPTOP-CV4JFSHE\kantavee committed
31
  bu1: Bu1Model = new MyBu1Model({})
32 33 34 35 36 37
  bu1Modal: table = {
    currentPage: 1,
    page: Array.from({ length: 1 }, (_, i) => i + 1),
    search: ""
  }
  currentModal = ""
LAPTOP-CV4JFSHE\kantavee committed
38
  constructor(private bu2Service: Bu2Service,
39
    private bu1Service: Bu1Service,
40 41
    private toastr: ToastrService,
    private cdr: ChangeDetectorRef
LAPTOP-CV4JFSHE\kantavee committed
42 43 44
  ) { }
  ngOnInit(): void {
    this.getBu2List()
45
    this.getBu1List()
46
  }
LAPTOP-CV4JFSHE\kantavee committed
47
  getBu2List() {
48
    this.bu2Service.getList().subscribe(response => {
LAPTOP-CV4JFSHE\kantavee committed
49
      this.bu2List = response
50
      this.onBu2TableSearchChange()
LAPTOP-CV4JFSHE\kantavee committed
51
    })
52
  }
53 54 55
  onBu2TableSearchChange() {
    this.bu2Table.currentPage = 1
    this.bu2Table.page = Array.from({ length: Math.ceil(this.filterBu2Table().length / 10) }, (_, i) => i + 1);
LAPTOP-CV4JFSHE\kantavee committed
56
  }
57 58 59 60
  filterBu2Table() {
    return this.bu2List.filter(x => x.bu2id.toLowerCase().includes(this.bu2Table.search) ||
      x.tdesc.toLowerCase().includes(this.bu2Table.search) ||
      x.edesc.toLowerCase().includes(this.bu2Table.search))
LAPTOP-CV4JFSHE\kantavee committed
61
  }
62 63 64 65
  selectBu2(bu2?: Bu2Model) {
    this.bu2 = new MyBu2Model(bu2 || {})
    this.selectBu1()
    if (this.bu2.parent) {
66
      this.bu1Service.getById(this.bu2.parent).subscribe(response => {
67
        this.bu1 = new MyBu1Model(response)
68
        this.cdr.detectChanges()
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
      })
    }
  }
  bu2idChange() {
    const bu2 = this.bu2List.find(x => x.bu2id == this.bu2.bu2id)
    this.selectBu2(bu2 || new MyBu2Model({ bu2id: this.bu2.bu2id }))
  }
  onBu2ModalSearchChange() {
    this.bu2Modal.currentPage = 1
    this.bu2Modal.page = Array.from({ length: Math.ceil(this.filterBu2Modal().length / 10) }, (_, i) => i + 1);
  }
  filterBu2Modal() {
    return this.bu2List.filter(x => x.bu2id.toLowerCase().includes(this.bu2Modal.search) ||
      x.tdesc.toLowerCase().includes(this.bu2Modal.search) ||
      x.edesc.toLowerCase().includes(this.bu2Modal.search))
LAPTOP-CV4JFSHE\kantavee committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
  }
  addBu2() {
    // this.bu2Service.post(this.bu2).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu2List()
    //   }
    // })
  }
  deleteBu2(bu2: Bu2Model) {
    // this.bu2Service.delete(new MyBu2Model(bu2)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getBu2List()
    //   }
    // })
98 99
  }

100
  getBu1List() {
101
    this.bu1Service.getList().subscribe(response => {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      this.bu1List = response
      this.onBu1ModalSearchChange()
    })
  }
  onBu1ModalSearchChange() {
    this.bu1Modal.currentPage = 1
    this.bu1Modal.page = Array.from({ length: Math.ceil(this.filterBu1Modal().length / 10) }, (_, i) => i + 1);
  }
  bu1idChange() {
    const bu1 = this.bu1List.find(x => x.bu1id == this.bu1.bu1id)
    this.selectBu1(bu1 || new MyBu1Model({ bu1id: this.bu1.bu1id }))
  }
  filterBu1Modal() {
    return this.bu1List.filter(x => x.bu1id.toLowerCase().includes(this.bu1Modal.search) ||
      x.tdesc.toLowerCase().includes(this.bu1Modal.search) ||
      x.edesc.toLowerCase().includes(this.bu1Modal.search))
  }
  selectBu1(bu1?: Bu1Model) {
    this.bu1 = new MyBu1Model(bu1 || {})
  }

  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