employee-categories.component.ts 5.41 KB
Newer Older
1
import { Component, EventEmitter, Input, Output } from '@angular/core';
2 3 4
import { ToastrService } from 'ngx-toastr';
import { EmpTypeModel, MyEmpTypeModel } from 'src/app/shared/model/employee-type.model';
import { EmpTypeService } from 'src/app/shared/services/employee-type.service';
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

@Component({
  selector: 'app-employee-categories',
  templateUrl: './employee-categories.component.html',
  styleUrls: ['./employee-categories.component.scss']
})
export class EmployeeCategories {
  @Input() pathTitle = ['การจัดการข้อมูลองค์กร', 'ข้อมูลลักษณะงาน', 'กลุ่มพนักงาน']
  @Output() sendPathTitle: EventEmitter<string[]> = new EventEmitter<string[]>();
  activeTab: string = 'tab1'; // กำหนด tab เริ่มต้น

  // ฟังก์ชันในการเปลี่ยนแท็บ
  changeTab(tab: { id: string, text: string }) {
    this.sendPathTitle.emit(['การจัดการข้อมูลองค์กร', 'ข้อมูลลักษณะงาน', tab.text])
    this.activeTab = tab.id;
  }
21 22 23 24 25 26

  currentPage = 1
  page = Array.from({ length: 1 }, (_, i) => i + 1);
  emp_typeList: EmpTypeModel[] = []
  emp_type: EmpTypeModel = new MyEmpTypeModel({})
  search = ""
27
  itemToDelete: EmpTypeModel | null = null;
28 29 30 31 32 33 34
  constructor(private EmpTypeService: EmpTypeService,
    private toastr: ToastrService
  ) { }
  ngOnInit(): void {
    this.getEmpTypeList()
  }

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  modalOptions: {
    [nameModal: string]: {  // ชื่อตรวจสอบการเปิดปิด
      isModalOpen: boolean; // เปิด/ปิด 
      modalSize: string; // ขนาดของ Modal  (s,m,l,vw10-vw100 )
      backdropClose: boolean; // (คลิก Backdrop แล้ว true ปิด false ไม่ปิด )
    }
  } = {
      "add": {
        isModalOpen: false,
        modalSize: 'm',
        backdropClose: true,
      },
      "edit": {
        isModalOpen: false,
        modalSize: 'm',
        backdropClose: true,
51 52 53 54 55
      },
      "upload":{
        isModalOpen: false,
        modalSize: 'm',
        backdropClose: true,
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
      }
    }

  openModal(name: string, size: string, closeOnBackdrop?: boolean) {
    this.modalOptions[name].modalSize = size;
    this.modalOptions[name].backdropClose = closeOnBackdrop || false;
    this.modalOptions[name].isModalOpen = true;
    document.body.style.overflow = 'hidden'; // ล็อก Scroll
  }

  closeModal(name: string) {
    this.modalOptions[name].isModalOpen = false;
    // ตรวจสอบว่ามี Modal อื่นเปิดอยู่หรือไม่
    if (!this.isAnyModalOpen()) {
      document.body.style.overflow = ''; // คืนค่าการ Scroll เฉพาะเมื่อ Modal ทั้งหมดปิดแล้ว
    }
  }

  isAnyModalOpen(): boolean {
    // Logic ตรวจสอบว่า Modal อื่นยังเปิดอยู่หรือไม่
    return Object.values(this.modalOptions).some(modal => modal.isModalOpen); // หากไม่มี Modal อื่นเปิด
  }
78 79 80 81 82 83 84 85 86 87 88
  getEmpTypeList() {
    this.EmpTypeService.getList().subscribe(response => {
      this.emp_typeList = response
      this.searchChange()
    })
  }
  searchChange() {
    this.currentPage = 1
    this.page = Array.from({ length: Math.ceil(this.emp_typeListFilter().length / 10) }, (_, i) => i + 1);
  }
  emp_typeListFilter() {
89 90
    return this.emp_typeList.filter(x => 
      x.codeId.toLowerCase().includes(this.search) ||
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      x.tdesc.toLowerCase().includes(this.search) ||
      x.edesc.toLowerCase().includes(this.search))
  }
  selectEmp_type(emp_type: EmpTypeModel) {
    // this.showSuccess()
    this.emp_type = new MyEmpTypeModel(emp_type)
  }
  addEmp_type() {
    // this.emp_typeService.post(this.emp_type).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getemp_typeList()
    //   }
    // })
  }
  deleteEmp_type(emp_type: EmpTypeModel) {
    // this.emp_typeService.delete(new MyEmpTypeModel(emp_type)).subscribe((response:any) => {
    //   if (response.success) {
    //     this.getemp_typeList()
    //   }
    // })
  }
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133


  openDeleteModal(item: EmpTypeModel) {
    this.itemToDelete = item; // เก็บข้อมูลที่ต้องการลบ
  }

  deleteSelected() {
    if (this.itemToDelete) {
      // ลบ item ที่ถูกเลือกออกจาก emp_typeList
      this.emp_typeList = this.emp_typeList.filter(item => item !== this.itemToDelete);
    }
  }

  // ฟังก์ชันลบข้อมูลที่เลือก
  deleteSelectedItems() {
    this.emp_typeList = this.emp_typeList.filter(item => !item['selected']);
  }

  onCheckboxChange(emp_type: EmpTypeModel) {
    console.log('Checkbox changed:', emp_type);
  }

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  showSuccessAdd() {
    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',
    });
  }
152

153
}