import { Component, OnInit, ViewChild } from '@angular/core'; import { UserService } from '../user.service'; import { Subscription } from 'rxjs/Subscription'; import { ActivatedRoute, Router } from '@angular/router'; import { cstmdummyHFAC, cstmdummyUserLevels } from '../../../_models/cstmdummydata'; import { DropDownsHR } from '../../../_helpers/dropdowns.class'; import { RootService } from '../../../_services/root.service'; import { UserClaims, User } from '../user-claims.class'; import { switchMap } from 'rxjs/operators/switchMap'; import { from } from 'rxjs/observable/from'; import { delay } from 'rxjs/operators/delay'; import { map } from 'rxjs/operators/map'; import { ClientService } from '../../clients/clients.service'; import { LocalService } from '../../../_services/local.service'; import { ItemViewModel } from '../../stock/MedStock.class'; import { NgForm } from '@angular/forms'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; @Component({ selector: 'app-register', templateUrl: './register.component.html' }) export class RegisterComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; @ViewChild(`division`) division; @ViewChild(`district`) district; @ViewChild(`tehsil`) tehsil; @ViewChild(`category`) category; @ViewChild(`hfTypeList`) hfType; private subscription: Subscription; public divisions: any[] = []; public districts: any[] = []; public tehsils: any[] = []; public categories: any[] = []; public hfTypes: any[] = []; public loading: boolean = false; public userId: string = ''; public user: User = new User(); public roles: any[] = []; public rolesData: any[] = []; public _userClaims: UserClaims[] = []; public userClaim: UserClaims = new UserClaims(); public comList: any[] = []; public _claimsList: any[]; public _isEdit = false; public allSelected = false; public selectedFiltersModel: any; public dropdata: Array<{ Name: string, Id: number }> = cstmdummyHFAC.slice(); constructor(private router: Router, private _rootService: RootService, private _userService: UserService, private _clientService: ClientService, private route: ActivatedRoute, private _localService: LocalService, private _router: Router) { super(); } ngOnInit() { this.route.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this._userService.getRegisterUserById(accId).subscribe((res: any) => { console.log(res); this.user = res; this._isEdit = true; }, (err) => { this.user = new User(); this._isEdit = false; }); } else { } }); this.checkUserSubscription(); this.getRoles(); this.getClients(); this.loadDropdownValues(); this.getClaimsList(); } selectAll($event) { if (this.allSelected == true) { this._claimsList.forEach(x => { this.user.UserClaimsList.splice(this.user.UserClaimsList.indexOf(x.Value), 1); }); this.user.UserClaimsList = []; } if (this.allSelected == false) { this.user.UserClaimsList = []; this._claimsList.forEach(x => { this.user.UserClaimsList.push(x.Value); }); } this.allSelected = !this.allSelected; } public isClaimChecked(claim) { this.user.UserClaimsList.includes(claim); return this.user.UserClaimsList.includes(claim); } public _claimsToPost = []; public checkChanged(value) { let checked = this.user.UserClaimsList.includes(value); // console.log(this._claimsList); // console.log(value); // console.log(checked); if (checked == false) { this.user.UserClaimsList.push(value); } if (checked == true) { this.user.UserClaimsList.splice(this.user.UserClaimsList.indexOf(value), 1); } } public getRoles = () => { this._userService.getRoles().subscribe( (res: any) => { this.roles = res as any; this.rolesData = this.roles; } ); } public getClaimsList() { this._userService.getClaimsList().subscribe((res: any) => { this._claimsList = res; if (this._isEdit == true) { this.allSelected = (this.user.UserClaimsList.length == this._claimsList.length) ? true : false; } console.log(res); }, (err) => { console.log(err); }); } public getClients = () => { this._clientService.getClients().subscribe( (res: any) => { this.comList = res as any; } ); } public canCreateUser = true; public checkUserSubscription() { let com = this._localService.get('com'); this._userService.checkClientSubscription(com.Id).subscribe((res: any) => { console.log(res); this.canCreateUser = res; if (res == false) { this._router.navigate(['/user']); } }, (err) => { console.log(err); }); } public userNameAlreadyExists = false; checkUserName(event) { this._userService.checkUserNameExistance(this.user.UserName).subscribe((res: any) => { this.userNameAlreadyExists = res; if (res == true) { // already exists } }, (err) => { }); } public emailAlreadyExists = false; checkEmail(event) { this._userService.checkEmailExistance(this.user.Email).subscribe((res: any) => { this.emailAlreadyExists = res; if (res == true) { // already exists } }, (err) => { }); } public onSubmit(val) { // console.log(val); // console.log(this.user); // if (this.user.role && this.user.role.length > 0) { // this.user.roles.push(this.user.role); // } let com = this._localService.get('com'); this.user.roles = 'Client-User'; this.user.ConfirmPassword = this.user.Password; // this.user.clientId = new ItemViewModel(com.Id, com.Name); debugger; this._userService.saveUser(this.user).subscribe((response: any) => { if (response) { console.log(response); this.router.navigate(['/user']); } }, err => { //this. = false; this.handleError(err); }); } private loadDropdownValues = () => { } public dropdownValueChanged = (value, filter) => { if (!value) { return; } } public addCodes(value: any[]) { let arr: any[] = []; value.forEach(val => arr.push(val.Code)); return arr; } private handleError(err: any) { this.loading = false; console.log(err); } }