import { Component, inject, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { FormsModule, ReactiveFormsModule, UntypedFormBuilder, Validators } from '@angular/forms'; import { Subject, Subscription} from 'rxjs'; import { MessageService } from 'primeng/api'; import {Staff, Code, userRole, ResetPassword} from '../models'; import { LookupService, Utils } from '../shares'; import { StaffService } from './staff.service'; import { CommonModule } from '@angular/common'; import { ButtonModule } from 'primeng/button'; @Component({ templateUrl: 'staff.pass.component.html', selector: 'staff-pass', imports:[ReactiveFormsModule,CommonModule,ButtonModule], styleUrls: ['staff.pass.component.css'] }) export class StaffPassComponent implements OnInit, OnDestroy { returnUrl =''; loginUser =''; _error =''; _id= -1; isNew = false; validationPoints?: Code[]; msg="[adminUser Component] "; private formBuilder = inject(UntypedFormBuilder); //for focus input // @ViewChild('mystaffid') mystaffNo!: MatInput; isChange = true; // disable use false//true for not disable. make sure it true is disable button. private subscription:Subscription = new Subscription(); subChanged$ = new Subject(); adminuserForm = this.formBuilder.group({ id: [0], //Validators.required password: ['',Validators.required], //Validators.required passwordAgain: ['',Validators.required], //Validators.required }); constructor( private staffService: StaffService, private messageService: MessageService, private lookupService: LookupService, private router: Router, private route: ActivatedRoute, ) { //item = {id:userRole.Normal, name: 'Switch', status:'Switch'}; //this.Roles.push(item); } getClassForRequire(prev:string,name:string){ const notok = !this.adminuserForm.controls[name].valid && this.adminuserForm.controls[name].touched; let str =prev; if (notok) str += " ng-invalid ng-dirty"; return str; } ngOnInit():void { this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; const id = Number(this.route.snapshot.paramMap.get('id')); // now load thing up const user = Utils.getCurrentUser(); // console.log(this.msg + "current login user ", user); if (user.username === '') alert("you are not login."); else this.loginUser = user.firstName; this._id = id; //console.log(this.msg + " " + id ); this.subscription.add(this.adminuserForm.valueChanges.subscribe(x => this.isChange = false)); this.subscription.add(this.subChanged$.subscribe(x => this.isChange = x)); } getEditText(): string { return "Reset Password"; } // this for disable submit button get isFieldsChange() { const chan = this.isChange || !this.adminuserForm.valid; // this disable so need true valid = true not //console.log(this.msg + 'is fields change', chan); return chan; } searchUser(): void { const staffid = this.adminuserForm.controls["stafflinkNo"].value; // console.log(this.msg + " the staffid is ", staffid); if (staffid != '') { //this.form.controls['your form control name'].value this.staffService.loadStaffById(staffid).subscribe({ next: x => { //console.log(this.msg + " this is stafflink no ", x); // now get current user. if (x.statusCode == 1) { //this.error = true; const fname = x.data.firstname + " " + x.data.lastname; this.adminuserForm.patchValue({name:fname, addedBy:this.loginUser}); } else { this.messageService.add({severity:'error', summary: 'Error', detail: x.message }); } }, error: e => { const message = e || e.message; //this.toastr.error(message); console.log("error ", e); } }); } else { //this.toastr.error("please enter staff link no"); } } // convenience getter for easy access in form fields get f() { return this.adminuserForm.controls;} validate(adminuser:any, passwordAgain:string): boolean { let result = true; const pass = adminuser.password.trim(); if (pass == '') { this._error = 'password is blank or empty'; result = false; } if (pass !== passwordAgain) { this._error = 'password is not match'; result = false; } return result; } onSubmit(): void { // if form valid then go save //make sure const okToSave = this.adminuserForm.valid; if (okToSave) { let adminuserValue = this.adminuserForm.value; let adminuser:ResetPassword = { id: this._id, password: adminuserValue.password, }; this._error =''; const again = adminuserValue.passwordAgain; const allOK = this.validate(adminuserValue, again); if (allOK) { this.subscription.add ( this.staffService.saveResetPassword(adminuser).subscribe({ next: x => { if (x.statusCode >= 1) { this.messageService.add({severity:'success', summary: 'Save user', detail: adminuser.id.toString() }); this.router.navigate([this.returnUrl]); } else { this.messageService.add({severity:'error', summary: 'Error', detail: x.message }); } }, error: e => { const message = e || e.message; // this.toastr.error(message); console.log("error ", e); } }) ); } else this.messageService.add({severity:'error', summary: 'Error', detail: this._error }); } } goBack(): void { this.router.navigate([this.returnUrl]); } ngOnDestroy() { this.subscription.unsubscribe(); } }