grade-management.component.ts 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ToastrService } from 'ngx-toastr';

export interface DataModel {
  check: boolean;
  code: string;
  tdesc: string;
  edesc: string;
}

@Component({
  selector: 'app-grade-management',
  templateUrl: './grade-management.component.html',
  styleUrls: ['./grade-management.component.scss'],
})
export class GradeManagementComponent {
  @Output() sendPathTitle: EventEmitter<string[]> = new EventEmitter<string[]>();
  addTab = false;
  editTab = false;
  currentPage = 1;
  page = Array.from({ length: 1 }, (_, i) => i + 1);
  numDataListChecked = 0;
  isDataListChecked = false;
  isDataListCheckedAll = false;
  search = '';

  dataList: DataModel[] = [
    { check: false, code: '01', tdesc: 'กลุ่ม 1', edesc: 'Group 1' },
    { check: false, code: '02', tdesc: 'กลุ่ม 2', edesc: 'Group 2' },
    { check: false, code: '03', tdesc: 'กลุ่ม 3', edesc: 'Group 3' },
    { check: false, code: '04', tdesc: 'กลุ่ม 4', edesc: 'Group 4' },
    { check: false, code: '05', tdesc: 'กลุ่ม 5', edesc: 'Group 5' },
    { check: false, code: '06', tdesc: 'กลุ่ม 6', edesc: 'Group 6' },
    { check: false, code: '07', tdesc: 'กลุ่ม 7', edesc: 'Group 7' },
  ];
  dataSelect: DataModel = { check: false, code: '', tdesc: '', edesc: '' }

  constructor(private toastr: ToastrService) {
    this.pathTitleChange();
  }

  pathTitleChange(data?: DataModel) {
    this.editTab = data ? true : false
    this.dataSelect =  data ||  { check: false, code: '', tdesc: '', edesc: '' }
    this.sendPathTitle.emit(
      this.addTab
        ? ['การประเมินสมรรถนะ', 'ทะเบียนเกรด', 'การจัดการเกรด', 'เพิ่มกลุ่มเกรด']
        : this.editTab
          ? ['การประเมินสมรรถนะ', 'ทะเบียนเกรด', 'การจัดการเกรด', 'แก้ไขกลุ่มเกรด']
          : ['การประเมินสมรรถนะ', 'ทะเบียนเกรด', 'การจัดการเกรด']
    );

  }

  searchChange() {
    this.currentPage = 1;
    const filteredData = this.dataListFilter();
    this.page = Array.from({ length: Math.ceil(filteredData.length / 10) }, (_, i) => i + 1);
  }

  dataListFilter() {
    return this.dataList.filter((x) => {
      const match = x.code.includes(this.search) || x.tdesc.includes(this.search);
      if (!match) x.check = false;
      return match;
    });
  }

  dataListCheck() {
    const dataCheck = this.dataListFilter();
    this.isDataListChecked = dataCheck.some((x) => x.check);
    this.isDataListCheckedAll = dataCheck.length ? dataCheck.every((x) => x.check) : false;
    this.numDataListChecked = dataCheck.filter((x) => x.check).length;
  }

  dataListCheckAll() {
    const selectAll = this.isDataListCheckedAll;
    this.dataList.forEach((x) => (x.check = selectAll));
    this.dataListCheck(); // อัปเดตสถานะการเช็ก
  }

  showSuccess() {
    this.toastr.success('เพิ่มผู้ใช้ใหม่สำเร็จ!', 'เเจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }
  showEdit() {
    this.toastr.success('ลบข้อมูลสำเร็จ', 'เเจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }

  showSuccessDelete() {
    this.toastr.success('ลบข้อมูลสำเร็จ', 'เเจ้งเตือน', {
      timeOut: 3000,
      positionClass: 'toast-top-right',
    });
  }

  addUser() {

  }
}