changeuserid.component.ts 5.49 KB
import { Component, OnInit, AfterViewInit,ViewChild } from '@angular/core';
import { ChangeUserIDService } from '../ChangeUserID/changeuserid.service';
import { UserService } from '../UpdateProfile/user.service';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { UserInfo } from '../ChangeUserID/UserInfo';
import { Http, Response } from '@angular/http';
//import { Global } from '../../Shared/global';
//import { DBOperation } from 'S';
import { Observable } from 'rxjs/Observable';
import { ConfirmService } from '../../Shared/Confirm/confirm.service';
import 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
 

@Component({
  templateUrl:'./changeuserid.component.html' // '../../../../../wwwroot/html/UpdateProfile/updateuserprofile.component.html'
})

export class ChangeUserID implements OnInit {
  UserId: number=1;
  //users: IUser[];
  user: UserInfo;
  //msg: string;
  //indLoading: boolean = false;
 // dbops: DBOperation;
  modalTitle: string;
  modalBtnTitle: string;
  baseUrl: string = "User";
  ChangeUserIdFrm: FormGroup;
  useFname: string;
  error;
  status: boolean;
  alerts: string;
  emailPattern = "^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$";
  //@ViewChild("profileModal")
  //profileModal: ModalComponent;
  //errorMessage: any;
  constructor(private userservice: UserService, private router: Router, private fb: FormBuilder, private http: Http,
    private _confirmService: ConfirmService
  ) { }
 
  ngOnInit(): void {
    this.user = new UserInfo();
    this.alerts = '';
      //this.userservice.GetUserById(this.UserId);
    this.ChangeUserIdFrm = this.fb.group({
      id: [''],
      loginid: ['', Validators.required],
      newloginid: ['', Validators.required],
      confirmloginid: ['', Validators.required]
    //  LastName: [''],
     // Gender: ['', Validators.required],
   //   Email: ['']
          
    });
    this.GetUserById();
  }
  //ngAfterviewint() {
  //  this.LoadUsers();
  //}
  
  
  //getCustomerById(UserId) {
  //  return this.userservice.GetUserById(UserId)
  //    .map((response: Response) => response.json())
  //    .catch(this._errorHandler)
  //}
    //formErrors = {
    //    'firstName': '',
    //    'lastName': '',
    //    'email': ''
    //};
  GetUserById() {

    this.userservice.GetUserById()
      .subscribe(x => { console.log(x); this.bindUsers(x) }, error => this.error = <any>error);    
  }
  UpdateUserProfile(this) {
   // debugger;
    this.user = this.ChangeUserIdFrm.value;
    //if(this.user.)
    console.log(this.user);
    var obj = this.user
    if (this.ChangeUserIdFrm.valid) {
      return this.userservice.UpdateUserProfileById(obj)
        .subscribe(
        n => (this.AfterInsertData(n)),
        error => this.error = <any>error);
    }
  }
  AfterInsertData(data) {
    //debugger;
    if (data.Status == "False") {
      // this._confirmService.activate(data.ResponseMessage, "alertMsg");
      //setTimeout(() => this.amCode.nativeElement.focus(), 0);
      // this.closeflag = false;
      return false;
    } else {
      this.status = true;
      debugger;
      
      this._confirmService.activate("User Profile Updated Successfully.", "alertMsg");
      //this.profileModal.open();
      // this.submitted = false;
      // this.GetAllAcctMgr();
      // this.DisableAllControls();
      //  this.AccountManagerID.enable();
      //  let accountManagerID: string = data.Id == null ? "" : data.Id.toString();
      // console.log(accountManagerID);

      //   this.GetAcctMgr(Number(accountManagerID));
      //this.GetAccountManagerListOptions(false);
      // this.defautValue = data.id;
      //  this.BtnReset = true;
      //  this.BtnSave = true;
      //   this.BtnEdit = false;
      //   this.BtnNew = false;
      //  this.BtnDelete = false;
      // this.Abbrevtion = this.Abbrev.value;
      //  setTimeout(() => this.AMform.controls["AccountManagerID"].setValue(accountManagerID.toString()), 1000);
      // setTimeout(() => this.AMform.markAsPristine(), 2000);
    }
    //if (this.closeflag) {
    //  this.close.emit(null);
    //}
    //else {
    //}
  }
  bindUsers(data) {
    debugger; 
    //console.log(data);
    //alert(JSON.stringify(data));
    this.user = data[0];

    console.log(this.user);
    this.ChangeUserIdFrm.controls['id'].setValue(this.user.Id)
    this.ChangeUserIdFrm.controls['loginid'].setValue(this.user.LoginId)
    this.ChangeUserIdFrm.controls['newloginid'].setValue('')
    this.ChangeUserIdFrm.controls['confirmloginid'].setValue('')
   // this.GetClientListOptions(false);
   // this.GetCrossRefClientListOptions(false);
  }
  //extractData(res: Response) {
  //  //debugger;   
  //  let body = res.json();
  //  return body;
  //}
  //handleError(error: any) {

  //  // In a real world app, we might use a remote logging infrastructure
  //  // We'd also dig deeper into the error to get a better message
  //  let errMsg = (error.message) ? error.message :
  //    error.status ? `${error.status} - ${error.statusText}` : 'Server error';
  //  console.error(errMsg); // log to console instead
  //  return Observable.throw(errMsg);
  //}
    validationMessages = {
        'firstName': {
            'required': 'First name is required.'
        },
        'lastName': {
            'required': 'Last name is required.'
        },
        'email': {
            'required': 'Email is required.',
            'pattern': 'Email pattern is not valid.'
        }
    }
  
}