Commit 31087b14 by Nattana Chaiyamat

กลุ่มพนักงาน

parent 466ca458
...@@ -120,7 +120,7 @@ ...@@ -120,7 +120,7 @@
</span> </span>
</label> </label>
<div class="relative flex rounded-md w-1/2"> <div class="relative flex rounded-md w-1/2">
<input type="text" class="ti-form-input h-16" <input type="text" class="ti-form-input"
[class.!border-red]="currentModal=='add'&&checkPrimary()" [class.!border-red]="currentModal=='add'&&checkPrimary()"
[class.!bg-input-readonly]="currentModal=='edit'" [readonly]="currentModal=='edit'" [class.!bg-input-readonly]="currentModal=='edit'" [readonly]="currentModal=='edit'"
[(ngModel)]="bu7.select.bu7id"> [(ngModel)]="bu7.select.bu7id">
......
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { ColumnModel } from '@syncfusion/ej2-grids';
import { ToastrService } from 'ngx-toastr'; import { ToastrService } from 'ngx-toastr';
import { EmpGroupModel, MyEmpGroupModel } from 'src/app/shared/model/emp-group.model'; import { EmpGroupModel, MyEmpGroupModel } from 'src/app/shared/model/emp-group.model';
import { EmpGroupService } from 'src/app/shared/services/emp-group.service'; import { EmpGroupService } from 'src/app/shared/services/emp-group.service';
...@@ -19,20 +20,35 @@ export interface DataModel { ...@@ -19,20 +20,35 @@ export interface DataModel {
export class EmployeeGroupUnit implements OnInit { export class EmployeeGroupUnit implements OnInit {
currentPage = 1 currentPage = 1
pageSize = 10 pageSize = 10
selectedItems: string[] = []; currentModal: 'add' | 'edit' | 'delete' = 'add'
modalStatus: 'add' | 'edit' | 'delete' | 'deleteGroup' = 'add'
page = Array.from({ length: 1 }, (_, i) => i + 1); page = Array.from({ length: 1 }, (_, i) => i + 1);
dataLoading = false emp_group: { loading: boolean, select: EmpGroupModel, dataList: EmpGroupModel[] } = { loading: false, select: new MyEmpGroupModel(), dataList: [] }
dataSelect: DataModel = { groupId: "", tdesc: "", edesc: "", companyId: "", checked: false }
emp_groupList: { check: boolean; data: DataModel & { checked?: boolean } }[] = []
emp_group: EmpGroupModel = new MyEmpGroupModel({})
search = ""
selectedFile: File | null = null; selectedFile: File | null = null;
selectedFileName: string = 'กรุณาเลือกไฟล์'; selectedFileName: string = 'กรุณาเลือกไฟล์';
numDataListChecked = 0 columns: ColumnModel[] = [{
isDataListChecked = false field: "groupId",
isDataListCheckedAll = false headerText: "รหัสฝ่าย",
type: "string",
isPrimaryKey: true,
},
{
field: "tdesc",
headerText: "รายละเอียดฝ่าย(ไทย)",
type: "string"
},
{
field: "edesc",
headerText: "รายละเอียดฝ่าย(อังกฤษ)",
type: "string"
}]
searchSettings = {
fields: ['groupId', 'tdesc', 'edesc'],
operator: 'contains',
ignoreCase: false
};
search = ''
selectedItems: { key: string, count: number, data: Map<string, boolean> } = { key: '', count: 0, data: new Map<string, boolean>() };
constructor(private empGroupService: EmpGroupService, constructor(private empGroupService: EmpGroupService,
private toastr: ToastrService, private toastr: ToastrService,
private cdr: ChangeDetectorRef, private cdr: ChangeDetectorRef,
...@@ -55,7 +71,7 @@ export class EmployeeGroupUnit implements OnInit { ...@@ -55,7 +71,7 @@ export class EmployeeGroupUnit implements OnInit {
} }
const formData = new FormData(); const formData = new FormData();
formData.append('file', this.selectedFile); formData.append('file', this.selectedFile);
this.dataLoading = true this.emp_group.loading = true
this.fileService.uploadExcel(formData, 'mgroup').subscribe({ this.fileService.uploadExcel(formData, 'mgroup').subscribe({
next: response => { next: response => {
if (response.success) { if (response.success) {
...@@ -63,11 +79,11 @@ export class EmployeeGroupUnit implements OnInit { ...@@ -63,11 +79,11 @@ export class EmployeeGroupUnit implements OnInit {
this.getEmpGroupList() this.getEmpGroupList()
} else { } else {
this.showAlert(response.message, 'error') this.showAlert(response.message, 'error')
this.dataLoading = false this.emp_group.loading = false
} }
}, error: error => { }, error: error => {
this.showAlert(error.message, 'error') this.showAlert(error.message, 'error')
this.dataLoading = false this.emp_group.loading = false
} }
}) })
} }
...@@ -92,66 +108,58 @@ export class EmployeeGroupUnit implements OnInit { ...@@ -92,66 +108,58 @@ export class EmployeeGroupUnit implements OnInit {
} }
getEmpGroupList() { getEmpGroupList() {
this.dataLoading = true this.emp_group.loading = true
this.selectedItems.data.clear()
this.empGroupService.getList().subscribe({ this.empGroupService.getList().subscribe({
next: response => { next: response => {
this.emp_groupList = response.map(x => ({ check: false, data: { groupId: x.groupId, tdesc: x.tdesc, edesc: x.edesc, companyId: x.companyId, checked: false } })) this.emp_group.dataList = response.map(x => {
this.dataLoading = false this.selectedItems.data.set(x.groupId, false)
this.searchChange() return new MyEmpGroupModel(x)
this.isDataListCheckedAll = false })
this.dataListCheckAll() this.selectedItems.key = 'groupId'
this.selectedItems.count = 0
this.emp_group.loading = false
this.cdr.detectChanges() this.cdr.detectChanges()
}, error: error => { }, error: error => {
this.dataLoading = false this.emp_group.loading = false
console.error('Error fetching employee types:', error);
this.cdr.detectChanges() this.cdr.detectChanges()
} }
}) })
} }
searchChange() {
this.currentPage = 1 selectEmp_group(emp_group?: EmpGroupModel) {
this.page = Array.from({ length: Math.ceil(this.emp_groupListFilter().length / 10) }, (_, i) => i + 1); if (emp_group) {
this.dataListCheck() this.emp_group.select = new MyEmpGroupModel(emp_group)
} } else if (this.currentModal == 'add') {
emp_groupListFilter() { this.emp_group.select = new MyEmpGroupModel()
return this.emp_groupList.filter(x => { } else if (this.currentModal == 'edit') {
const data = x.data this.emp_group.select = new MyEmpGroupModel({ groupId: this.emp_group.select.groupId })
const match = data.groupId.toLowerCase().includes(this.search.toLowerCase()) || data.tdesc.toLowerCase().includes(this.search.toLowerCase()) || data.edesc.toLowerCase().includes(this.search.toLowerCase()); }
return match;
});
}
setData(data?: DataModel) {
this.dataSelect = JSON.parse(JSON.stringify(data || { groupId: "", tdesc: "", edesc: "", companyId: "" }));
} }
addEmp_group() { addEmp_group() {
const body = new MyEmpGroupModel({ groupId: this.dataSelect.groupId, tdesc: this.dataSelect.tdesc, edesc: this.dataSelect.edesc, companyId: this.dataSelect.companyId }) this.emp_group.loading = true
this.dataLoading = true this.empGroupService.post(this.emp_group.select).subscribe({
this.empGroupService.post(body).subscribe({
next: response => { next: response => {
if (response.success) { if (response.success) {
this.showAlert(response.message, 'success') this.showAlert(response.message, 'success')
this.getEmpGroupList() this.getEmpGroupList()
} else { } else {
this.showAlert(response.message, 'error') this.showAlert(response.message, 'error')
this.dataLoading = false this.emp_group.loading = false
this.cdr.detectChanges() this.cdr.detectChanges()
} }
}, error: error => { }, error: error => {
this.showAlert(error.message, 'error') this.showAlert(error.message, 'error')
this.dataLoading = false this.emp_group.loading = false
this.cdr.detectChanges() this.cdr.detectChanges()
} }
}) })
} }
deleteEmp_group() { deleteEmp_group() {
let body: EmpGroupModel | EmpGroupModel[] = []; this.emp_group.loading = true
if (this.dataSelect.groupId) { const selectedKeys = Array.from(this.selectedItems.data.keys());
body = new MyEmpGroupModel({ groupId: this.dataSelect.groupId, tdesc: this.dataSelect.tdesc, edesc: this.dataSelect.edesc, companyId: this.dataSelect.companyId }); const body = this.emp_group.dataList.filter(x => selectedKeys.includes(x.groupId) && this.selectedItems.data.get(x.groupId)).map(x => new MyEmpGroupModel(x))
} else {
body = this.emp_groupList.filter(x => x.check).map(x => new MyEmpGroupModel({ groupId: x.data.groupId, tdesc: x.data.tdesc, edesc: x.data.edesc, companyId: x.data.companyId }));
}
this.dataLoading = true
this.empGroupService.delete(body).subscribe({ this.empGroupService.delete(body).subscribe({
next: response => { next: response => {
if (response.success) { if (response.success) {
...@@ -159,35 +167,18 @@ export class EmployeeGroupUnit implements OnInit { ...@@ -159,35 +167,18 @@ export class EmployeeGroupUnit implements OnInit {
this.getEmpGroupList(); this.getEmpGroupList();
} else { } else {
this.showAlert(response.message, 'error'); this.showAlert(response.message, 'error');
this.dataLoading = false this.emp_group.loading = false
this.cdr.detectChanges() this.cdr.detectChanges()
} }
}, },
error: error => { error: error => {
this.showAlert(error.message, 'error') this.showAlert(error.message, 'error')
this.dataLoading = false this.emp_group.loading = false
this.cdr.detectChanges() this.cdr.detectChanges()
} }
}); });
} }
dataListCheckAll() {
const selectAll = this.isDataListCheckedAll;
this.emp_groupList.filter(x => {
const data = x.data
const match = data.groupId.toLowerCase().includes(this.search.toLowerCase()) || data.tdesc.toLowerCase().includes(this.search.toLowerCase()) || data.edesc.toLowerCase().includes(this.search.toLowerCase());
return match;
}).forEach(x => x.check = selectAll);
this.dataListCheck();
}
dataListCheck() {
const dataCheck = this.emp_groupListFilter();
this.isDataListCheckedAll = dataCheck.length ? dataCheck.every(x => x.check) : false;
this.numDataListChecked = this.emp_groupList.filter(x => x.check).length;
this.isDataListChecked = Boolean(this.numDataListChecked)
}
showAlert(text: string, type: 'success' | 'error') { showAlert(text: string, type: 'success' | 'error') {
this.toastr[type](text, 'แจ้งเตือน', { this.toastr[type](text, 'แจ้งเตือน', {
timeOut: 3000, timeOut: 3000,
...@@ -195,18 +186,17 @@ export class EmployeeGroupUnit implements OnInit { ...@@ -195,18 +186,17 @@ export class EmployeeGroupUnit implements OnInit {
}) })
} }
clearEmp_group(modalStatus: string) { checkPrimary() {
if (modalStatus == 'add') { return this.emp_group.dataList.find(x => x.groupId == this.emp_group.select.groupId)
this.dataSelect.groupId = ''
this.dataSelect.tdesc = ''
this.dataSelect.edesc = ''
} else if (modalStatus == 'edit') {
this.dataSelect.tdesc = ''
this.dataSelect.edesc = ''
}
} }
checkPrimary() { numSelectItem() {
return this.emp_groupList.find(x => x.data.groupId == this.dataSelect.groupId) const selectedKeys = Array.from(this.selectedItems.data.keys());
const num = this.emp_group.dataList.filter(x => selectedKeys.includes(x.groupId) && this.selectedItems.data.get(x.groupId)).length
return num
}
onSelectItemChange(arg: any) {
this.selectedItems = arg
} }
} }
export interface EmpGroupModel{ export interface EmpGroupModel {
groupId: string; groupId: string;
tdesc: string; tdesc: string;
edesc: string; edesc: string;
...@@ -11,11 +11,11 @@ export class MyEmpGroupModel implements EmpGroupModel { ...@@ -11,11 +11,11 @@ export class MyEmpGroupModel implements EmpGroupModel {
tdesc: string; tdesc: string;
edesc: string; edesc: string;
companyId: string; companyId: string;
constructor(data: Partial<EmpGroupModel>) { constructor(data?: Partial<EmpGroupModel>) {
this.groupId = data.groupId || "" this.groupId = data?.groupId || ""
this.tdesc = data.tdesc || "" this.tdesc = data?.tdesc || ""
this.edesc = data.edesc || "" this.edesc = data?.edesc || ""
this.companyId = data.companyId || "" this.companyId = data?.companyId || ""
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment