Commit b9c354e1 by Natthaphat

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

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