Commit b9c354e1 by Natthaphat

จัดการบริษัท แก้ภาษา

parent 956ae068
......@@ -82,7 +82,7 @@
</div>
</td>
<td>
<span class="badge bg-{{ item.status == 1 ? 'primary' : 'danger'}} text-white">
<span class="badge bg-{{ item.status == 1 ? 'primary' : 'warning'}} text-white">
{{item.getStatus()}}
</span>
</td>
......@@ -187,13 +187,23 @@
<img [src]="selectModel.getPicture()" alt="" id="profile-img">
<span class="badge rounded-full bg-primary avatar-badge">
<input (click)="triggerFileInput()" ng2FileSelect [uploader]="uploaderProfile" type="file"
name="photo" class="absolute w-full h-full opacity-[0]" id="profile-change" [disabled]="action === 'edit'">
name="photo" class="absolute w-full h-full opacity-[0]" id="profile-change"
[disabled]="action === 'edit'">
<i class="fe fe-camera text-[.625rem]"></i>
</span>
</span>
</div>
</div>
<div class="xl:col-span-12 col-span-12" *ngIf="action == 'edit'">
<label for="companyId" class="form-label">{{'รหัสบริษัท' | translate}}</label>
<input type="text" class="form-control !bg-input-readonly" id="companyId" placeholder=""
[(ngModel)]="selectModel.companyId" [disabled]="action === 'edit'">
<!-- <div class="text-danger" *ngIf="!selectModel.companyId && action === 'add'">
{{'Please fill in information' | translate}}
</div> -->
</div>
<div class="xl:col-span-12 col-span-12">
<label for="thName" class="form-label">{{'ชื่อบริษัท ไทย' | translate}}</label>
<input type="text" class="form-control" id="thName" placeholder="" [(ngModel)]="selectModel.thName">
......@@ -209,34 +219,43 @@
</div>
</div>
<div class="xl:col-span-12 col-span-12" *ngIf="action == 'edit'">
<label for="companyId" class="form-label">{{'รหัสบริษัท' | translate}}</label>
<input type="text" class="form-control !bg-input-readonly" id="companyId" placeholder="" [(ngModel)]="selectModel.companyId"
[disabled]="action === 'edit'" >
<!-- <div class="text-danger" *ngIf="!selectModel.companyId && action === 'add'">
{{'Please fill in information' | translate}}
</div> -->
</div>
<div class="xl:col-span-12 col-span-12">
<label for="email" class="form-label">{{'Email' | translate}}</label>
<input type="email" class="form-control" id="emailAddress" placeholder="Email Addresss" [(ngModel)]="selectModel.email" (input)="filterEngInput($event)">
<input type="email" class="form-control" id="emailAddress" placeholder="Email Addresss"
[(ngModel)]="selectModel.email" (input)="filterEngInput($event)">
<div class="text-danger" *ngIf="!selectModel.email">
{{ 'Please fill in information' | translate }}
</div>
<div class="text-danger" *ngIf="selectModel.email && (!selectModel.email.includes('@'))">
{{ 'Invalid email format @' | translate }}
</div>
<div class="text-danger" *ngIf="selectModel.email && selectModel.email.includes('@') && !selectModel.email.split('@')[1]?.includes('.')">
<div class="text-danger"
*ngIf="selectModel.email && selectModel.email.includes('@') && !selectModel.email.split('@')[1]?.includes('.')">
{{ 'Invalid email format .' | translate }}
</div>
</div>
<!-- <div class="xl:col-span-12 col-span-12">
<!-- <div class="xl:col-span-6 col-span-12" *ngIf="action !== 'edit'">
<label for="password" class="form-label">{{'Password' | translate}}</label>
<input type="password" class="form-control" id="emailAddress" placeholder="Choose Password" [(ngModel)]="selectModel.password">
<input type="password" class="form-control" id="password" placeholder="" [(ngModel)]="password">
<div class="text-danger" *ngIf="!password">
{{ 'Please fill in information' | translate }}
</div>
</div>
<div class="xl:col-span-6 col-span-12" *ngIf="action !== 'edit'">
<label for="deal-title" class="form-label">{{'Confirm Password' | translate}}</label>
<input type="password" class="form-control" id="deal-title" placeholder="" [(ngModel)]="confirmPassword">
<div class="text-danger" *ngIf="!confirmPassword">
{{'Please fill in information' | translate}}
</div>
<div class="text-danger" *ngIf="confirmPassword && (confirmPassword != password)">
{{'Password Not Match' | translate}}
</div>
</div> -->
<!-- <div class="xl:col-span-12 col-span-12">
<label for="companyDetail" class="form-label">{{'ข้อมูลบริษัท' | translate}}</label>
<textarea class="form-control" id="companyDetail" placeholder="" [(ngModel)]="selectModel.companyDetail">
......@@ -285,10 +304,27 @@
data-hs-overlay="#modal-detail">
{{'Cancel' | translate}}
</button>
<button type="button" (click)="save()" class="ti-btn bg-primary text-white !font-medium"
[class.ti-btn-disabled]="!selectModel.thName||!selectModel.engName"
[disabled]="!selectModel.thName||!selectModel.engName">
{{'Save' | translate}}</button>
<button *ngIf="action === 'add'" type="button" (click)="save()"
class="ti-btn bg-primary text-white !font-medium" [class.ti-btn-disabled]="!selectModel.thName||
!selectModel.engName ||
(!selectModel.email || isEmailDuplicate || !selectModel.email.includes('@') || !selectModel.email.includes('.'))"
[disabled]="!selectModel.thName||
!selectModel.engName ||
(!selectModel.email || isEmailDuplicate || !selectModel.email.includes('@') || !selectModel.email.includes('.'))">
<!-- !password || !confirmPassword || (confirmPassword !== password)" -->
{{'Save' | translate}}</button>
<button *ngIf="action === 'edit'" type="button" (click)="save()"
class="ti-btn bg-primary text-white !font-medium"
[class.ti-btn-disabled]="!selectModel.thName||
!selectModel.engName ||
(!selectModel.email || isEmailDuplicate || !selectModel.email.includes('@') || !selectModel.email.includes('.'))"
[disabled]="!selectModel.thName||
!selectModel.engName ||
(!selectModel.email || isEmailDuplicate || !selectModel.email.includes('@') || !selectModel.email.includes('.'))">
{{'Save' | translate}}</button>
</div>
</div>
</div>
......
......@@ -14,6 +14,8 @@ import { environment } from '../../../../environments/environment';
import { TokenService } from '../../../shared/services/token.service';
import { CompanyModelS } from '../../models/companys.mode';
import { CompanyServiceS } from '../../services/companys.service';
import { AuthModel } from '../../models/auth.model';
import { RoleModel } from '../../models/role.model';
@Component({
selector: 'app-company-manage',
......@@ -24,7 +26,6 @@ import { CompanyServiceS } from '../../services/companys.service';
TranslateModule,
NgSelectModule,
FormsModule,
MatPaginator,
RouterModule,
FileUploadModule
],
......@@ -42,12 +43,18 @@ export class CompanyManageComponent {
filterList: CompanyModelS[] = [];
selectModel: CompanyModelS = new CompanyModelS();
selectedItems = new Map<string, boolean>();
existingEmails: CompanyModelS[] = []
// empList: CompanyModelS[] = [];
// descName = 'engName';
pageIndex = 0;
uploaderProfile: FileUploader | undefined;
uploadErrorMsg: string = "";
password: string = '';
confirmPassword = ""
isEmailDuplicate = false;
get searchTerm(): string {
return this._searchTerm;
}
......@@ -123,11 +130,11 @@ export class CompanyManageComponent {
next: (response: CompanyModelS[]) => {
this.itemsList = response.map((x: any) => new CompanyModelS(x, this.translate));
console.log('ข้อมูลบริษัท (itemsList)', this.itemsList);
this.updatePagedItems();
this.updatePagedItems();
},
error: (error) => {
console.error('error cant get company', error);
swal("ข้อผิดพลาด", "ไม่สามารถดึงข้อมูลบริษัทได้", "error");
swal("ข้อผิดพลาด", "ไม่สามารถดึงข้อมูลบริษัทได้", "error");
}
});
}
......@@ -136,6 +143,13 @@ export class CompanyManageComponent {
this.getCompany();
}
onEmailChange(email: string) {
const lowerEmail = email.trim().toLowerCase();
this.isEmailDuplicate = this.existingEmails.some(
user => user.email && user.email.toLowerCase() === lowerEmail
);
}
filter(v: string) {
return this.itemsList?.filter(
(x) =>
......@@ -158,7 +172,7 @@ export class CompanyManageComponent {
})
.then((willDelete: any) => {
if (willDelete) {
this.comService.deleteCompany(item.companyId).subscribe(result => {
this.comService.put(item).subscribe(result => {
swal("ลบสำเร็จ!!", "ลบข้อมูลสำเร็จ", "success");
this.ngOnInit();
}, error => {
......@@ -200,19 +214,97 @@ export class CompanyManageComponent {
})
.then((willSave: any) => {
if (willSave) {
this.comService.saveOrUpdateCompany(this.selectModel).subscribe(result => {
console.log(result);
swal("บันทึกสำเร็จ!!", "บันทึกข้อมูลสมาชิก", "success");
this.ngOnInit();
this.childModal?.nativeElement.click();
}, error => {
console.error("เกิดข้อผิดพลาดในการบันทึก/อัปเดต:", error);
swal("ข้อผิดพลาด!!", "ไม่สามารถบันทึก/อัปเดตข้อมูลได้", "error");
});
if (this.action === 'add') {
this.comService.saveOrUpdateCompany(this.selectModel).subscribe(result => {
console.log(result);
swal("บันทึกสำเร็จ!!", "บันทึกข้อมูลสมาชิก", "success");
this.ngOnInit();
this.childModal?.nativeElement.click();
}, error => {
console.error("เกิดข้อผิดพลาดในการบันทึก/อัปเดต:", error);
swal("ข้อผิดพลาด!!", "ไม่สามารถบันทึก/อัปเดตข้อมูลได้", "error");
});
} else if (this.action === 'edit') {
this.comService.saveOrUpdateCompany(this.selectModel).subscribe({
next: result => {
swal("Update Success!!", "บันทึกข้อมูลบริษัท", "success");
this.ngOnInit();
this.childModal?.nativeElement.click();
},
error: err => {
swal("Error", "ไม่สามารถอัปเดตได้", "error");
}
});
}
}
});
}
// save() {
// swal({
// title: "คุณแน่ใจหรือไม่?",
// text: "คุณต้องการบันทึกหรือไม่",
// icon: "warning",
// dangerMode: false,
// buttons: ["ยกเลิก", "ยืนยัน"],
// })
// .then((willSave: any) => {
// if (willSave) {
// if (this.action === 'add') {
// const body = new AuthModel();
// body.username = this.selectModel.email;
// body.password = this.password;
// body.role = new RoleModel({ roleId: 'employer' });
// const rawCompany = { ...body.company };
// rawCompany.thName = rawCompany.thName || this.selectModel.thName || "";
// rawCompany.engName = rawCompany.engName || this.selectModel.engName || "";
// rawCompany.email = rawCompany.email || this.selectModel.email || "";
// rawCompany.status = rawCompany.status ?? 1;
// body.company = new CompanyModelS(rawCompany);
// console.log(body);
// this.comService.register(body).subscribe({
// next: res => {
// if (this.selectModel.status === 1) {
// swal("Save Success!!", "บันทึกข้อมูลบริษัท", "success");
// this.ngOnInit();
// this.childModal?.nativeElement.click();
// } else {
// swal("Save Failed", res?.message || "ไม่สามารถลงทะเบียนได้", "warning");
// }
// },
// error: err => {
// console.error('Error response:', err);
// const errorMessage = err?.error?.message || err?.message || 'ไม่ทราบสาเหตุ';
// if (errorMessage.includes('email')) {
// swal("Save Failed", "อีเมลซ้ำหรือไม่ถูกต้อง", "warning");
// } else {
// swal("Error", errorMessage, "error");
// }
// }
// });
// } else if (this.action === 'edit') {
// this.comService.saveOrUpdateCompany(this.selectModel).subscribe({
// next: result => {
// swal("Update Success!!", "บันทึกข้อมูลบริษัท", "success");
// this.ngOnInit();
// this.childModal?.nativeElement.click();
// },
// error: err => {
// swal("Error", "ไม่สามารถอัปเดตได้", "error");
// }
// });
// }
// }
// });
// }
updatePagedItems() {
const startIndex = this.pageIndex * 10;
const endIndex = startIndex + 10;
......@@ -236,55 +328,36 @@ export class CompanyManageComponent {
}
deleteSelect() {
let companyInfo = '';
const selectedCompanyIdsToDelete: string[] = [];
let employerInfo = '';
this.selectedItems.forEach((isSelected, companyId) => {
if (isSelected) {
const item = this.itemsList.find(c => c.companyId === companyId);
if (item) {
companyInfo += `${this.translate.instant('บริษัท')}: ${item.thName}\n`;
selectedCompanyIdsToDelete.push(item.companyId);
const com = this.itemsList.find(com => com.companyId === companyId);
if (com) {
employerInfo += `${this.translate.instant('Fullname')}: ${com.getName()}\n`;
}
}
});
if (selectedCompanyIdsToDelete.length === 0) {
swal("ข้อผิดพลาด", "กรุณาเลือกบริษัทที่ต้องการลบ", "warning");
return;
}
swal({
title: "คุณแน่ใจหรือไม่?",
text: companyInfo,
title: "Are you sure?",
text: employerInfo,
icon: "warning",
dangerMode: true,
buttons: ["ยกเลิก", "ใช่, ลบเลย!"],
buttons: ["Cancel", "Yes, Delete it!"],
})
.then((willDelete: any) => {
if (willDelete) {
const deletePromises = selectedCompanyIdsToDelete.map(companyId =>
this.comService.deleteCompany(companyId).toPromise()
.then(() => true)
.catch(error => {
console.error(`Error deleting company ${companyId}:`, error);
return false;
})
);
Promise.all(deletePromises)
.then(results => {
const allSuccessful = results.every(success => success);
if (allSuccessful) {
swal("ลบสำเร็จ!!", "บันทึกข้อมูลสำเร็จ", "success");
} else {
swal("สำเร็จบางส่วน/ข้อผิดพลาด!!", "มีการลบข้อมูลบางส่วนไม่สำเร็จ หรือมีข้อผิดพลาด", "warning");
this.selectedItems.forEach((isSelected, companyId) => {
if (isSelected) {
const com = this.itemsList.find(com => com.companyId === companyId);
if (com) {
this.comService.put(com).subscribe(result => {
swal("Save Success!!", "บันทึกข้อมูลสำเร็จ", "success");
this.ngOnInit();
});
}
this.ngOnInit();
this.selectedItems.clear();
this.allSelected = false;
this.someSelected = false;
});
}
});
}
});
}
......
......@@ -330,7 +330,7 @@
</div>
</div>
<div class="xl:col-span-6 col-span-12">
<div class="xl:col-span-6 col-span-12" *ngIf="action !== 'edit'">
<label for="deal-title" class="form-label">{{'Confirm Password' | translate}}</label>
<input type="password" class="form-control" id="deal-title" placeholder="" [(ngModel)]="confirmPassword">
<div class="text-danger" *ngIf="!confirmPassword">
......
......@@ -2,12 +2,14 @@ import { TranslateService } from "@ngx-translate/core"
import { BaseModel } from "./base.model"
import { RoleModel } from "./role.model"
import { ProfileModel } from "./profile.model"
import { CompanyModelS } from "./companys.mode"
export interface AuthModel {
username: string
password: string
role: RoleModel
profile: ProfileModel
company: CompanyModelS
}
export class AuthModel extends BaseModel implements AuthModel {
......@@ -15,6 +17,7 @@ export class AuthModel extends BaseModel implements AuthModel {
password: string
role: RoleModel
profile: ProfileModel
company: CompanyModelS
constructor(data?: Partial<AuthModel>, translateService?: TranslateService) {
super(data, translateService)
......@@ -22,6 +25,7 @@ export class AuthModel extends BaseModel implements AuthModel {
this.password = data?.password!
this.role = data?.role ? new RoleModel(data.role, translateService) : data?.role!
this.profile = data?.profile ? new ProfileModel(data.profile, translateService) : data?.profile!
this.company = data?.company ? new CompanyModelS(data.company, translateService) : data?.company!
}
}
......
......@@ -149,9 +149,9 @@ export class CompanyModelS extends BaseModel implements CompanyModelS {
getStatus(): string {
if (this.status == 1) {
return this.translateService.instant('PUBLIC');
return this.translateService.instant('Public');
} else {
return this.translateService.instant('PENDING');
return this.translateService.instant('Pending');
}
}
......
......@@ -5,7 +5,8 @@ import { Injectable } from '@angular/core';
import { forkJoin, map, Observable, switchMap } from 'rxjs';
import { environment } from "../../../environments/environment";
import { CompanyModelS } from '../models/companys.mode';
import { PageResponseModel } from '../models/base.model'; // ตรวจสอบว่ามี BaseModel ใน base.model.ts
import { PageResponseModel, ResponseModel } from '../models/base.model'; // ตรวจสอบว่ามี BaseModel ใน base.model.ts
import { AuthModel } from '../models/auth.model';
@Injectable({
providedIn: 'root'
......@@ -13,6 +14,7 @@ import { PageResponseModel } from '../models/base.model'; // ตรวจสอ
export class CompanyServiceS {
api = "/admin/manage-company";
urlApi = environment.baseUrl + "/admin/manage-company";
apiAuthUrl = "/auth"
constructor(private http: HttpClient) { }
......@@ -21,7 +23,7 @@ export class CompanyServiceS {
}
getList(): Observable<CompanyModelS[]> {
return this.http.get<CompanyModelS[]>(this.urlApi + "/lists");
return this.http.get<CompanyModelS[]>(this.urlApi + "/lists?status=1");
}
getListByPageSize(body: { page: number; size: number }): Observable<PageResponseModel<CompanyModelS>> {
......@@ -60,6 +62,14 @@ export class CompanyServiceS {
);
}
register(body: AuthModel) {
return this.http.post<ResponseModel>(this.apiAuthUrl + "/register", body);
}
put(body: CompanyModelS) {
return this.http.put<ResponseModel>(this.urlApi, new CompanyModelS(body));
}
saveOrUpdateCompany(body: CompanyModelS): Observable<any> {
return this.http.post(this.urlApi, body); // ใช้ POST สำหรับทั้งคู่
}
......
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